Iterative Statements in Python
In Python, iterative structures can be implemented using the while and for statements. These allow you to execute a block of instructions repeatedly, either conditionally or unconditionally.
While Statement
The while statement lets you create a conditional loop based on an event.
while [ condition ] :
instruction block
In Python, the block of instructions is not enclosed in braces because it uses indentation, which means using whitespace at the beginning of lines to indicate the block structure.
It's important to remember to type the colon ( : ) immediately after the conditional expression.
A Practical Example
In the following code, the user is asked to input a number.
n = 0
while n < 10:
n = int(input('Enter a number greater than or equal to 10: '))
If the number is less than 10, the program asks the user to input another number.
A possible input/output sequence might look like this:
Enter a number greater than or equal to 10: 4
Enter a number greater than or equal to 10: 7
Enter a number greater than or equal to 10: 11
In the first and second attempts, I entered 4 and 7. The loop repeats because these numbers are less than 10.
On the third attempt, I entered 11, which breaks the loop condition.
Note: It's a good habit to get used to using the while statement in Python programming because it's versatile and can handle both conditional and unconditional loops. Here's a practical example of an unconditional loop using the while statement.
For Statement
The for statement creates an unconditional loop.
for [item] in [iterable]:
instruction block
In Python, the for statement iterates over all elements of an iterable object (e.g., list, tuple, etc.).
For each element in the iterable, the for loop executes a block of instructions.
Note: The block of instructions is not enclosed in braces but is defined by indentation—whitespace at the beginning of the line to indicate that they are part of the loop. Also, remember to include the colon (:) before the instruction block.
The for statement is often used with the range function, which creates an iterable consisting of N elements.
A Practical Example
In this example, I display the numbers from 1 to 9 on the screen.
for i in range(1, 10):
print(i)
The range(1, 10) function creates an iterable from 1 (inclusive) to 10 (exclusive).
The for loop reads each element and executes the block of instructions for each one.
After each iteration, the variable i is incremented.
The output is as follows:
1
2
3
4
5
6
7
8
9
The loop repeats 9 times and is not conditioned on a random event.
On the tenth iteration, the program exits the loop and continues with the next instructions.
Break and Continue
In Python's for and while loops, you can use two statements to control loop execution.
Break
The break statement terminates the loop.
Execution moves to the first instruction after the loop.
A Practical Example
for i in range(1, 5):
if i == 3: break
print(i)
The program's output is:
1
2
On the third iteration, the loop ends due to the break statement.
Continue
The continue statement skips the current iteration.
Execution moves to the next iteration of the loop.
A Practical Example
for i in range(1, 5):
if i == 3: continue
print(i)
The program's output is:
1
2
4
On the third iteration, the loop skips to the next iteration due to the continue statement.