Transcript Loops

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
Leading Decision
WHILE condition
body of loop – group of one or more statements
indent one level
ENDWHILE
loops
2
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
Expression
TRUE
body
statement
1-2-3-4 for While Loops
1.Initial condition
2.Test - WHILE(…)
3.Body
4.Next - often the same as step 1
loops
4
Example
Display "Do you want to play?(y/n)"
Input ans
WHILE (ans = ‘y’) OR (ans = ‘Y’)
…
//body
Display “Do you want to continue(y/n)?”
Input ans
ENDWHILE
Display “Thanks for playing!”
loops
1
2
3
4
5
Loops
Sentinel
controlled
keep processing data
until a special value is
entered to indicate that
processing should stop
Count
Controlled
keep processing data
for a specified number
of times
keep processing data as
long as there is more
data in the file
End-of-file
Controlled
Flag
Controlled
Read blood pressures until
a special value
(like -1) selected by you is
read.
Read 100 blood
pressures.
Read all the blood
pressures from a file no
matter how many are there
keep processing data
Read blood pressures until
while a flag condition is a dangerously high BP (200
true
or more) is read.
loops
6
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
1-2-3-4 Sentinel Value
1. Initial condition
•
Get first value
2. Test - WHILE(…)
• While (val != sentinel)
3. Body
4. Next - often the same as step 1
•
Get next value
loops
8
// Sentinel controlled loop
total = 0;
Display "Enter a blood pressure (-1 to stop ) "
Input thisBP;
1
WHILE (thisBP != -1) // WHILE not sentinel
total = total + thisBP;
Display “Enter a blood pressure (-1 to stop ) ”
Input thisBP
ENDWHILE
Display total
2
3
4
Example
Input number
WHILE (number < 0)
Display “Enter positive values only!”
Input number
ENDWHILE
loops
10
Reading in records with sentinel
value
•
Trailer record
Read fahrTemp
WHILE fahrTemp <> 999
cels_temp = (5 * (fahrTemp – 32))/9
Print fahrTemp, celsTemp
Read fahrTemp
ENDWHILE
loops
1
2
3
4
11
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 record
WHILE (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
1-2-3-4 Reading in Records
1. Initial condition
•
Read first record
2. Test - WHILE(…) –
• While (not eof)
3. Body
4. Next - often the same as step 1
•
Read next record
loops
13
Example
Read fahrTemp
WHILE (there is a record)
cels_temp = (5 * (fahrTemp – 32))/9
Print fahrTemp, celsTemp
Read fahrTemp
ENDWHILE
loops
1
2
3
4
14
// End-of-file controlled loop
Open file
total = 0;
Read thisBP;
// priming read
1
WHILE (there is a record)
total = total + thisBP
Read thisBP
// read another
END WHILE
Display total;
2
3
4
Count-controlled loop
• 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
1-2-3-4 Count
1. Initial condition
•
Initialize counter
2. Test - WHILE(…)
• While (counter > 0)
3. Body
4. Next –
•
Increment Counter
loops
17
Known Count
//Print Hello 10 times
Declare integer count
count = 0;
WHILE (count < 10)
Display "Hello"
count = count + 1
ENDWHILE
1
2
3
4
Count is variable
//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
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
20
Declare integer thisBP, total, count
Open file
count = 0
// initialize 1
total = 0
Read thisBP
WHILE ( count < 100 AND there is a record)
total = total + thisBP ;
count= count + 1
Read thisBP;
4
END WHILE
Display “The total = “ total
Display "The average is " total/count
2
3
21
Infinite Loop
index = 1
WHILE (index < 5)
Print “Good Morning!”
ENDWHILE
loops
22
Never executed
WHILE ans = “yes”
….
Print “Do you want to add another number?”
Input answer
ENDWHILE
loops
23
Don't forget to prime the loop!
•
Initialize initial condition by reading in or
setting value
•
Input ans
WHILE (ans = 'y')
index = 0
WHILE (index < 10)
•
•
Read name, ssNum,phone
WHILE (there is a record)
loops
24
Declare integer count
Declare real total, avg, num
total = 0
count = 0
Input num
WHILE not end-of-file
total = total + num
count = count + 1
Input num
ENDWHILE
IF count = 0 THEN
Print “No numbers entered”
ELSE
avg = total/count
Print “The average is “,avg
loops
ENDIF
25
Trailing Decision Loop
REPEAT
Body
UNTIL condition
Test at the bottom
Statements are executed at least once
loops
26
Trailing decision loop
Body
condition
FALSE
TRUE
loops
27
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
28
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
29
Counted Loop
DO counter = initial_value to end_value
…
ENDDO
Automatic:
1. When the computer executes the DO instruction, it sets
the counter equal to the initial value
2. When the loop executes the ENDDO, it increments the
counter
3. 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.
loops
30
Examples
• DO count = 1 to 10
Display "Hello"
ENDDO
• DO count = 1 to 100
Display count
ENDDO
• DO num = 10 to 0 step - 1
Display num
ENDDO
Display "Blast off"
loops
31