Presentation is loading. Please wait.

Presentation is loading. Please wait.

Branching Constructs Review

Similar presentations


Presentation on theme: "Branching Constructs Review"— Presentation transcript:

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

2 Iterative Constructs while, for, do-while 2

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

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

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

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

7 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) for (int i = 0; i < 20; ++i) { int intVar; cin >> intVar; if(intVar < 0) continue;

8 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, there is 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

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


Download ppt "Branching Constructs Review"

Similar presentations


Ads by Google