chapter 6 - repetition
DESCRIPTION
Chapter 6 - Repetition. while Loop. Simplest loop Two parts: test expression and loop body Pre-tested loop Execute loop body if test true Bypass loop body if test false. Logical expression (variable or arithmetic expression) Boolean results (True or False). General Structure. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Chapter 6 - Repetition](https://reader035.vdocuments.net/reader035/viewer/2022062309/56815864550346895dc5c302/html5/thumbnails/1.jpg)
Chapter 6 - Repetition
![Page 2: Chapter 6 - Repetition](https://reader035.vdocuments.net/reader035/viewer/2022062309/56815864550346895dc5c302/html5/thumbnails/2.jpg)
while Loop
Simplest loop Two parts: test expression and loop body Pre-tested loop
– Execute loop body if test true– Bypass loop body if test false
![Page 3: Chapter 6 - Repetition](https://reader035.vdocuments.net/reader035/viewer/2022062309/56815864550346895dc5c302/html5/thumbnails/3.jpg)
General Structure
while (expression) { statement1 statement2 … }
Logical expression (variable or arithmetic expression)Boolean results (True or False)
![Page 4: Chapter 6 - Repetition](https://reader035.vdocuments.net/reader035/viewer/2022062309/56815864550346895dc5c302/html5/thumbnails/4.jpg)
Steps In Using a while Loop
1. Initialize variable acting as test expression 2. Evaluate the boolean test 3. If true execute statements within loop 4. Update the variable 5. Go back to step 2
Note: if no change to variable then infinite loop (never ends) is created
![Page 5: Chapter 6 - Repetition](https://reader035.vdocuments.net/reader035/viewer/2022062309/56815864550346895dc5c302/html5/thumbnails/5.jpg)
Example while Loop
int i= 0, number = 1;while (number) { cout << “Please type a number. ”
<< “Type 0 (zero) to stop execution.\n”; cin >> number; i++; if (i > 50) break; }
Initialize variables
Variable as expressionvalue other than zero tests true
Loop body
Statements that provide exit from loop
![Page 6: Chapter 6 - Repetition](https://reader035.vdocuments.net/reader035/viewer/2022062309/56815864550346895dc5c302/html5/thumbnails/6.jpg)
Example while Loop
int i= 0;while (i <= 5) { cout << “Loop number is “ << i; i++; }
Initialize test expression variable
Expression evaluated, whentrue, statements in loop executed
Statements within loop
Changing variable to provide exit from loop
![Page 7: Chapter 6 - Repetition](https://reader035.vdocuments.net/reader035/viewer/2022062309/56815864550346895dc5c302/html5/thumbnails/7.jpg)
do-while Loop
Used when you want to execute loop at least once
Loops on test being true and exits when test is false
![Page 8: Chapter 6 - Repetition](https://reader035.vdocuments.net/reader035/viewer/2022062309/56815864550346895dc5c302/html5/thumbnails/8.jpg)
General Syntax
do
{ statement1;
statement2;
… } while (expression);
![Page 9: Chapter 6 - Repetition](https://reader035.vdocuments.net/reader035/viewer/2022062309/56815864550346895dc5c302/html5/thumbnails/9.jpg)
do while Loop Example
Bare bones Does not alert user of problems
do { cout << “\n enter an id number:”; cin >> id_num; } while ((id_num < 100) || (id_num>1999));
![Page 10: Chapter 6 - Repetition](https://reader035.vdocuments.net/reader035/viewer/2022062309/56815864550346895dc5c302/html5/thumbnails/10.jpg)
Better do while Loop do { cout << “\n Enter an id number:”; cin >> id_num; if ((id_num < 100) || (id_num > 1999)) { cout << “\n invalid number entered” << “\n please check and re-enter”; } else break; } while (1);
Decision Statement – Chapter 5
Expression always trueExit loop if id number is valid
![Page 11: Chapter 6 - Repetition](https://reader035.vdocuments.net/reader035/viewer/2022062309/56815864550346895dc5c302/html5/thumbnails/11.jpg)
Recap while(s) while
– Most commonly used when repetition not counter controlled
– Pre-tested– Loop body may not be executed
do-while– Convenient when at least one repetition needed– Post-tested
![Page 12: Chapter 6 - Repetition](https://reader035.vdocuments.net/reader035/viewer/2022062309/56815864550346895dc5c302/html5/thumbnails/12.jpg)
For Loop
Convenient for counter controlled loops Pre-tested loop Same behavior as while
– Set lcv to initial value– Test lcv against terminating value– Update lcv to next value
All done in for header statement
![Page 13: Chapter 6 - Repetition](https://reader035.vdocuments.net/reader035/viewer/2022062309/56815864550346895dc5c302/html5/thumbnails/13.jpg)
General Syntax
for (initialize lcv; test lcv; update lcv) {
statement; statement; }
![Page 14: Chapter 6 - Repetition](https://reader035.vdocuments.net/reader035/viewer/2022062309/56815864550346895dc5c302/html5/thumbnails/14.jpg)
for Loop (cont.) Example: for (int i = 0; i < 10; i++)
cout << i; Initialization, testing and updating in same
statement Semicolons required Braces optional if only one statement
![Page 15: Chapter 6 - Repetition](https://reader035.vdocuments.net/reader035/viewer/2022062309/56815864550346895dc5c302/html5/thumbnails/15.jpg)
Example: int finalValue; cin >> finalValue; for (int counter = 0; counter < finalValue; counter++) cout << "*";
Display Screen
3finalValue
3counter
0
True
*1
True
*2
True
*3
False
![Page 16: Chapter 6 - Repetition](https://reader035.vdocuments.net/reader035/viewer/2022062309/56815864550346895dc5c302/html5/thumbnails/16.jpg)
For Loop Examples
float sum = 0; for (float x = 0.5; x < 20.1; x += 0.5)
sum += sqrt (x);
for (int count = 0; count < n; count++)cout << ch;
![Page 17: Chapter 6 - Repetition](https://reader035.vdocuments.net/reader035/viewer/2022062309/56815864550346895dc5c302/html5/thumbnails/17.jpg)
Comparison: int finalvalue; cin >> finalValue; for (int counter = 0; counter < finalValue; counter++) cout << "*";
counter = 0; cin >> finalValue; while (counter < finalValue) { cout << "*"; counter++;}
int counter, finalValue;int finalvalue;cin >> finalValue;for (int counter = 0;counter < finalValue;counter ++)cout << "*";
![Page 18: Chapter 6 - Repetition](https://reader035.vdocuments.net/reader035/viewer/2022062309/56815864550346895dc5c302/html5/thumbnails/18.jpg)
Debugging and Testing
Off by one errors– Check loops to ensure you are executing the
correct number of times– x < 10 or x <= 10
Check loop boundaries– Can create infinite loop
Does answer make sense
![Page 19: Chapter 6 - Repetition](https://reader035.vdocuments.net/reader035/viewer/2022062309/56815864550346895dc5c302/html5/thumbnails/19.jpg)
For Loop Modifications
break statement– Can use within loop to terminate early– Controversial
Multiple expressions for initialization and increment– Use comma separated list– for (I = 1, j = 2; I < 10; I++, j++)
![Page 20: Chapter 6 - Repetition](https://reader035.vdocuments.net/reader035/viewer/2022062309/56815864550346895dc5c302/html5/thumbnails/20.jpg)
Summary
Create while loops Create do-while loops Create for loops Trace and debug loops Use loops to solve problems
Learned how to: