Conditinoal Constructs Review what is a block? what is special about declaring a variable inside a block? what is a scope of a variable? what are conditional constructs? what type of conditional constructs have we studied? what is nested if? what is multiway-if? How does multiway-if relate to nested if? what is a switch statement? is it better than multiway-if? what does break inside switch do? what is conditional operator? conditional assignment? what construct can be used instead? what is programming idiom? what is a unary, binary, ternary operator?
Iterative Constructs while, for, do-while
Iterative Constructs provide ability to execute the same code multiple times three constructs while statement do-while statement for statement
The while Statement expression body true false syntax while (expression) body semantics if expression is true then execute body body is either a single statement or a block iteration: single execution of body iterate until expression evaluates to false example while (n != 0) { cin >> n; if (n > max) max = n; } expression body true false
The do-while Statement while (n != 0 ) { cin >> n; if (n > max) max = n; } The do-while Statement syntax do body while (expression); semantics execute body if expression is true then iterate again iterate until expression evaluates to false example int max=0, n; do { cin >> n; if (n > max) max = n; } while (n == 0); body expression true false
The for Statement initStatement false expression true body postStatement syntax for(initStatement; expression; postStatement) body semantics execute initStatement evaluate expression, if true: iterate iteration: execute body execute postStatement repeat expression evaluation example for (int i = 0; i < 20; ++i) cout << "i is " << i << endl; loop variable - declared inside for its scope is body of the loop modifying loop variable inside body is poor style, use while instead
Iterate and Keep Track Idiom what is idiom again? often need to iterate while keep track of some value across iterations – maximum value found, sum, if all positive, etc. idiom before loop, declare tracking variable to keep track, initialize it what is initialization again? inside loop, update tracking variable, use branching if necessary to examine after loop, use the tracking variable that accumulated the result 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;
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; avoid break with loops as they make code less readable (makes regular loop exit unnecessary): first try to code loop without it continue - skip the remaining statements and start a new iteration (evaluate expression) for (int i = 0; i < 20; ++i) { int intVar; cin >> intVar; if(intVar < 0) continue;
Nesting of Iterative Constructs iterative constructs can be nested: one iterative construct may be inside the body of another 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, no need for curly brackets nesting may be more than two loops deep for/while/do-while can be mixed in nesting besides nested loops, loop body may contain other code including branching constructs: a branching construct nested in the loop
Iteration Key Points make sure there is a statement that will eventually falsify the looping construct expression (i.e., the loop must stop) make sure that all counters and tracking variables are initialized have a clear purpose for the loop good way to know if you do: can write clear comments above loop