Skip to content

Control Statements

Control statements in Python determine the flow of execution based on conditions or repetitions. This chapter introduces the concept of loops and how we can manipulate the flow of loops using control statements.

Sometimes we want to repeat a set of statements in our program. For instance: Print 1 to 1000. A loop allows us to repeat a block of code multiple times. Instead of writing the same code again and again, loops help automate tasks by iterating over sequences (like lists, tuples, dictionaries, etc.) or running until a specific condition is met.


Loop TypeDescription
While LoopRepeats a statement or group of statements while a given condition is TRUE. It tests the condition before executing the loop body.
For LoopExecutes a sequence of statements multiple times and abbreviates the code that manages the loop variable.

The while loop in Python is used when we want to execute a block of code as long as a condition is true.

Syntax:

while condition:
# Code block to be executed

Example:

i = 1
while i < 5:
print(i)
i += 1

Explanation:

  • The condition i < 5 is checked before each iteration.
  • As long as the condition is true, the code block inside the loop is executed.
  • i is incremented by 1 in each iteration.

Note: If the condition never becomes false, the loop keeps getting executed, making it an INFINITE LOOP.


An infinite loop occurs when the loop's condition is always true and never becomes false. This leads to the loop running endlessly. Such loops should be used cautiously, as they can cause programs to freeze or crash.

Example of Infinite Loop:

while True:
print("This is an infinite loop!")

To break out of an infinite loop, we typically use a break statement, which we will cover later.


The for loop in Python provides the ability to loop over the items of any sequence, such as a list, tuple, or a string.

Syntax:

for item in sequence:
# Code block to be executed

Example:

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)

The range() function generates a sequence of numbers, which is often used with loops. It is particularly useful for generating a series of numbers in a for loop.

Syntax:

range(start, stop, step)
  • start: The starting number (default is 0).
  • stop: The end number (exclusive).
  • step: The difference between each number in the sequence (default is 1).

Example:

for i in range(1, 6):
print(i)

Sometimes we need to iterate over both the index and the value of a sequence. We can achieve this using the range() function combined with len().

Example:

fruits = ["apple", "banana", "cherry"]
for i in range(len(fruits)):
print(f"Index: {i}, Value: {fruits[i]}")

If an else block is used with a for loop, it is executed only when the for loop terminates normally (i.e., not by a break statement).

Example:

l = [1, 7, 8]
for item in l:
print(item)
else:
print("done") # This is printed when the loop exhausts!

A nested loop is a loop inside another loop. The inner loop runs completely for each iteration of the outer loop.

Syntax:

for iterating_var in sequence:
for iterating_var in sequence:
statements(s)
statements(s)

Example:

for i in range(1, 10):
for j in range(1, 10):
k = i * j
print(k, end=' ')
print()

Output:


Loop control statements change the flow of a loop from its normal behavior. Python provides three main loop control statements:

Control StatementDescription
Break StatementThe break statement terminates the loop immediately and the control flows to the statement after the body of the loop.
Continue StatementThe continue statement terminates the current iteration of the statement, skips the rest of the code in the current iteration and the control flows to the next iteration of the loop.
Pass StatementThe pass keyword in Python is generally used to fill up empty blocks and is similar to an empty statement represented by a semi-colon in languages such as Java, C++, JavaScript.

The break statement is used to exit the loop before it has iterated through all items or before the condition becomes false. When break is executed, the loop terminates immediately.

Example:

for i in range(1, 6):
if i == 3:
break
print(i)

Explanation: The loop stops as soon as i becomes 3, due to the break statement.


The continue statement is used to skip the rest of the code inside the loop for the current iteration and move to the next iteration.

Example:

for i in range(1, 6):
if i == 3:
continue
print(i)

Explanation: When i is 3, the continue statement is executed, which skips printing 3 and moves to the next iteration.


  • The pass statement does nothing and is used as a placeholder.
  • It is useful when you need a loop but do not want to execute any code inside the loop at the moment.
  • The pass keyword in Python is generally used to fill up empty blocks and is similar to an empty statement represented by a semi-colon in languages such as Java, C++, and Javascript.

Example:

for i in range(1, 6):
pass # Without pass, the program will throw an error

In this case, nothing will be executed, but the loop is syntactically correct.