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)
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
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
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...
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
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
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
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
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"