Kontrola toka izvršenja

Slides:



Advertisements
Similar presentations
Chapter 3: Control Flow S. M. Farhad. Statements and Blocks An expression becomes a statement when it is followed by a semicolon Braces { and } are used.
Advertisements

Recursive Descent Technique CMSC 331. UMBC 2 The Header /* This program matches the following A -> B { '|' B } B -> C { '&' C } C -> D { '^' D } D ->
Sort the given string, without using string handling functions.
C Intro.
0 Chap. 3 Control Flow 3.1 Statements and Blocks Imperative Programming, B. Hirsbrunner, diuf.unifr.ch/pai/ip Session 4, 3 April if, if … else.
1 Homework Assignments Turn in HW1 (If not done yet, catch up!) Questions about HW1? Anyone still stuck on apply / UNIX account? Everyone have the books?
Introduction to C Programming CE Lecture 13 Strings in C.
Introduction to Computers and Programming Class 15 Introduction to C Professor Avi Rosenfeld.
Imperative Programming Prof. Béat Hirsbrunner Amine Tafat, PhD Student Matthias Buchs and Raphaël Lesceux, Graduate Students Department of Informatics.
0 Arrays (1/2) #include /* count digits, white space, others */ main() { int c, i, nwhite, nother; int ndigit[10]; nwhite = nother = 0; for (i = 0; i
Functions / Procedures
Review: midterm #9 #include void main(void) { int c; c = getchar(); if(c>=48){ if(c
C Programming A Modern Approach
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
C Tokens Identifiers Keywords Constants Operators Special symbols.
CSC 270 – Survey of Programming Languages C Lecture 4 – Strings Heavily borrowed from Dr. Robert Siegfried and Dietel How to Program in C Powerpoint Presentations.
Outline Symbolic Constants Character Input and Output if … else switch…case.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Programming Language  C Tutorial Introduction 主講人:虞台文.
CPT: Strings/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship.
Introduction to Computer Algorithmics and Programming Ceng 113 Program Control Statements.
1/16 Programski jezik C Vladimir Filipović
CS 162 Introduction to Computer Science Chapter 17 C++ String Objects Herbert G. Mayer, PSU (Copied from Prof. Phillip Wong at PSU) Status 11/30/2014.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Chapter 05 (Part III) Control Statements: Part II.
C Program Design C Arrays 主講人:虞台文. Content Basics Defining Arrays Array Initialization Static Arrays Passing Arrays to Functions Array Examples – Find.
Xuan Guo Review for the Final Exam Xuan Guo July 29 8:30AM – 10:30AM Classroom South 300 CSC
1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?
Programming Language  C Functions and Program Structure 主講人:虞台文.
Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd.
CS 261 C Basics Page 2 1/14/2016 CS 261, WSU Vancouver Primitive Types Notes: 4 A is a constant; B is a variable.
Strings program. C Program to Check if a given String is Palindrome #include void main() { char string[25], reverse_string[25] = {'\0'}; int i, length.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Characters and Strings Functions.
CMSC 104, Version 8/061L25Strings.ppt Strings Topics String Libraries String Operations Sample Program Reading Sections
Chapter 1 Basic C Programming
Sudeshna Sarkar, IIT Kharagpur 1 I/O in C + Misc Lecture –
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.
الف) تابع y = f(x) = X >= 0x -2 < x < 0 x3x3 X
Tutorial #4 Summer 2005.
Programmation impérative - Prof. Béat Hirsbrunner
Character Processing How characters can be treated as small integers?
Control Flow (Chapter 3)
A First Book of ANSI C Fourth Edition
I/O in C + Misc Lecture Sudeshna Sarkar, IIT Kharagpur.
Exercises on String Operations
C programming---basic
CS1100 Computational Engineering
Structured Programming (Top Down Step Refinement)
Variables in C Topics Naming Variables Declaring Variables
Chapter 9 One-Dimensional Arrays
The switch Statement Topics Multiple Selection switch Statement
The switch Statement Topics Multiple Selection switch Statement
CSC215 Homework Homework 03 Due date: Oct 07, 2016.
Your questions from last session
Relational, Logical, and Equality Operators
Homework Any Questions?.
Chapter 8 Character Arrays and Strings
Character Processing How characters can be treated as small integers?
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
Introduction to Computer Organization & Systems
Strings #include <stdio.h>
Characters and Strings Functions
Variables in C Topics Naming Variables Declaring Variables
C Characters and Strings
Programming Language  C Control Flow
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

Kontrola toka izvršenja Vladimir Filipović vladaf@matf.bg.ac.rs

Naredbe i blokovi x = 0; i++; printf (…); { } [ ] if, else, while, for - { }

If - else if (izraz) naredba1 else naredba2 if (n > 0) if (a > b) z = a; else z = b; if (n > 0) { if (a > b) z = a; } else z = b; if (n >= 0) for (i = 0; i < n; i++) if (s[i] > 0) { printf("…"); return i; } else /* POGREŠNO */ printf("error - - n is negative\n"); if (a > b) z = a; else z = b;

Else - if if (izraz) naredba else if (izraz) /* binsearch: find x in v[0] <= v[1] <= ... <= v[n-1] */ int binsearch(int x, int v[ ], int n) { int low, high, mid; low = 0; high = n - 1; while (low <= high) { mid = (low+high)/2; if (x < v[mid]) high = mid - 1; else if (x > v[mid]) low = mid + 1; else /* found match */ return mid; } return -1; /* no match */

Switch switch (izraz) { case konst-izraz: naredbe default: naredbe } #include <stdio.h> main() /* count digits, white space, others */ { int c, i, nwhite, nother, ndigit[10]; nwhite = nother = 0; for (i = 0; i < 10; i++) ndigit[i] = 0; while ((c = getchar()) != EOF) { switch (c) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': ndigit[c-'0']++; break; case ' ': case '\n': case '\t': nwhite++; default: nother++; } printf("digits ="); printf(" %d", ndigit[i]); printf(", white space = %d, other = %d\n", nwhite, nother); return 0; Switch switch (izraz) { case konst-izraz: naredbe default: naredbe }

Petlje - while i for while (izraz) naredba for (izraz1 ; izraz2 ; izraz3) naredba izraz1 ; while (izraz2) { naredba izraz3 ; } for ( ; ; ) { … } while ((c = getchar()) == ' ' || c == '\n' || c == '\t') ; /* preskočiti znake praznog prostora */ for (i = 0; i < n; i++) …

#include <ctype.h> /* atoi: convert s to integer; version 2 */ int atoi(char s[ ]) { int i, n, sign; for (i = 0; isspace(s[i]); i++) /* skip white space */ ; sign = (s[i] == '-') ? -1 : 1; if (s[i] == '+' || s[i] == '-') /* skip sign */ i++; for (n = 0; isdigit(s[i]); i++) n = 10 * n + (s[i] - '0'); return sign * n; }

/* shellsort: sort v[0]...v[n-1] into increasing order */ void shellsort(int v[ ], int n) { int gap, i, j, temp; for (gap = n/2; gap > 0; gap /= 2) for (i = gap; i < n; i++) for (j=i-gap; j>=0 && v[j]>v[j+gap]; j-=gap) { temp = v[j]; v[j] = v[j+gap]; v[j+gap] = temp; } #include <string.h> /* reverse: reverse string s in place */ void reverse(char s[]) { int c, i, j; for (i = 0, j = strlen(s)-1; i < j; i++, j--) { c = s[i]; s[i] = s[j]; s[j] = c; } for (i = 0, j = strlen(s)-1; i < j; i++, j--) c = s[i], s[i] = s[j], s[j] = c;

Petlje - do-while do naredba while (izraz) ; /* itoa: convert n to characters in s */ void itoa(int n, char s[ ]) { int i, sign; if ((sign = n) < 0) /* record sign */ n = -n; /* make n positive */ i = 0; do { /* generate digits in reverse order */ s[i++] = n % 10 + '0'; /* get next digit */ } while ((n /= 10) > 0); /* delete it */ if (sign < 0) s[i++] = '-'; s[i] = '\0'; reverse(s); }

Naredbe break i continue /* trim: remove trailing blanks, tabs, newlines */ int trim(char s[]) { int n; for (n = strlen(s)-1; n >= 0; n--) if (s[n] != ' ' && s[n] != '\t' && s[n] != '\n') break; s[n+1] = '\0'; return n; } for (i = 0; i < n; i++) { if (a[i] < 0) /* preskočiti negativne elemente */ continue; … /* obraditi nenegativne elemente */ }

Naredba goto i oznake for ( … ) for ( … ) { … if (disaster) goto error; } error: clean up the mess for (i = 0; i < n; i++) for (j = 0; j < m; j++) if (a[i] == b[j]) goto found; /* nije pronađen nijedan zajednički element */ … found: /* pronađen jedan: a[i] == b[j] */ found = 0; for (i = 0; i < n && !found; i++) for (j = 0; j < m && !found; j++) if (a[i] == b[j]) found = 1; if (found) /* pronađen jedan: a[i-1] == b[j-1] */ … else /* nije pronađen nijedan zajednički element */