Presentation is loading. Please wait.

Presentation is loading. Please wait.

Branching Constructs Review l what are branching constructs? what type of branching constructs have we studied? l what is nested if? l what is multiway.

Similar presentations


Presentation on theme: "Branching Constructs Review l what are branching constructs? what type of branching constructs have we studied? l what is nested if? l what is multiway."— Presentation transcript:

1 Branching Constructs Review l what are branching constructs? what type of branching constructs have we studied? l what is nested if? l what is multiway if? How does multiway if relate to nested if? l what is a switch statement? is it better than multiway if? l what does break inside switch do? l what is conditional assignment? what construct can be used instead? l what are named constants? why are they needed? l what is a block? what is special about declaring a variable inside a block? what is a scope of a variable? l what is programming idiom? l what is a unary, binary, ternary operator? 1

2 Iterative Constructs while, for, do-while

3 Iterative Constructs l provide n ability to execute the same code multiple times l three constructs n while statement n do-while statement n for statement 3

4 The while Statement l syntax while ( expression ) action l semantics n if expression is true then execute action n repeat this process until expression evaluates to false l action is either a single statement or a block expression action truefalse 4

5 The do - while Statement l syntax do action while ( expression ); l semantics n execute action n if expression is true then execute action again n repeat this process until expression evaluates to false l action is either a single statement or a block action true false expression 5

6 The for Statement l syntax for( init_statement ; expression ; post_statement ) action l semantics n execute init_statement n evaluate expression if true –execute action –execute post_statement –repeat l example for (int i = 0; i < 20; ++i) cout << "i is " << i << endl; expression action true false init_statement post_statement 6

7 Iterate and Keep Track Idiom l what is idiom again? l often need to iterate while keep track of some value across iterations – maximum found, sum, if all positive, etc. l idiom n before loop declare variable to keep track, initialize it n inside loop, update tracking variable, use branching if necessary to examine n after loop, use the tracking variable that accumulated the result n example: cout << "Input number [0 to quit]: "; int max, n; cin >> n; max = n; while (n != 0 ) { cin >> n; if ( n > max) max = n; } cout << ”Maximum number: ” << max << endl; 7

8 Break and Continue with Iterative Constructs break - exits innermost loop int sum=0; while(sum < 100) { int i; cin >> i; if (i< 0) { cout << ”found negative number\n”; break; } sum +=i; } continue - skip the remaining statements and start a new iteration (evaluate expression) int sum=0; for (int i = 0; i > intVar; if(intVar < 0) continue; sum +=i; } avoid break/continue with loops as they make code less readable (avoid regular loop exit) n first try to code loop without them 8

9 Nesting of Iterative Constructs l iterative constructs can be nested: one iterative construct may be inside the body of another l example: for (int i = 0; i < 10; ++i) // outer loop for (int j = 0; j < 10; ++j) // inner loop cout << i << j << endl; what would this code output? note, there is no need for curly brackets l nesting may be more than two loops deep l for/while/do-while can be mixed in nesting l besides nested loops, loop body may contain other code n including branching constructs: a branching construct nested in the loop 9

10 Iteration key points l make sure there is a statement that will eventually nullify the iteration criterion (i.e., the loop must stop) l make sure that initialization of any loop counters or iterators is properly performed l have a clear purpose for the loop n document the purpose of the loop and how the body of the loop advances the purpose of the loop 10


Download ppt "Branching Constructs Review l what are branching constructs? what type of branching constructs have we studied? l what is nested if? l what is multiway."

Similar presentations


Ads by Google