1 Flow of Control Chapter 4 in ABC. 2 Operators and Associativity OperatorAssociativity +(unary) -(unary) ++ -- !right to left * / % left to right + -left.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
BBS514 Structured Programming (Yapısal Programlama)1 Selective Structures.
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement.
1 TDBA66, VT-03, Lecture - Ch. 5 Repetition Loops are common in programming (computers can do boring things over and over again) Type of loopWhen usedC-structures.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
C Programming Lecture 12. The Compound Statement b A compound statement is a series of declarations and statements surrounded by braces. b A compound.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
13&14-2 Know the forms of loop statements in C (while,do/while,for). Understanding how data conversion occurs. Read/Write data in files using Unix redirection.
Conditional Statement
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
C Programming Lecture 11.
Computer programming Lecture 4. Lecture 4: Outline Making Decisions [chap 6 – Kochan] –The if Statement –The if-else Construct –Logical Operators –Boolean.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
1 Flow of Control. 2 Outline Relational, Equality, and Logical Operators Relational Operators and Expressions Equality Operators and Expressions Logical.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
ECE 103 Engineering Programming Chapter 18 Iteration Herbert G. Mayer, PSU CS Status 7/19/2015 Initial content copied verbatim from ECE 103 material developed.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Repetition and Iteration ANSI-C. Repetition We need a control instruction to allows us to execute an statement or a set of statements as many times as.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
CS115 FALL Senem KUMOVA-METİN1 CHAPTER 4 FLOW OF CONTROL-1 Operators, If and switch statements.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Perl Chapter 3 Conditional statements. Control Expressions Control expressions – interpreted as T/F (evaluated as strings or numbers) – simple, relational,
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 2 : August 28 webpage:
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Chapter 4 – C Program Control
CSE 220 – C Programming Loops.
Administrative things
Chapter 4 - Program Control
Lecture 13 & 14.
Flow of Control.
Looping.
Flow of Control.
Chapter 8 JavaScript: Control Statements, Part 2
Chapter 6 Decision Making and Looping
Introduction to C Programming
Flow of Control.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Program Control Topics While loop For loop Switch statement
Relational, Logical, and Equality Operators
Flow of Control.
Computer Programming Techniques Semester 1, 1998
ECE 103 Engineering Programming Chapter 18 Iteration
Chapter 4 - Program Control
CprE 185: Intro to Problem Solving (using C)
Chap 7. Advanced Control Statements in Java
Flow of Control.
CSC215 Lecture Control Flow.
Controlling Program Flow
Chapter 13 Control Structures
Presentation transcript:

1 Flow of Control Chapter 4 in ABC

2 Operators and Associativity OperatorAssociativity +(unary) -(unary) !right to left * / % left to right + -left to right >= left to right == !=left to right &&left to right ||left to right = += -= *= /= etc. right to left A boolean expression evaluates to 0 or 1 (int) There is no type “boolean” in C. Numeric values are used instead: 0  false,  0  true

3 int i = 1, j = 2, k = 3; Declarations and Initializations ExpressionEquivalent ExpressionValue i == jj == i0 i != jj != i1 i + j + k == -2 * -k(( i + j ) + k ) == (( -2 ) * ( -k ))1

4 char c = `A'; int i = 7, j = 7; double x = 0.0, y = 2.3; Declarations and Initializations ExpressionEquivalent ExpressionValue ! c? ! ( i – j )? ! i – j? ! ! ( x + y )? ! x * ! ! y?

5 char c = `A'; int i = 7, j = 7; double x = 0.0, y = 2.3; Declarations and Initializations ExpressionEquivalent ExpressionValue ! c 0 ! ( i – j ) 1 ! i – j( ! i ) – j-7 ! ! ( x + y )! ( ! ( x + y ) )1 ! x * ! ! y( ! x ) * ( ! ( ! y ) )1

6 charc = `B'; inti = 3, j = 3, k = 3; doublex = 0.0, y = 2.3; Declarations and Initializations ExpressionEquivalent ExpressionValue i && j && k( i && j ) && k1 x || i && j – 3x || ( i && ( j – 3 ) )0 i < j && x < y( i < j ) && ( x < y )0 i < j || x < y( i < j ) || ( x < y )1 ‘A’ <= c && c <= ‘Z’( ‘A’ <= c ) && ( c <= ‘Z’ )1 c – 1 == ‘A’ || c + 1 == ‘Z’( ( c – 1 ) == ‘A’ ) || ( ( c + 1 ) == ‘Z’ )1

7 Declarations and Initializations int main(void){ charc = `w‘, c2; inti = 1, j = 2, k = -7; double x = 7e + 33, y = 0.001;... ExpressionEquivalent ExpressionValue ‘a’ + 1 < c? -i – 5 * j >= k + 1? 3 < j < 5? x <= x + y? x < x + y? what is the value of c2? c2 is not initialized (i.e. contains garbage). It is NOT a compilation error to access an uninitialized variable

8 Declarations and Initializations int main(void){ charc = `w‘, c2; inti = 1, j = 2, k = -7; double x = 7e + 33, y = 0.001;... ExpressionEquivalent ExpressionValue ‘a’ + 1 < c(‘a’ + 1 ) < c1 -i – 5 * j >= k + 1(( -i ) – ( 5 * j )) >= ( k + 1 )0 3 < j < 5( 3 < j ) < 51 x <= x + y( x – ) <= ( x + y )1 x < x + yx < ( x + y )0 c2 is not initialized (i.e. contains garbage). It is NOT a compilation error to access an uninitialized variable

9 Short-circuit Evaluation (&&,||) Short circuit evaluation: The evaluation process stops as the outcome “true” / “false” is known. if ( (i != 0) && ( 1/i <....)){... } into cnt = 0; while ( ++cnt <= 3 && (c = getchar()) != EOF ) {. } Keep looping as long as the expression evaluates to a value different than zero Do Something What is the maximum number that getchar() is called? not evaluated if i==0

10 if ( a == 1 ) printf( “***\n” ); else printf( “###\n” ); if ( a == 1 ) printf( “***\n” ); printf( “another command\n” ); The if Statement An if / else statement An if statement, not followed by an else statement

11 if ( a == 1 ) a single command; else a single command; The if Statement Open a block if you like if ( a == 1 ) { some commands… } else { some commands… }

12 Software 1, TAU The if Statement if (a == 1) if (b == 2) printf("***\n"); else printf("###\n"); is equivalent to if (a == 1) if (b == 2) printf("***\n"); else printf("###\n"); Indentation does not influence the else statement

13 Software 1, TAU The if Statement if (a == 1) if (b == 2) printf("***\n"); else printf("###\n"); NOT equivalent to if (a == 1) { if (b == 2) printf("***\n"); } else printf("###\n"); “ Dangling else “ : What about the else???

14 Switch Statement switch ( c ) { case `a': ++a_cnt; break; case `b': ++b_cnt; break; case `c': case `C': ++cC_cnt; break; default: ++other_cnt; } value to be evaluated (integral expression) The cases are constant integral expressions with unique values, indicating labels to jump to (according to the value of c)

15 The conditional operator exp1?exp2:exp3 x = exp1?exp2:exp3; is Equivalent to: if (exp1) x =exp2; elsex = exp3; Example: chara = `a', b = `b'; inti = 1, j = 2; doublex = 7.07; ExpressionEquivalent ExpressionValueType i == j ? a – 1 : b + 1( i == j ) ? ( a – 1 ) : ( b + 1 )99int j % 3 == 0 ? i + 4 : x(( j % 3 ) == 0 ) ? ( i + 4 ) : x7.07double j % 3 ? i + 4 : x( j % 3 ) ? ( i + 4 ) : x5.0double The type is determined by both exp2 and exp3 – irrespective of which is evaluated ‘ a ‘ has a decimal value of 97

16 int i=1, factorial=1; While ( i++ < n ) factorial *= i; While ( ( c = getchar() ) != EOF ) { if ( c >= ‘a’ && c <= ‘z’ ) ++lowercase_letter_cnt; ++total_cnt; } The while Statement Loop until i is equal to, or bigger than n. Loop until the EOF character is encountered.

17 for ( expr1; expr2; expr3) statement next statement expr1; while ( expr2 ) { statement expr3 } next statement The while Statement These are equivalent!!!

18 The for Statement i = 1;sum = 0; for ( ; i <= 10; ++i)for ( ; i <= 10; ) sum += i; sum += i++; i = 1; sum = 0; for ( ; ; ) { sum += i++; printf("%d\n", sum); } Here we sum the numbers from 1 to 10 Here we have an infinite loop!!! sum = 0 for ( i = 1; i <= 10; ++i ) sum += i; The loop stops when the condition (middle statement) evaluates to 0

19 The for Statement for ( sum = 0, i = 1; i <=n; ++i ) sum += i; is equivalent to: for ( sum = 0, i = 1; i <= n; sum += i, ++i ); but is not equivalent to: for ( sum = 0, i = 1; i <= n; ++i, sum += i );

20 The comma Operator Usually used in “for loops” - allows for multiple initializations and multiple processing of indices. Has the lowest precedence of all the operators in C left-to-right Associativity The expression exp1,exp2 as a whole has the value and type of the right operand (i.e. exp2) Example: inti, j, k = 3; doublex = 3.3; ExpressionEquivalent ExpressionValue i = 1, j = 2, ++k + 1(( i = 1 ), ( j = 2 )), (( ++k ) + 1 )5 k != 1, ++x * ( k != 1 ), ((( ++x ) * 2.0 ) + 1 )9.6

21 The do-while Statement int error = 1; do { int, n; printf( “Input a positive integer: ” ); scanf( “%d”, &n ); if (error = (n <= 0) ) printf( “\nERROR; Do it again!\n\n” ); } while (error);

22 The do-while Statement int error = 1; do { int res, n; printf( “Input a positive integer: ” ); res = scanf( “%d”, &n ); if (res != 1){ printf(“\nERROR: failed to read integer number!\n\n”); break; } if (error = (n <= 0) ) printf( “\nERROR; Do it again!\n\n” ); } while (error);

23 The break Command while(1){ while ( 1 ) { scanf( “%1f”, &x ); if ( x < 0.0 ) break; printf( “%f\n”, sqrt( x ) ); } Exit the loop if x is negative break jumps to here!!

24 The continue Command for ( i = 0; i < TOTAL; ++i ) { c = getchar(); if ( c >= `0' && c <= `9' ) continue;. } Jump to the end of the current iteration

25 to here goto and labeling goto error;..... error: { printf( “An error has occurred -- bye!\n” ); exit(1); } while (scanf( “%1f”, &x ) == 1 ) { if (x < 0.0) goto negative_alert; printf( “%f %f\n”, sqrt(x), sqrt(2 *x)); } negative_alert: printf( “Negative value encountered!\n” ); Jump from this line of code to the line with the appropriate label Jump from here

26 Count blanks, digits, letters, newlines and others #include int main( void ) { int blank_cnt = 0, c = 0, digit_cnt = 0, letter_cnt = 0, nl_cnt = 0, other_cnt = 0; while ( (c = getchar()) != EOF ) if ( c == ' ' ) ++blank_cnt; else if ( c >= '0' && c <= '9' ) ++digit_cnt; else if (c >= 'a' && c = 'A' && c <= 'Z') ++letter_cnt; else if (c == '\n') ++nl_cnt; else ++other_cnt; Brackets are not neccessary (but it is advised to use them for readability!!)

27 Example: Count blanks, digits, letters, newlines and others printf( “%10s%10s%10s%10s%10s%10s\n\n”, “blanks”, “digits”, “letters”, “lines”, “others”, “total”); printf( “%10d%10d%10d%10d%10d%10d\n\n”, blank_cnt, digit_cnt, letter_cnt, nl_cnt, other_cnt, blank_cnt + digit_cnt + letter_cnt + nl_cnt + other_cnt ); return 0; } blanksdigitsletterslinesotherstotal

28 Example: Print a table of values for some Boolean functions #include int main( void ) { int b1 = 0, b2 = 0, b3 = 0, b4 = 0, b5 = 0; int cnt = 0; printf( “\n%5s%5s%5s%5s%5s%5s%7s%7s%11s\n\n”, “Cnt”, “b1”, “b2”, “b3”, “b4”, “b5”, “fct1”, “fct2”, “majority” );. Boolean variables Headings

29 Print a table of values for some Boolean functions for ( b1 = 0; b1 <= 1; ++b1 ) for ( b2 = 0; b2 <= 1; ++b2 ) for (b3 = 0; b3 <= 1; ++b3) for (b4 = 0; b4 <= 1; ++b4) for (b5 = 0; b5 <= 1; ++b5) printf( “%5d%5d%5d%5d%5d%5d%6d%7d%9d\n”, ++cnt, b1, b2, b3, b4, b5, b1 || b3 || b5, b1 && b2 || b4 && b5, b1 + b2 + b3 + b4 + b5 >= 3 ); putchar('\n'); return 0; } Cntb1b2b3b4b5fct1fct2majority … The result

30 A Test that Fails #include int main( void ) { int cnt = 0; double sum = 0.0, x = 0.0; for ( x = 0.0; x != 9.9; x += 0.1 ) { sum += x; printf( “cnt = %5d\n”, ++cnt ); } printf( “sum = %f\n”, sum ); return 0; } The test x<9.9 is more robust in this case ==> It is a good programming style to use a relational expression, when appropriate, rather than equality expression. The test x<9.9 is more robust in this case ==> It is a good programming style to use a relational expression, when appropriate, rather than equality expression. Trouble!!! (on most machines goes into an infinite loop)

31 #include #define LIMIT 46 int main(void) { long f0 = 0, f1 = 1, n = 0, temp = 0; printf( “%7s%19s%29s\n%7s%19s%29s\n%7s%19s%29s\n”, “ ”, “Fibonacci”, “Fibonacci”, “ n”, “ number”, “ quotient”, “--”, “ ”, “ ” ); printf( “%7d%19d\n%7d%19d\n”, 0, 0, 1, 1 ); for ( n = 2; n <= LIMIT; ++n ) { temp = f1; f1 += f0; f0 = temp; printf( “%7ld%19ld%29.16f\n”, n, f1, (double) f1 / f0 ); } return 0; } Print Fibonacci Numbers and Quotients Headings The first two cases

32 n Fibonacci number Fibonacci quotient ……… ……… Print Fibonacci Numbers and Quotients