Programming in C DaleWeemsHeadington Chapter 9 Additional Control

  • Slides: 26
Download presentation
Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do. . While, For

Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do. . While, For statements) 1

Switch Statement Is a selection control structure for multi-way branching. SYNTAX switch ( Integral.

Switch Statement Is a selection control structure for multi-way branching. SYNTAX switch ( Integral. Expression ) { case Constant 1 : Statement(s); case Constant 2 : Statement(s); . . . default : Statement(s); } // optional 2

float char weight. In. Pounds = 165. 8 ; weight. Unit ; . .

float char weight. In. Pounds = 165. 8 ; weight. Unit ; . . . // user enters letter for desired weight. Unit switch ( weight. Unit ) { } case ‘P’ : case ‘p’ : cout << weight. In. Pounds << “ pounds “ << endl ; break ; case ‘O’ : case ‘o’ : cout << 16. 0 * weight. In. Pounds << “ ounces “ << endl ; break ; case ‘K’ : case ‘k’ : cout << weight. In. Pounds / 2. 2 << “ kilos “ << endl ; break ; case ‘G’ : case ‘g’ : cout << 454. 0 * weight. In. Pounds << “ grams “ << endl ; break ; default : cout << “That unit is not handled! “ << endl ; break ;

Switch Statement l The value of Integral. Expression (of char, short, int, long or

Switch Statement l The value of Integral. Expression (of char, short, int, long or enum type ) determines which branch is executed. l Case labels are constant ( possibly named ) integral expressions. Several case labels can precede a statement. 4

Control in Switch Statement l Control branches to the statement following the case label

Control in Switch Statement l Control branches to the statement following the case label that matches the value of Integral. Expression. Control proceeds through all remaining statements, including the default, unless redirected with break. l If no case label matches the value of Integral. Expression, control branches to the default label, if present. Otherwise control passes to the statement following the entire switch statement. l Forgetting to use break can cause logical errors because after a branch is taken, control proceeds sequentially until either break or the end of the switch statement occurs. 5

Do-While Statement Is a looping control structure in which the loop condition is tested

Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX do { Statement } while ( Expression ) ; Loop body statement can be a single statement or a block. 6

Function Using Do-While void Get. Yes. Or. No ( /* out */ char& response

Function Using Do-While void Get. Yes. Or. No ( /* out */ char& response ) // Inputs a character from the user // Postcondition: // response has been input && response == ‘y’ or ‘n’ { do { cin >> response ; // skips leading whitespace if ( ( response != ‘y’ ) && ( response != ‘n’ ) ) cout << “Please type y or n : “ ; } while ( ( response != ‘y’ ) && ( response != ‘n’ ) ) ; } 7

Do-While Loop vs. While Loop l l l POST-TEST loop (exit -condition) The looping

Do-While Loop vs. While Loop l l l POST-TEST loop (exit -condition) The looping condition is tested after executing the loop body. Loop body is always executed at least once. l l l PRE-TEST loop (entry-condition) The looping condition is tested before executing the loop body. Loop body may not be executed at all. 8

Do-While Loop DO Statement WHILE Expression TRUE FALSE When the expression is tested and

Do-While Loop DO Statement WHILE Expression TRUE FALSE When the expression is tested and found to be false, the loop is exited and control passes to the 9 statement that follows the do-while statement.

A Count-Controlled Loop SYNTAX for ( initialization ; test expression ; update ) {

A Count-Controlled Loop SYNTAX for ( initialization ; test expression ; update ) { 0 or more statements to repeat } 10

The for loop contains an initialization an expression to test for continuing an update

The for loop contains an initialization an expression to test for continuing an update to execute after each iteration of the body 11

Example of Repetition int num; for ( num = 1 ; num <= 3

Example of Repetition int num; for ( num = 1 ; num <= 3 ; num++ ) { cout << num << “Potato” << endl; } 12

The output was: 1 Potato 2 Potato 3 Potato 13

The output was: 1 Potato 2 Potato 3 Potato 13

What happens? l l l num is initialized to 1 expression num <= 3

What happens? l l l num is initialized to 1 expression num <= 3 is evaluated as true body is executed displaying 1 Potato update executed gives num value 2 expression num <= 3 is evaluated as true body is executed displaying 2 Potato update executed gives num value 3 expression num <= 3 is evaluated as true body is executed displaying 3 Potato update executed gives num value 4 expression num <= 3 is evaluated as false so loop ends 14

Count-controlled loop int count ; for ( count = 4 ; count > 0

Count-controlled loop int count ; for ( count = 4 ; count > 0 ; count-- ) { cout << count << endl; } cout << “Done” << endl; OUTPUT: 4 3 2 1 Done 15

What is output? int count; for ( count = 0 ; count < 10

What is output? int count; for ( count = 0 ; count < 10 ; count++ ) { cout << “ ” ; } 16

OUTPUT ***** NOTE: the 10 asterisks are all on one line. Why? 17

OUTPUT ***** NOTE: the 10 asterisks are all on one line. Why? 17

What output from this loop? int count; for (count = 0; count < 10;

What output from this loop? int count; for (count = 0; count < 10; count++) ; { cout << “ ” ; } 18

OUTPUT l l l No output from the for loop! Why? The ; right

OUTPUT l l l No output from the for loop! Why? The ; right after the ( ) means that the body statement is a null statement In general, the Body of the for loop is whatever statement immediately follows the ( ) That statement can be a single statement, a block, or a null statement. Actually, the code outputs one * after the loop completes its counting to 10. 19

Several Statements in Body Block const int MONTHS = 12 ; int count ;

Several Statements in Body Block const int MONTHS = 12 ; int count ; float bill ; float sum = 0. 0 ; for (count = 1; count <= MONTHS; count++ ) { cout << “Enter bill: “ ; cin >> bill ; sum = sum + bill ; } cout << “Your total bill is : “ << sum << endl ; 20

Break Statement l Break statement can be used with Switch or any of the

Break Statement l Break statement can be used with Switch or any of the 3 looping structures. l It causes an immediate exit from the Switch, While, Do-While, or For statement in which it appears. l If the break is inside nested structures, control exits only the innermost structure containing it. 21

Continue Statement l Is valid only within loops. l Terminates the current loop iteration,

Continue Statement l Is valid only within loops. l Terminates the current loop iteration, but not the entire loop. l In a For or While, continue causes the rest of the body statement to be skipped. In a For statement, the update is done. l In a Do-While, the exit condition is tested, and if true, the next loop iteration is begun. 22

Imagine using. . . l a character, a length, and a width to draw

Imagine using. . . l a character, a length, and a width to draw a box. For example, l using the values ‘&’, 4, and 6 would display &&&&&& 23

Write prototype for void function called Draw. Box ( ) with 3 parameters. The

Write prototype for void function called Draw. Box ( ) with 3 parameters. The first is type char, the other 2 are type int. void Draw. Box( char, int ); NOTE: Some C++ books include identifiers in prototypes. Any valid C++ identifiers, as long as each is different, can be used. void Draw. Box( char letter, int Num 1, int Num 2); 24

void Draw. Box(char What, int Down, int Across) // 3 formal parameters { int

void Draw. Box(char What, int Down, int Across) // 3 formal parameters { int row, col; // 2 local variables for ( row = 0; row < Down; row++ ) { for (col = 0; col < Across; col++ ) { cout << What; } cout << endl; } return; }

THE DRIVER PROGRAM #include <iostream. h> void Draw. Box (char, int); // prototype int

THE DRIVER PROGRAM #include <iostream. h> void Draw. Box (char, int); // prototype int main (void) { char letter = ‘&’; Draw. Box(letter, 4, 2*3); Draw. Box(‘V’, 9, 3); return 0; // actual parameters // appear in call } 26