Tutorial 7

🔁

Loops: - FOR, WHILE, REPEAT UNTIL

0478 IGCSE


Often, we are required to execute a block of code many times - for example, if processing test scores for every student in the class. For this, there are 3 types of loops (iteration)

  • FOR (count-controlled): will execute code a fixed number of times, either known at write time (e.g. a fixed number of students in a class) or runtime (e.g. looping through every character in a string entered by the user)
  • WHILE (pre-conditional): checks condition, then executes code - loops while condition is TRUE - if the condition is initially false, the code will never execute. Use case - e.g. looping while neither player has won the game
  • REPEAT...UNTIL (post-conditional): executes code, then checks condition - loops while condition is FALSE. Will always execute at least one time, since condition is checked after that initial execution. Use case - e.g. asking user to enter password, until they get it correct or have exceeded max tries

1

FOR Loops

As mentioned, a FOR is a count-controlled loop - here we will simply loop from 1 to 10 and output all the integers in that range

Note how the variable after NEXT has to match the for counter variable (in this example, called "index" - but can be called anything)

DECLARE index : INTEGER FOR index <-- 1 TO 10 OUTPUT index NEXT index

We can also give a custom STEP value - this defines how much the loop parameter 'jumps' each iteration of the loop - the example below will hence output the odd numbers 1, 3, 5, 7, 9

DECLARE index : INTEGER FOR index <-- 1 TO 10 STEP 2 OUTPUT index NEXT index

2

WHILE Loops

Although you can create a simple WHILE loop to loop a fixed number of times as in the example below, a FOR loop would be better suited for this case - this is simply for demonstration

DECLARE index : INTEGER index <-- 1 WHILE index <= 10 DO OUTPUT index index <-- index + 1 ENDWHILE

A better use case would be when we don't know how many times to loop - for example, a user entering their password - they might get it right on the first time, they might never get it right...

DECLARE correctPassword, enteredPassword : STRING correctPassword <-- "supersecret" OUTPUT "Enter your password:" INPUT enteredPassword WHILE enteredPassword <> correctPassword DO OUTPUT "Password incorrect - try again" INPUT enteredPassword ENDWHILE OUTPUT "Login success!"

3

REPEAT UNTIL Loops

As with WHILE loops, although you can create a simple REPEAT UNTIL loop to loop a fixed number of times as in the example below, a FOR loop would be better suited for this case - this is simply for demonstration

DECLARE index : INTEGER index <-- 1 REPEAT OUTPUT index index <-- index + 1 UNTIL index > 10

Again, conditional loops are better suited for when we don't know how many times we need to loop - for example, a quiz question which we require they enter correctly in order to pass - maybe they are correct the first time, maybe never...

Note: in a real program, you would probably want to e.g. convert the user's answer to lowercase too (this would ensure if they entered "High" or "hIgH", these would also be considered correct), perhaps have an extra if statement notifying them they got the question wrong etc

DECLARE userAnswer : STRING REPEAT OUTPUT "If a real language, would pseudocode be considered high or low-level?" INPUT userAnswer UNTIL userAnswer = "high" OUTPUT "That's correct!"

4

Sequences

Create FOR loops to output the follow sequences:

  • All the positive integers 0 to 100
  • All the integers between -100 and 100 inclusive
  • The 15 times table from 0 to 150
  • The first 100 square numbers
  • The first 10 cube numbers in descending order
  • 0 to 1 in increments of 0.01
  • From 1 to up to 10000, where the gap between each number starts from 1 and increments by 1 each iteration
DECLARE stepAmount : INTEGER OUTPUT "--- Challenge 1 ---" FOR n <-- 0 TO 100 OUTPUT n NEXT n OUTPUT "--- Challenge 2 ---" FOR n <-- -100 TO 100 OUTPUT n NEXT n OUTPUT "--- Challenge 3 ---" FOR n <-- 0 TO 150 STEP 15 OUTPUT n NEXT n OUTPUT "--- Challenge 4 ---" FOR n <-- 1 TO 100 OUTPUT n * n NEXT n OUTPUT "--- Challenge 5 ---" FOR n <-- 10 TO 1 STEP -1 OUTPUT n ^ 3 NEXT n OUTPUT "--- Challenge 6 ---" FOR n <-- 0 TO 1 STEP 0.01 OUTPUT n NEXT n OUTPUT "--- Challenge 7 ---" stepAmount <-- 1 FOR n <-- 1 TO 10000 STEP stepAmount OUTPUT n stepAmount <-- stepAmount + 1 NEXT n

5

42

Use a post-conditional loop to ask the user "What is the Ultimate Answer to Life, The Universe and Everything", telling them they're wrong, until they enter "42" or "fourty two" (apparently the correct spelling is "forty"...so you can choose to accept both)

Note: in a real program, you might want to convert their answer to lower case, to ensure "Fourty Two" is converted to "fourty two" and hence considered valid - see the built-in functions tutorial to see how to do this

DECLARE userAnswer : STRING DECLARE correct : BOOLEAN correct <-- FALSE REPEAT OUTPUT "What is the Ultimate Answer to Life, The Universe and Everything?" INPUT userAnswer IF userAnswer = "42" OR userAnswer = "fourty two" OR userAnswer = "forty two" THEN correct <-- TRUE OUTPUT "That's correct - how did you know?" ELSE OUTPUT "Unlucky...do you need a few million more years to think?" ENDIF UNTIL correct = TRUE

6

Factors of 60

Create a pre-conditional loop that keeps asking the user to input 2 integers, until those integers multiply to equal 60

Display the sum at the end - e.g. "Correct: 4 * 15 = 60"

DECLARE num1, num2 : INTEGER num1 <-- 0 num2 <-- 0 WHILE num1 * num2 <> 60 DO OUTPUT "Enter first of two numbers that multiply to equal 60:" INPUT num1 OUTPUT "Enter second of two numbers that multiply to equal 60:" INPUT num2 ENDWHILE OUTPUT "Correct: ", num1, " * ", num2, " = 60"