Download presentation
Presentation is loading. Please wait.
1
Chapter 6 - Repetition
2
Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition in control flow. u In C++ repetition in execution can be realized using a “while”, “do-while” and a “for” statement.
3
Topics for discussion u Repetition structure (loop) design u while loop : syntax, semantics, example u Loop control u do-while : syntax, semantics, example u for loop : syntax, semantics, example u Summary
4
while loop : syntax while ( condition ) statement u “condition” is a logical expression that evaluates to true or false. It could be a relational or Boolean expression. u “statement” could a single statement or more than one statement bounded by { }.
5
while loop : semantics 1) The condition is evaluated. 2) If it is true, then the body of the loop is executed. Then the control is transferred back to the condition for re-evaluation. 3) If the logical expression is false, the while loop is exited and control is transferred to the statement after the while statement.
6
The while statement while (condition) statement; while (condition) { statement block } yes no
7
Loop Design u Loop design should consider: –Initialization of conditions (sum = 0) –Termination of the loop (when n == 0) –Testing (at the top or bottom of the loop) t (at the top, n > 0) –Updating conditions t ( n = n -1) –Of course, the body of the loop. t (sum = sum + n; n = n -1;) u Body of the loop: Statements representing the process to be repeated. These are statements within the scope of a loop.
8
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 expression value other than zero tests true Loop body Statements that provide exit from loop Lesson 6.2
9
Example while Loop int i= 0; while (i <= 5) { cout << “Loop number is “ << i; i++; } Initialize test expression variable Expression evaluated, when true, statements in loop executed Statements within loop Changing variable to provide exit from loop Lesson 6.2
10
Example u Write statements that takes as input n, the side of a square and print and square of size n with asterisks. //PRE: n is a value between 2 and 20 //Output is for n=3 ***
11
Do-while - syntax u Use this control structure: u When a loop needs to be executed at least once. u When the testing of the conditions needs to be done at the bottom. do statement while ( condition );
12
do-while semantics 1) Execute the statement. 2) Evaluate the expression. If it is TRUE then proceed to step 1) else exit the loop. NOTE: do-while is executed at least once.
13
The do/while statement do statement; while (condition) do { statement block } while (condition) yes no while
14
Example for do-while Usage: Prompt user to input “month” value, keep prompting until a correct value of moth is input. do { cout <<“Please input month {1-12}”); cin >> month; } while ((month 12));
15
do while Loop Example u Bare bones u Does not alert user of problems do { cout << “\n enter an id number:”; cin >> id_num; } while ((id_num 1999)); Lesson 6.3
16
Better do while Loop do { cout << “\n Enter an id number:”; cin >> id_num; if ((id_num 1999)) { cout << “\n invalid number entered” << “\n please check and re-enter”; } else break; } while (1); Decision Statement – Chapter 5 Expression always true Exit loop if id number is valid Lesson 6.3
17
Recap while(s) u while –Most commonly used when repetition not counter controlled –Pre-tested –Loop body may not be executed u do-while –Convenient when at least one repetition needed –Post-tested Lesson 6.3
18
For loop - syntax for (initialize exp; test exp; update exp) statement initialize exp : done only once at the start test exp: This is a condition that evaluates to TRUE or FALSE. update exp: This specifies how to update condition. Note: for loop is used when the number of times to be repeated is fixed/known apriori.
19
For loop - Semantics 1) Initialize exp is executed. 2) Test exp is evaluated. If it is TRUE, body of for is executed else exit for loop; 3) After the execution of the body of the loop, update exp is executed to update condition; go to Step 2 above.
20
The for statement initalize test increment/ decrement true statement(s) false
21
for Loop (cont.) u Example: for (int i = 0; i < 10; i++) cout << i; u Initialization, testing and updating in same statement u Semicolons required u Braces optional if only one statement Lesson 6.4
22
Example: int finalValue; cin >> finalValue; for (int counter = 0; counter < finalValue; counter++) cout << "*"; Display Screen 3 finalValue 3 counter 0 True * 1 * 2 * 3 False Lesson 6.4
23
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; Lesson 6.4
24
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 << "*"; Lesson 6.4
25
Debugging and Testing u Off by one errors –Check loops to ensure you are executing the correct number of times –x < 10 or x <= 10 u Check loop boundaries –Can create infinite loop u Does answer make sense Lesson 6.4
26
For Loop Modifications u break statement –Can use within loop to terminate early –Controversial u Multiple expressions for initialization and increment –Use comma separated list –for (I = 1, j = 2; I < 10; I++, j++) Lesson 6.4
27
Summary u Create while loops u Create do-while loops u Create for loops u Trace and debug loops u Use loops to solve problems Learned how to: Chapter 6
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.