Programming in c++ Looping Dale/eems/Headington
1 Looping
Programming in C++ Chapter 6 Topics While Statement Syntax o Phases of Loop Execution Two Types of Loops: Count-Controlled Loops &Event-Controlled Loops s Using the End-of-File Condition to Control Input Data s Using a While Statement for Summing and ounting How to Design Loops %Nested While Loops s Loop Testing and debugging
2 Chapter 6 Topics ❖While Statement Syntax ❖Phases of Loop Execution ❖Two Types of Loops: Count-Controlled Loops &Event-Controlled Loops ❖Using the End-of-File Condition to Control Input Data ❖Using a While Statement for Summing and Counting ❖How to Design Loops ❖Nested While Loops ❖Loop Testing and Debugging
Programming in C++ What is a loop? %A loop is a repetition control structure it causes a single statement or block to be executed repeatedly
3 ❖A loop is a repetition control structure. ❖it causes a single statement or block to be executed repeatedly What is a loop?
Programming in C++ While Statement SYNTAX while( Expression ∥ oop body NOTE: Loop body can be a single statement, a null statement, or a block
4 While Statement SYNTAX while ( Expression ) { . . // loop body . } NOTE: Loop body can be a single statement, a null statement, or a block
Programming in C++ When the expression is tested and found to be false, the loop is exited and control passes to the statement which follows the loop body. WHILE LOOP FALSE Expression TRUE body statement
5 When the expression is tested and found to be false, the loop is exited and control passes to the statement which follows the loop body. WHILE LOOP FALSE TRUE body statement Expression
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