1 Chapter 4 Repetition StructuresKing Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi Edited By: Fatimah Alakeel & Noor Alhareqi Chapter 4 Repetition Structures 1st Semester
2 Conditional Loops In many programming situations, we will not be able to determine the exact number of loop repetitions before loop execution begins. Sentinel-Controlled loops Flag-Controlled loop
3 Sentinel-Controlled while LoopUsed when exact number of entry pieces is unknown, but last entry (special/sentinel value) is known. i.e. the user uses a special value called a sentinel value to indicate “end of data entry.” The sentinel value must be chosen so that it’s not confused with an acceptable input value. General form: Input the first data item into variable; while (variable != sentinel) { . input a data item into variable; }
4 Sentinel-Controlled while Loop example1Read input before loop Program to compute the product of entered numbers prod= 1; cout << "Enter a value, 0 will stop the program:"; cin >> value; while(value != 0) { prod = prod * value; cout << "Enter a value, 0 will stop the program:"); } cout << "The product is:” << prod; It is very common for loops to have identical initialization and update steps while performing input operations where the number of input values is not known in advance. Initialization Testing Update
5 Sentinel-Controlled while Loop example2/* Compute the sum of a list of exam scores. */ #include
6 A.AlOsaimi
7 Convert Sentinel Controlled while loop for loopBecause the for statement combines the initialization, test, and update in once place, some programmers prefer to use it to implement sentinel-controlled loops. cout<<"Enter first score or ”<< SENTINEL << “to quit:”; for( cin >> score; score != SENTINEL; cin >> score) { sum += score; cout << "Enter next score or ” << SENTINEL << “to quit:”; }
8 Flag-Controlled while Loopint value used to control loop ( true 1 or false 0) . General form: bool found = false; while (!found) { . if (expression) found = true; }
9 Example OUTPUT: inside loop 3 is found value of flag is:1#include
10 Flag-Controlled while Loop- Example 1(Guessing the number game)C Standard General Utilities Library It defines several general purpose functions such as rand()
11 Nested Loops Usually used to work with two dimensional arrays (later).Nested loops consist of an outer loop with one or more inner loops. Each time the outer loop is repeated, the inner loops are reentered Their loop control expressions are reevaluated All required iterations are performed again.
12 Example: Sum of Scores of 3 sections calculate the total scores in each section for 3 sections. Each section's * scores are terminated by the sentinel -99. */ #include
13 A.AlOsaimi
14 What is the Output? #include
15 A.AlOsaimi
16 do while statement Both the for statement and the while statement evaluate the loop condition before the first execution of the loop body. In most cases, this pretest is desirable and prevents the loop from executing when there may be no data items to process. There are some situations, generally involving interactive input, when we know that a loop must execute at least one time.
17 do while Example1 #define KMS_PER_MILE 1.609#include
18 do while Example2 int main () {#include
19 do while Example3 /* next do-while checks that the entered value is between n_min and n_max */ do { cout << "Enter an integer between “<< n_min << “ and “<< n_max << “inclusive “; cin>> inval ; if (inval < n_min || inval > n_max) cout << "Sorry wrong input, try agin\n”; } while (inval < n_min || inval > n_max) ; cout << "input checked successfully”; return 0; }
20 break Statements Used to Can be placed within if statement of a loop.exit early from a loop. (while, for, and do...while) skip remainder of switch structure. Can be placed within if statement of a loop. If condition is met, loop is exited immediately. After the break statement executes, the program continues to execute with the first statement after the structure
21 break Statements Output 1 2 3 4Example : int count ; for ( count = 1 ; count <= 10 ; count ++ ) { if ( count == 5) break ; cout << count } Output
22 continue Statements Used in while, for, and do...while structures.When executed in a loop, the remaining statements in the loop are skipped; proceeds with the next iteration of the loop. When executed in a while/do…while structure, expression is evaluated immediately after continue statement. In a for structure, the update statement is executed after the continue statement; the loop condition then executes.
23 continue Statements Output 1 2 3 4 6 7 8 9 10Example : int count ; for ( count = 1; count <= 10 ; count ++ ) { if ( count == 5) continue; cout << count ; } Output