Eltham High School Information Technology Problem Solving and Program Design LOOP This is also known as ITERATION/REPETITION. A loop happens when on e or more programming instructions are repeated until a condition is met. There are three major types of loops: FOR----DO WHILE and WHILE----DO REPAET ---UNTIL The above types of loop can be categorized as BOUNDED OR UNBOUND LOOPS. Bounded Loop (Definite Loop): This type of loop is set to repeat a process a number of times. With this loop, you know where you are starting until you end. So the loop as a start and an end that you are aware of. FOR---DO FOR---DO LOOP STRUCTURE FOR counter = <start number> TO <end number> DO <Statement 1> <Statement 2> ENDFOR Example: Write a pseudocode to print, “This is a loop” 5 times on the screen. FOR counter = 1 TO 5 DO Print “This is a loop” ENDFOR The above loop will be printed/run five (5) times with the result below 1. This is a loop 2. This is a loop 3. This is a loop 4. This is a loop 5. This is a loop At the end of writing it 5 times, the loop will stop because it reach the limit. **ACTIVITIES** Write a for loop to print the following. 1. To print the words “this is a loop 3 times. 2. Ge the name of 4 people and their age. 3. Get the age of 5 people. 4. Get 6 test scores. 5. Get 4 test score and add them. 1 UNBOUNDED LOOP (INDEFINITE LOOP): This type of loop repeats a set of steps or instructions until a particular condition becomes false. It allows a program to be repeated multiple times, just as when you let a song repeats multiple times. WHILE and WHILE---DO REPEAT---UNTIL WHILE STRUCTURE With this type of loop it will check to see if the condition is TRUE and while the condition is TRUE it will repeat the code until it is no true/false. This type of loop is also called a TOP CHECKING LOOP as the control condition is placed at the first line of the code. If the statement/instruction encloses in the loop are false the loop will never be executed. This is also similar to he IF statement. It check the condition before entering the loop. WHILE < condition> <statement/instruction> ENDWHILE Examples: Write a pseudodoce to let persons get a ride while they have more than 0 tickets. While x < 8 Print x X = x+1 The above loop will print Say the value enter for x at first is 4 then it will be, Print 4 Then add 1 to that 4: x = x+1 Print 5 Then add 1 to that 5: x = x + 1 Print 6 Then add 1 to that 6: x = x + 1 Print 7 Then add 1 to that 7: x = x + 1 Print 8 At this point 8 is not less than 8 so that means you are done with the while loop and it becomes false. **ACTIVITIES** Write loop for the following. 1. Print the numbers from 0 to 4 2. Print the numbers from 1 to 5 3. Find the sum of 10 and 1 while sum is not equal to 14. 2 WHILE---DO LOOP STRUCTURE This type of loop is similar to a while loop only difference is that the while do loop condition is tested at the end of the loop. Therefore, the instruction/statement is executed at least once before the condition is tested. It is also called the BOTTOM-CHECKING LOOP WHILE <condition is true> DO <Instruction 1> <Instruction 2> <Instruction 3> ENDWHILE Example: Input a number that is less than 6. Print “enter a number less than 6” Read num WHILE num < 6 DO Print “that is not less than 6” Read num ENDWHILE If the user had entered 2 that means you can now repeat the action of entering numbers until 6 or a number that is greater than 6 is entered. **ACTIVITIES** Write a while do loop for the following. 1. Write a pseudocode to get numbers until 0 is entered. 2. Write a while loop to collect series of numbers and only stop when the user enters 999 3. Write a while loop to collect names and stop when the user enters x. 4. Write a while loop to collect numbers from users and stop when the user enter a number that is less than 7. 3 REPEAT---UNTIL LOOP STRUCTURE This loop executes a block statement (s) repeatedly, until a given condition evaluates to TRUE. This loop test the condition at the bottom of the loop. It is a bit similar to the WHILE loop EXCEPT that the REPEAT UNTIL loop is guaranteed to execute at least one time. REPEAT <instruction 1/statement 1> <instruction 2/statement 2> <instruction 3/statement 3> UNTIL <condition is met> Examples: Pseudocode to determine adult age. REPEAT Read age UNTIL age >= 18 **ACTIVITIES** Write a repeat until loop for the following. 1. To collect a series of numbers and only stop when the user enters 999. 2. Collect names form user and stop if the user enters an x. 3. Collect ages and name and stop when they enter an age over 18. LOOPS FLOWCHART 4 5