loops
• A loop is a repetition control structure.• body - statements to be repeated• control statement - decides whether another
repetition needs to be made • leading decision loop - control statement before
body• trailing decision loop - control statement after
body• Counted loop- for• Logical loop - WHILE..ENDWHILE
Loops
loops 2
Leading DecisionWHILE condition
body of loop – group of one or more statements
indent one level
ENDWHILE
loops
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
bodystatement
Expression
loops 4
1-2-3-4 for While Loops
1.Initial condition2.Test - WHILE(…)3.Body4.Next - often the same as step 1
loops 5
Example
Display "Do you want to play?(y/n)" 1
Input ans
WHILE (ans = ‘y’) OR (ans = ‘Y’) 2
… //body 3
Display “Do you want to continue(y/n)?” 4
Input ans
ENDWHILE
Display “Thanks for playing!”
loops 6
Loops
Sentinel controlled
keep processing data until a special value is entered to indicate that processing should stop
Read blood pressures until a special value (like -1) selected by you is read.
Count Controlled
keep processing data for a specified number of times
Read 100 blood pressures.
End-of-file Controlled
keep processing data as long as there is more data in the file
Read all the blood pressures from a file no matter how many are there
Flag Controlled
keep processing data while a flag condition is true
Read blood pressures until a dangerously high BP (200 or more) is read.
loops
A Sentinel-controlled Loop
• Read numbers until -1, 999
• Not always easy to determine sentinel value
• requires a “priming read”
• “priming read” means you read one set of data before the WHILE
loops 8
1-2-3-4 Sentinel Value
1. Initial condition • Get first value
2. Test - WHILE(…) • While (val != sentinel)
3. Body4. Next - often the same as step 1
• Get next value
loops
// Sentinel controlled loop
total = 0;
Display "Enter a blood pressure (-1 to stop ) " 1
Input thisBP;
WHILE (thisBP != -1) // WHILE not sentinel 2
total = total + thisBP; 3
Display “Enter a blood pressure (-1 to stop ) ” 4
Input thisBP
ENDWHILE
Display total
loops 10
Example
Input number
WHILE (number < 0)
Display “Enter positive values only!”
Input number
ENDWHILE
loops 11
Reading in records with sentinel value
• Trailer record
Read fahrTemp 1
WHILE fahrTemp <> 999 2
cels_temp = (5 * (fahrTemp – 32))/9 3
Print fahrTemp, celsTemp
Read fahrTemp 4
ENDWHILE
loops
End-of-File Controlled Loop
• depends on fact that a file goes into fail state when you try to read a data value beyond the end of the file
• No trailer recordWHILE (there is a record)WHILE (not end of file)WHILE (records exist)
• Computer indicates there are no more records by sending a signal to the program
• Must read record before entering loop – there may be no records
loops 13
1-2-3-4 Reading in Records
1. Initial condition • Read first record
2. Test - WHILE(…) – • While (not eof)
3. Body4. Next - often the same as step 1
• Read next record
loops 14
Example
Read fahrTemp 1
WHILE (there is a record) 2
cels_temp = (5 * (fahrTemp – 32))/9 3
Print fahrTemp, celsTemp
Read fahrTemp 4
ENDWHILE
loops
Open file
total = 0;
Read thisBP; // priming read 1
WHILE (there is a record) 2
total = total + thisBP 3
Read thisBP // read another 4
END WHILE
Display total;
// End-of-file controlled loop
loops
• Do something a set number of times
• Need counter– initialize– increment
• iteration counter - incremented during each iteration of the loop
• event counter - incremented each time a particular event occurs
Count-controlled loop
loops 17
1-2-3-4 Count
1. Initial condition • Initialize counter
2. Test - WHILE(…) • While (counter > 0)
3. Body4. Next –
• Increment Counter
loops
//Print Hello 10 times
Declare integer count
count = 0; 1
WHILE (count < 10) 2
Display "Hello" 3
count = count + 1 4
ENDWHILE
Known Count
loops
//Print Hello 10 times
Declare integer count
Display "How many times should we print Hello?"
Input count 1
WHILE (count > 0) 2
Display "Hello" 3
count = count -1 4
ENDWHILE
Count is variable
loops 20
Accumulators and Counters• To find the average of a group of numbers-need running
total and how many numbers• Counter – storage area in which we count
– increment– counter = counter + 1– paychecks = paychecks + 1
• Accumulator – storage area for keeping cumulative or running totals– accumulator = accumulator + number– total_wages_paid = total_wages_paid + net_pay– Total = total + num
• You must initialize the values of accumulators and counters
loops
Declare integer thisBP, total, count
Open filecount = 0 // initialize 1total = 0Read thisBP WHILE ( count < 100 AND there is a record) 2 total = total + thisBP ; 3
count= count + 1 Read thisBP; 4END WHILEDisplay “The total = “ total Display "The average is " total/count
21
loops 23
Never executed
WHILE ans = “yes”
….
Print “Do you want to add another number?”
Input answer
ENDWHILE
loops 24
Don't forget to prime the loop!
• Initialize initial condition by reading in or setting value
• Input ansWHILE (ans = 'y')
• index = 0WHILE (index < 10)
• Read name, ssNum,phone WHILE (there is a record)
loops 25
Declare integer countDeclare real total, avg, numtotal = 0count = 0Input numWHILE not end-of-file
total = total + numcount = count + 1Input num
ENDWHILEIF count = 0 THEN
Print “No numbers entered”ELSE
avg = total/countPrint “The average is “,avg
ENDIF
loops 26
Trailing Decision Loop
REPEAT
Body
UNTIL condition
Test at the bottom
Statements are executed at least once
loops 28
Example
REPEAT
Display “Enter two numbers”
Input num1,num2
Display num1, “ + “, num2, “ = “, num1+num2
Display “Do you want to enter two numbers again?”
Input ans
UNTIL ans = “no”
loops 29
Counted loop
• Fixed number of iterations• Use a variable as a counter which starts at a
specified number and increments the variable each time the loop is processed
• Repeats until the counter is greater than an ending number
• Beginning value, ending value, increment value
loops 30
Counted LoopDO counter = initial_value to end_value
…ENDDOAutomatic:1. When the computer executes the DO instruction, it sets
the counter equal to the initial value2. When the loop executes the ENDDO, it increments the
counter3. When the counter is less than or equal to the ending
number, the processing continues to the body. When the counter is greater than the ending number, the processing continues at the instruction that follows the ENDDO instruction.