It is the code that makes the computer repeat certain lines of code.
A loop repeats the code inside it. There are several different types of loop: for, while and repeat.
Counters are really useful when combined with loops. When using a counter with a while loop there are a couple of points to remember:
Create the counter variable and assign it a start value outside the loop.
Increment (or decrement if you’re counting down) the counter inside the loop. Not doing this will create an infinite loop. +=1 increments by 1. -=1 decrements by 1.
FOR loops can execute a block of code a defined number of times.
Pseudocode syntax:
FOR var_name = initial_value TO final_value
statements
NEXT var_name
Write a program that prints numbers from 5 to 10.
Output all even numbers smaller than 100.
Write a program that prints odd numbers smaller than 100.
Write a program that prints even numbers between 30 and 50, both included.
Write a program that asks the user for a number. The program prints that number and the consecutive 5 numbers. For example: the user enters the number 34. The program should print: 34 35 36 37 38 39.
Output the following series:
1 2 4 8 16 32 64 128 256
Output the 30 first numbers in the Fibonacci series.
1 1 2 3 5 8 13...
Time tables. Example:
Enter the time table: 9
1 * 9 = 9
2 * 9 = 18
3 * 9 = 27
4 * 9 = 36
5 * 9 = 45
6 * 9 = 54
7 * 9 = 63
8 * 9 = 72
9 * 9 = 81
10 * 9 = 90
Ask the user to enter 5 numbers and your program should say which one is the biggest of those five numbers.
The cubed number sequence starts: 1, 8, 27, 64, 125.
Write a program that:
Asks the user to input a number.
Display N numbers in the cubed sequence according to user input.
xx
A while loop repeats the code inside it while the condition is true. When the computer gets to the start of the loop it checks the condition. If the condition is true the computer enters the loop and runs the code inside it. If the condition is false, the computer skips past the loop and carries on with the rest of the program.
When the computer gets to the end of the loop it goes back and checks the condition again.
If the condition is still true it goes back into the loop and runs the code inside again. This keeps happening until the condition changes to become false.
Syntax in pseudocode:
WHILE condition DO
statements
ENDWHILE
Syntax in Python:
while (condition) :
code statements
This task example is in the form of a quiz which works in the following way:
OUTPUT "What is the capital of France?"
INPUT answer
WHILE answer <> "Paris"
OUTPUT "Incorrect, try again!"
INPUT answer
OUTPUT "Correct! Yahoo!"
Your task is:
to do the trace table
translate the pseudocode to Python code
Create a program that asks the user to enter a number that is 10 or smaller than 10.
Your program should:
allow the user to enter a number
check whether the number is smaller or equals to 10 or not
if the number is NOT smaller or equals to ten, your program should output the message Incorrect, enter a number smaller or equals to 10. And, allow the user to input a number again.
When the user has entered a number smaller or equals to 10, your program should output a thank you message, this way:
Thank you, you entered 7
Write a program that stores a secret number in a variable (you decide the number and the name of the variable)
The user has to guess the secret number, the program should loop until they get it right.
Once the user has guessed correctly they get a congratulations message
Write an algorithm that inputs the height of children who want to ride on a rollercoaster. Children under 1.2 metres are rejected. The ride starts when eight children have been accepted.
Write an algorithm in pseudocode and Python that inputs the ages of people entering an event. Your algorithm stops asking for ages of people when a negative value for age has been inputted.
Your algorithm outputs the number of people at the event over the age of 18.
Write an algorithm in pseudocode and Python that allows you to input numbers, ignores any negative numbers and then outputs the average of all the positive numbers only. An input of zero ends the process.
This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is false.
Pseudocode syntax:
REPEAT
statements
UNTIL condition
Study the following algorithm:
1 Smallest ← 100
2 INPUT NumberOfStudents
3 X ← 0
4 REPEAT
5 INPUT age
6 x ← X + 1
7 IF age < Smallest
8 THEN
9 Smallest ← age
10 ENDIF
11 UNTIL X = NumberOfStudents
12 OUTPUT Smallest
a) Do the trace table of the algorithm above.
b) Explain the purpose of the algorithm above
c) In the above algorithm, X is a variable.
Give a more appropriate name to the variable X.
Read this section of program code that inputs 10 positive numbers and then outputs the smallest number input.
1 Small ← 1000
2 Counter ← 0
3 REPEAT
4 INPUT Num
5 IF Num < Small THEN Small ← Num
6 Counter ← Counter + 1
7 UNTIL Counter = 10
8 OUTPUT Small
a) Do the trace table of the algorithm above.
b) Identify three changes you would need to make to find the largest number input instead of the smallest number.
c) Rewrite the program code with your changes.
Read this section of program code that inputs 10 positive numbers and then outputs the total.
1 Total ← 0
2 Counter ← 0
3 REPEAT
4 INPUT Num
5 Total ← Total + Num
6 OUTPUT Total
7 Counter ← Counter + 1
8 UNTIL Counter = 10
This code works, but it is inefficient.
a) Do the trace table of the algorithm above.
b) Suggest three improvements that could be made.
c) Rewrite the program code with your improvements.