Programming in C++ A Comparison of If and While IF-THEN SRATEMENT WHILE STATEMENT If (Expression) While(Expression falsetrue false true Statement1 Statement1 Statement2 Statement2
6 A Comparison of If and While If (Expression) Statement1 Statement2 false true While (Expression) Statement1 Statement2 false true IF-THEN SRATEMENT WHILE STATEMENT
Programming in C++ Phases of Loop EXecution 心 Loop entry Iteration 心 Loop test 心 Loop exit o Termination
7 Phases of Loop Execution ❖Loop entry ❖Iteration ❖Loop test ❖Loop exit ❖Termination
Programming in C++ Two Types of Loops count controlled loops repeat a specIfied number of times event-controlled loops some condition within the loop body changes and this causes the repeating to stop
8 Two Types of Loops count controlled loops repeat a specified number of times event-controlled loops some condition within the loop body changes and this causes the repeating to stop
Programming in C++ Count-controlled loop contains o an initialization of the loop control variable an expression to test for continuing the loop o an update of the loop control variable to be executed with each iteration of the body
9 ❖an initialization of the loop control variable ❖an expression to test for continuing the loop ❖an update of the loop control variable to be executed with each iteration of the body Count-controlled loop contains
Programming in C++ Count-controlled Loop int count count 4 m/initialize loop variable while(count>0) ∥ test expression cout≤< count≤≤end;∥ repeated action count update loop variable cout≤<“Done”<<endl; 10
10 int count ; count = 4; // initialize loop variable while (count > 0) // test expression { cout << count << endl ; // repeated action count -- ; // update loop variable } cout << “Done” << endl ; Count-controlled Loop