Presentation is loading. Please wait.

Presentation is loading. Please wait.

A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.

Similar presentations


Presentation on theme: "A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading."— Presentation transcript:

1 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

2 loops2 Leading Decision WHILE condition body of loop – group of one or more statements indent one level ENDWHILE

3 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 body statement Expression

4 loops4 1-2-3-4 for While Loops 1.Initial condition 2.Test - WHILE(…) 3.Body 4.Next - often the same as step 1

5 loops5 Example Display "Do you want to play?(y/n)"1 Input ans WHILE (ans = ‘y’) OR (ans = ‘Y’)2 …//body3 Display “Do you want to continue(y/n)?”4 Input ans ENDWHILE Display “Thanks for playing!”

6 loops6 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.

7 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

8 loops8 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

9 // Sentinel controlled loop total = 0; Display "Enter a blood pressure (-1 to stop ) "1 Input thisBP; WHILE (thisBP != -1) // WHILE not sentinel2 total = total + thisBP;3 Display “Enter a blood pressure (-1 to stop ) ”4 Input thisBP ENDWHILE Display total

10 loops10 Example Input number WHILE (number < 0) Display “Enter positive values only!” Input number ENDWHILE

11 loops11 Reading in records with sentinel value Trailer record Read fahrTemp1 WHILE fahrTemp <> 999 2 cels_temp = (5 * (fahrTemp – 32))/9 3 Print fahrTemp, celsTemp Read fahrTemp4 ENDWHILE

12 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

13 loops13 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

14 loops14 Example Read fahrTemp1 WHILE (there is a record)2 cels_temp = (5 * (fahrTemp – 32))/93 Print fahrTemp, celsTemp Read fahrTemp4 ENDWHILE

15 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

16 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

17 loops17 1-2-3-4 Count 1.Initial condition Initialize counter 2.Test - WHILE(…) While (counter > 0) 3.Body 4.Next – Increment Counter

18 //Print Hello 10 times Declare integer count count = 0;1 WHILE (count < 10)2 Display "Hello"3 count = count + 14 ENDWHILE Known Count

19 //Print Hello 10 times Declare integer count Display "How many times should we print Hello?" Input count1 WHILE (count > 0)2 Display "Hello"3 count = count -1 4 ENDWHILE Count is variable

20 loops20 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

21 Declare integer thisBP, total, count Open file count = 0 // initialize 1 total = 0 Read thisBP WHILE ( count < 100 AND there is a record) 2 total = total + thisBP ; 3 count= count + 1 Read thisBP;4 END WHILE Display “The total = “ total Display "The average is " total/count 21

22 loops22 Infinite Loop index = 1 WHILE (index < 5) Print “Good Morning!” ENDWHILE

23 loops23 Never executed WHILE ans = “yes” …. Print “Do you want to add another number?” Input answer ENDWHILE

24 loops24 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)

25 loops25 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 ENDIF

26 loops26 Trailing Decision Loop REPEAT Body UNTIL condition Test at the bottom Statements are executed at least once

27 loops27 Trailing decision loop Body condition FALSE TRUE

28 loops28 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”

29 loops29 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

30 loops30 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.

31 loops31 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"


Download ppt "A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading."

Similar presentations


Ads by Google