Loops
Looping means repeating something. It is a very common programming construct. We can use it to perform repetitive actions in our programs. Let’s start with the while
construct. This construct evaluates a Boolean expression in every loop, and as long as the expression returns True
, the code block under while
is run repeatedly. When the Boolean expression returns False
, the loop terminates. Following is a simple example:
i = 0 while i < 10: print(i) i = i + 1
The output is as follows:
>>> %Run -c $EDITOR_CONTENT 0 1 2 3 4 5 6 7 8 9
Sometimes, we may wish to run the loop forever. In such cases, we can use 1
or True
in place of the Boolean expression in the while
construct as follows:
#while 1 while True: print("Processing...")
In order to exit the loop, press Ctrl + C in the shell. Following is the output:
Processing... Processing... Processing... Processing...