ECE 246 (INTRODUCTION TO C PROGRAMMING

)
CHAPTER 3: CONTROL FLOW

Learning Outcomes

Able to understand the assignment variations Able to understand the usage selection statements/structures Able to understand the usage of repetition statements/structures Able to understand the usage of continue/break

Lesson Outline

 

Assignment variations Selection statements: if, if else, switch Repetition statements: for, while, do while Break/continue

 Before writing a program:   Have a through understanding of the problem Carefully plan an approach for solving it Know what “building blocks” are available Use good programming principles  While writing a program:   .Let’s Recap….

Let’s Recap…    Computing problems  All can be solved by executing a series of actions in a specific order Algorithm: procedure in terms of  Actions to be executed  The order in which these actions are to be executed Program control  Specify order in which statements are to be executed .

Let’s Recap…  Pseudocode     Artificial. informal language that helps us develop algorithms Similar to everyday English Not actually executed on computers Helps us “think out” a program before writing it   Easy to convert into a corresponding C++ program Consists only of executable statements .

using the addition assignment operator  Statements of the form variable = variable operator expression. can be rewritten as variable operator= expression. can be abbreviated as c += 3.  Examples of other assignment operators: d e f g -= *= /= %= 4 5 3 9 (d (e (f (g = = = = d e f g * / % 4) 5) 3) 9) .More Assignment Operators  Assignment operators abbreviate assignment expressions c = c + 3.

8 Assignment Sample Explanation operator expression Assume: int c = 3. f = 6. . += -= *= /= %= c += 7 d -= 4 e *= 5 f /= 3 g %= 9 C = c + 7 D = d . d = 5. g = 12. e = 4.4 E = e * 5 F = f / 3 G = g % 9 Assigns 10 to c 1 to d 20 to e 2 to f 3 to g More Arithmetic assignment operators.

Increment and Decrement Operators  Increment operator (++)  Can be used instead of c+=1 Can be used instead of c-=1 Operator is used before the variable (++c or --c) Variable is changed before the expression it is in is evaluated  Decrement operator (--)   Preincrement    Postincrement   Operator is used after the variable (c++ or c--) Expression executes before the variable is changed .

 When variable not in an expression   Has the same effect as c++. ++c ). printf( “%d”. c now has the value of 6 Preincrementing and postincrementing have the same effect ++c. c++ ).Increment and Decrement Operators  If c equals 5.   Prints 6 printf( "%d". then printf( "%d". printf( “%d”.  Prints 5 In either case. c ). . c ).

then use the new value of b in the expression in which b resides. Increment and decrement operators.11 Operator ++ ++ --- Sample expression Explanation ++a a++ --b b-Increment a by 1. then increment a by 1. . then use the new value of a in the expression in which a resides. Use the current value of b in the expression in which b resides. Decrement b by 1. then decrement b by 1. Use the current value of a in the expression in which a resides.

print statement is ignored and the program goes onto the next statement  Indenting makes programs easier to read  C ignores whitespace characters .The if selection statement   Selection structure:  Used to choose among alternative courses of action  Pseudocode: If student’s grade is greater than or equal to 60 Print “Passed” If condition true  Print statement executed and program goes on to next statement  If false.

follow appropriate path  Diamond symbol (decision symbol)    .  C code corresponds closely to the pseudocode Indicates decision is to be made Contains an expression that can be true or false Test the condition.The if selection statement  Pseudocode statement in C: if ( grade >= 60 ) printf( "Passed\n" ).

Flowcharting the single-selection if statement. .

The if…else selection statement  if  Only performs an action if the condition is true Specifies an action to be performed both when the condition is true and when it is false If student’s grade is greater than or equal to 60 Print “Passed” else Print “Failed”  if…else   Psuedocode:  Note spacing/indentation conventions .

scanf("%d\n %f\n". return 0. printf("End of program output"). int id_num.&id_num. } . printf("Please type in the car number and mileage:\n").\n".id_num).&miles). if (miles > limit) printf("Car %d is over the limit.0. float miles.If statement Exercise: Expected Output /* demonstrates the usage of if */ #include <stdio.h> int main() { const float limit = 3000.

else printf( "Failed\n").The if…else selection statement  Sample of C code: if ( grade >= 60 ) printf( "Passed\n"). .

.. .else statement.Flowcharting the double-selection if.

} .\n”). else printf(“The area of this circle is %f“.(3. if (radius < 0. printf("Please type in the radius\n").0) printf(“A negative radius is invalid.&radius). return 0.If else statement Exercise: Expected Output /* demonstrates the usage of if else*/ #include <stdio.h> int main() { float radius.14*radius*radius)). scanf(“%f\n".

rest of statements skipped .The if…else selection statement  Nested if…else statements   Test for multiple cases by placing if…else selection statements inside if…else selection statement Once condition is met.

The if…else selection statement  Pseudocode for a nested if…else statement If student’s grade is greater than or equal to 90 Print “A” else If student’s grade is greater than or equal to 80 Print “B” else If student’s grade is greater than or equal to 70 Print “C” else If student’s grade is greater than or equal to 60 Print “D” else Print “F” .

would be executed automatically .\n" ). printf( "You must take this course again.The if…else selection statement  Compound statement:   Set of statements within a pair of braces Example: if ( grade >= 60 ) printf( "Passed.\n" ).\n" ).\n" ). }  Without the braces. the statement  printf( "You must take this course again. else { printf( "Failed.

scanf("%c\n".\n"). else if (code == 'M') printf("The item is military grade. if (code == 'S') printf("The item is space exploration grade.\n"). } . return 0. printf("Enter a specification code\n").h> int main() { char code. else printf("An invalid code was entered.\n").&code). else if (code == 'T') printf("The item is toy grade.Nested If else statement Exercise1: Expected Output /* demonstrates the usage of nested if else*/ #include <stdio.\n"). else if (code == 'C') printf("The item is commercial grade.\n").

printf("Enter the input weight (kg)\n"). float inkg. if (inkg >= 90) digout = 1111.Nested If else statement Exercise2: Expected Output /* demonstrates the usage of nested if else*/ #include <stdio. else if (inkg >= 70) digout = 1101. return 0.&inkg). } .digout). scanf("%f\n". else if (inkg >= 80) digout = 1110. printf("The digital output is %d \n".h> int main() { int digout. else digout = 1011. else if (inkg >= 60) digout = 1100.

exits from statement .switch Multiple-Selection Statement  switch  Useful when a variable or expression is tested for all the values it can assume and different actions are taken Series of case labels and an optional default case switch ( value ){  case '1': actions  case '2': actions  default: actions  }  Format   break.

switch multiple-selection statement with breaks.

Nested If else statement Exercise2: Expected Output
/* demonstrates the usage of switch*/ #include <stdio.h> int main() {

int opselect; float fnum, snum;
printf("Please type in two numbers\n"); scanf("%f\n%f\n",&fnum,&snum); printf("Enter a select code:\n"); printf("1 For addition:\n2 For multiplication:\n3 For division:\n"); scanf("%d\n",&opselect); switch (opselect) { case 1: printf("The sum of the numbers entered is %f", fnum+snum); break; case 2: printf("The product of the numbers entered is %f", fnum*snum); break; case 3: printf("The first number divided by the second is %f", (fnum/snum)); break; } return 0; }

Good Programming Practice

Provide a default case in switch statements. Cases not explicitly tested in a switch are ignored. The default case helps prevent this by focusing the programmer on the need to process exceptional conditions. There are situations in which no default processing is needed.

. it is considered good programming practice to place the default clause last.Good Programming Practice  Although the case clauses and the default case clause in a switch statement can occur in any order.

Good Programming Practice  In a switch statement when the default clause is listed last. But some programmers include this break for clarity and symmetry with other cases. the break statement is not required. .

Repetition Statement Essentials    Loop  Group of instructions computer executes repeatedly while some condition remains true Counter-controlled repetition  Definite repetition: know how many times loop will execute  Control variable used to count repetitions Sentinel-controlled repetition  Indefinite repetition  Used when number of repetitions not known  Sentinel value indicates "end of data" .

e.while  Counter-controlled repetition requires     The name of a control variable (or loop counter) The initial value of the control variable An increment (or decrement) by which the control variable is modified each time through the loop A condition that tests for the final value of the control variable (i.. whether looping should continue) .Counter-Controlled Repetition .

counter ).while  Example: int counter = 1.     Names counter Defines it to be an integer Reserves space for it in memory Sets it to an initial value of 1 . // increment }  The statement int counter = 1.Counter-Controlled Repetition . ++counter. // initialization while ( counter <= 10 ) { // repetition condition printf( "%d\n".

1: fig04_01. 4.c Counter-controlled repetition */ #include <stdio. /* initialization */ Definition and assignment are performed simultaneously while ( counter <= 10 ) { /* repetition condition */ printf ( "%d\n". /* indicate program ended successfully */ 16 17 } /* end function main */ 1 2 3 4 5 6 7 8 9 10 . /* display counter */ ++counter. /* increment */ } /* end while */ return 0. counter ).h> /* function main begins program execution */ int main( void ) { int counter = 1.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 /* Fig.

.Counter-Controlled Repetition  Condensed code   C Programmers would make the program more concise Initialize counter to 0  while ( ++counter <= 10 ) printf( “%d\n. counter ).

The combination of vertical spacing before and after control statements and indentation of the bodies of control statements within the control-statement headers gives programs a two-dimensional appearance that greatly improves program readability. As a general rule.Good Programming Practices      Control counting loops with integer values. try to avoid using more than three levels of nesting. Indent the statements in the body of each control statement. Too many levels of nesting can make a program difficult to understand. . Put a blank line before and after each control statement to make it stand out in a program.

for statement header components. .

counter is incremented by 1. /* define counter */ /* initialization.2: fig04_02. and increment are all included in the for statement header. */ for ( counter = 1. counter <= 10. 4.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 /* Fig. counter ). repetition condition. } /* end for */ return 0. counter++ ) { printf( "%d\n". /* indicate program ended successfully */ 18 } /* end function main */ for loop begins by setting counter to 1 and repeats while counter <= 10. Each time the end of the loop is reached.h> /* function main begins program execution */ int main( void ) { int counter. .c Counter-controlled repetition with the for statement */ #include <stdio.

. the loop-continuation condition should be counter <= 10 rather than counter < 11 or counter < 10.Error-Prevention Tip  Using the final value in the condition of a while or for statement and using the <= relational operator will help avoid off-by-one errors. for example. For a loop used to print the values 1 to 10.

counter <= 10.for Repetition Statement  Format when using for loops for ( initialization. loopContinuationTest.  Prints the integers from one to ten . counter++ ) printf( "%d\n". increment ) statement  Example: for( int counter = 1. counter ).

j++. while ( loopContinuationTest ) { statement.for Repetition Statement   For loops can usually be rewritten as while loops: initialization. j + i ). j + i <= 10. j = 0. . } Initialization and increment  Can be comma-separated lists  Example: for (int i = 0. increment. i++) printf( "%d\n".

Common Programming Errors   Using commas instead of semicolons in a for header is a syntax error. . This is normally a logic error. Placing a semicolon immediately to the right of a for header makes the body of that for statement an empty statement.

j <= 80. j += 5 ) Notes about the for statement:  "Increment" may be negative (decrement)  If the loop continuation condition is initially false  The body of the for statement is not performed  Control proceeds with the next statement after the for statement  Control variable  Often printed or used inside for body. loop-continuation. and increment can contain arithmetic expressions.for Statement : Notes and Observations   Arithmetic expressions  Initialization. j <= 4 * x * y. but not necessary . j += y / x ) is equivalent to for ( j = 2. If x equals 2 and y equals 10 for ( j = x.

. this can lead to subtle errors. It is best not to change it.Error-Prevention Tip  Although the value of the control variable can be changed in the body of a for loop.

.Flowcharting a typical for repetition statement.

/* indicate program ended successfully */ } /* end function main */ . counter += 2 ) printf ( "%d\n". counter <=20.h> /* function main begins program execution */ int main( void ) { int counter. counter ). /* declaration */ /* for statement header */ for ( counter = 2. /* display counter */ return 0.Exercise: Expected Output /* by using for */ #include <stdio.

Good Programming Practices   Although statements preceding a for and statements in the body of a for can often be merged into the for header. Limit the size of control-statement headers to a single line if possible. avoid doing so because it makes the program more difficult to read. .

num*num. num++ ) /* for statement header */ printf ( "%d\t%d\t%d\n". num <=MAXNUM. /* declaration of constant value*/ int num./* by using for */ #include <stdio. num. printf("NUMBER\tSQUARE\tCUBE\n------\t------\t----\n"). /* indicate program ended successfully */ } /* end function main */ . num*num*num ). for ( num = 1.h> /* function main begins program execution */ int main( void ) { const int MAXNUM = 10. /* display*/ return 0.

Error-Prevention Tip  Do not use variables of type float or double to perform monetary calculations. The impreciseness of floating-point numbers can cause errors that will result in incorrect monetary values. .

. } while ( condition ).do…while Repetition Statement  The do…while repetition statement   Similar to the while structure Condition for repetition only tested after the body of the loop is performed  All actions are performed at least once do {  Format: statement.

counter ).  Prints the integers from 1 to 10 .do…while Repetition Statement  Example (letting counter = 1): do { printf( "%d ". } while (++counter <= 10).

Good Programming Practice  Some programmers always include braces in a do... ... This helps eliminate ambiguity between the do.while statement containing one statement and the while statement.while statement even if the braces are not necessary.

In a sentinel-controlled loop. make sure the sentinel value is eventually input. for or do. In a countercontrolled loop. make sure there is not a semicolon immediately after the header of a while or for statement. . To prevent this.while statement never becomes false...Common Programming Error  Infinite loops are caused when the loopcontinuation condition in a while. make sure the control variable is incremented (or decremented) in the loop.

/* display counter */ /* end do.h> /* function main begins program execution */ int main( void ) { int counter = 1. 4.1 2 3 4 5 6 7 8 9 10 11 12 13 14 /* Fig. /* initialize counter */ do { printf( "%d "..c Using the do/while repetition statement */ #include <stdio..while */ } while ( ++counter <= 10 ).9: fig04_09. /* indicate program ended successfully */ 15 16 } /* end function main */ 1 2 3 4 5 6 7 8 9 10 increments counter then checks if it is less than or equal to 10 . return 0. counter ).

while repetition statement...Flowcharting the do. .

break and continue Statements  break    Causes immediate exit from a while. for. do…while or switch statement Program execution continues with the first statement after the structure Common uses of the break statement   Escape early from a loop Skip the remainder of a switch statement .

/* counter */ /* loop 10 times */ for ( x = 1. /* display value of x */ } /* end for */ printf( "\nBroke out of loop at x == %d\n". x <= 10. /* indicate program ended successfully */ 24 25 } /* end function main */ 1 2 3 4 Broke out of loop at x == 5 . x++ ) { /* if x is 5.11: fig04_11.h> /* function main begins program execution */ int main( void ) { int x. 4.c Using the break statement in a for statement */ #include <stdio. break immediately ends for loop 23 return 0. x ). x ). /* break loop only if x is 5 */ } /* end if */ printf( "%d ". terminate loop */ if ( x == 5 ) { break.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 /* Fig.

then the loopcontinuation test is evaluated . for or do…while statement  Proceeds with the next iteration of the loop   while and do…while  Loop-continuation test is evaluated immediately after the continue statement is executed for  Increment expression is executed.break and continue Statements  continue  Skips the remaining statements in the body of a while.

12: fig04_12.h> /* function main begins program execution */ int main( void ) { int x. x <= 10.c Using the continue statement in a for statement */ #include <stdio. 4. return 0.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 /* Fig. /* skip remaining code in loop body */ } /* end if */ printf( "%d ". x++ ) { /* if x is 5. /* indicate program ended successfully */ continue skips to end of for loop and performs next iteration 24 25 } /* end function main */ 1 2 3 4 6 7 8 9 10 Used continue to skip printing the value 5 . x ). continue with next iteration of loop */ if ( x == 5 ) { continue. /* counter */ /* loop 10 times */ for ( x = 1. /* display value of x */ } /* end for */ printf( "\nUsed continue to skip printing the value 5\n" ).

has one operand  Useful as conditions in loops Expression true && false true || false !false Result false true true . logical negation )  Reverses the truth/falsity of its condition  Unary operator.Logical Operators    && ( logical AND )  Returns true if both conditions are true || ( logical OR )  Returns true if either of its conditions are true ! ( logical NOT.

Truth table for the && (logical AND) operator. expression1 expression2 expression1 && expression2 0 0 nonzero nonzero 0 nonzero 0 nonzero 0 0 0 1 .

expression1 expression2 expression1 || expression2 0 0 nonzero nonzero 0 nonzero 0 nonzero 0 1 1 1 .Truth table for the logical OR (||) operator.

expression 0 nonzero !expression 1 0 .Truth table for operator ! (logical negation).

Performance Tip  In expressions using operator &&. In expressions using operator ||. . This can reduce a program’s execution time. make the condition that is most likely to be true the leftmost condition. make the condition that is most likely to be false the leftmost condition.

Operators ++ (postfix) + * + < == && || ?: = .Operator precedence and associativity.(prefix) (type) Associativity right to left right to left left to right left to right left to right left to right left to right left to right right to left right to left left to right Type postfix unary multiplicative additive relational equality logical AND logical OR conditional assignment comma .(postfix) ++ (prefix) -. += -= *= /= %= / <= != > >= ! % -.

 Checks payCode. zero values are false Example using ==: if ( payCode == 4 ) printf( "You get a bonus!\n" ).Confusing Equality (==) and Assignment (=) Operators  Dangerous error     Does not ordinarily cause syntax errors Any expression that produces a value can be used in control structures Nonzero values are true. if it is 4 then a bonus is awarded .

  This sets payCode to 4 4 is nonzero. and bonus awarded no matter what the payCode was  Logic error.Confusing Equality (==) and Assignment (=) Operators  Example. so expression is true. not a syntax error . replacing == with =: if ( payCode = 4 ) printf( "You get a bonus!\n" ).

 lvalues can be used as rvalues. such as variable names  x = 4.Confusing Equality (==) and Assignment (=) Operators   lvalues  Expressions that can appear on the left side of an equation  Their values can be changed. rvalues  Expressions that can only appear on the right side of an equation  Constants. . such as numbers  Cannot write 4 = x.  Must write x = 4. but not vice versa  y = x.

if…else or switch  Repetition – while. and any repetition can be rewritten as a while statement . do…while or for  Any selection can be rewritten as an if statement.Structured Programming Summary  All programs can be broken down into 3 controls  Sequence – handled automatically by compiler  Selection – if.

and an increment of 5 . and an increment of 1 Use a counter named icount that has an initial value of 1. a final value of 20. and an increment of 2 Use a counter named j that has an initial value of 1. a final value of 20.Review Exercises Write individual for statements for the following cases:    Use a counter named i that has an initial value of 1. a final value of 100.

2. and an increment of -1 Use a counter named icount that has an initial value of 20. a final value of 1.Review Exercises continue… Write individual for statements for the following cases:    Use a counter named icount that has an initial value of 20. a final value of 1.0. and an increment of -2 Use a counter named count that has an initial value of 1. and an increment of 0.2 . a final value of 16.

determine the number of times that each for loop is executed for the previous for statements.Review Exercises continue… Finally. .

M. & Deitel. C How To Program. P.J. H.M.References   Burgess. C Programming Tutorial (K&R version 4) Deitel. Pearson International Edition SEE YOU NEXT LESSON!  . (2007). (1999).