Counting Loops. A counter-controlled loop (or counting loop) is a loop whose repetition is managed by a loop control variable whose value represents a count
This is why it is called a pre-test. Note 3: This is a sentinel controlled loop (-1). 6. Page 2. 2. Flowchart view of while loop.
Types of Loops. • Counting loop. – Know how many times to loop. • Sentinel-controlled loop. – Expect specific input value to end loop.
Sentinel-Controlled while Loops. Example 1: Average of numbers. Example 2: Telephone Digits. Sentinel. Sentinel variable is tested in the condition. Loop ends
For sentinel-controlled loops. 1. Read before the loop (priming read). 2. Test input to make sure it is not the sentinel value. 3. Process.
Java provides a powerful control structure called a loop which controls how many 4.2.4 Controlling a Loop with a Sentinel Value.
Jan 3 2015 For sentinel-controlled loops. 1. Read before the loop (priming read). 2. Test input to make sure it is not the sentinel value. 3. Process.
variable but in exit controlled loop Control variable can be counter & sentinel variable. • Execution: In entry controlled loop each execution occurs by
after each iteration of the loop it stores the sum of the numbers added so far (running total) int days;. //Counter for count-controlled loop!
Kinds of Loops !Event Controlled Loop Executes until a specified situation !Describes all types of loops !Count Controlled Loop Executes a specified number of times !Sentinel Controlled Loop Executes until a dummy value is encountered in the input 6 Event Controlled Loop (Example) int high = 20;int low = 0;
l Count-controlled loop: body executes a specific number of times using a counter ? actual count may be a literal or stored in a variable l Count-controlled loop follows a pattern: ? initialize counter to zero (or other start value) ? test counter to make sure it is less than count ? update counter during each iteration 15 5 6 The
Case Study: Using Sentinel Controlled Loops Formulating Algorithms with Top-Down Stepwise Refinement Problem becomes: Develop a class-averaging program that will process an arbitrary number of grades each time the program is run Unknown number of students How will the program know to end? Use sentinel value
A Sentinel-controlled Loop Requires a “priming read” A priming read is the reading of one set of data before the loop to initialize the variables in the expression 32 total = 0; cout > thisBP; // Sentinel controlled loop 33 // Sentinel controlled loop cont while(thisBP != -1) // While not
Case 2: Sentinel-Controlled whileLoops •A sentinel variable is tested in the condition •The loop ends when the sentinel is encountered •The following is an example of a sentinel-controlled whileloop: cin >> variable; //initialize the loop control variable while (variable != sentinel) //test the loop control variable {
Requires a “priming read” before the loop starts ? so the sentinel is NOT included in the sum ? the loop can be skipped (if first value is the sentinel) 5 8 Sentinel controlled loop Sentinel example 7 • Example: !!!!! • Output: float total = 0 0; //Accumulator! float miles; //daily miles ridden!!