When translated into a computer language this code would have the following output:
1
2
3
4
What is happening here? Like selection, we have a test condition. The while loop tests whether the variable X is less than 5. We set X to 1 at the start of the program. The first time we run through the loop (contained in curly brackets) we print X to the screen, which is 1. The next line in the loop adds 1 to the variable X.
The code in the loop is repeated while X is less than 5. As we keep adding 1 to X each time we go through the loop, eventually we reach the point where X is not less than 5, at which point the loop terminates, and the program would move on the next statement if there was one.
Let's see how this code fragment would look in the Python programming language.
Example3.py
x = 1
while x < 5:
print(x)
x = x + 1
Our actual code is looking very similar to our pseudocode.