Python Loops
Python has two primitive loop commands:
• while loops
• for loops
This command terminates
the loop's execution and
1 Break statement transfers the program's
control to the statement
next to the loop.
This command skips the
current iteration of the
loop. The statements
following the continue
2 Continue statement
statement are not
executed once the Python
interpreter reaches the
continue statement.
The pass statement is
used when a statement is
3 Pass statement syntactically necessary,
but no code is to be
executed.
The while Loop
With the while loop we can execute a set of statements as long as a condition is
true.
Syntax:
while (condition){
# Code to be executed while the condition is true
}
• condition is the expression or condition that is evaluated before each
iteration. If the condition is true, the code block inside the loop is
executed. If the condition is false initially, the code block is skipped, and
the loop terminates immediately.
• The code block inside the loop is indented and contains the statements to
be executed repeatedly while the condition remains true.
While loops are particularly useful when the number of iterations is uncertain or
dependent on dynamic conditions. They allow for flexible iteration based on
changing circumstances within a program.
Example
Print i as long as i is less than 6:
i=1
while i < 6:
print(i)
i += 1
The break Statement
With the break statement we can stop the loop even if the while condition is
true:
Example
Exit the loop when i is 3:
i=1
while i < 6:
print(i)
if i == 3:
break
i += 1
The continue Statement
With the continue statement we can stop the current iteration, and continue
with the next:
Example
Continue to the next iteration if i is 3:
i=0
while i < 6:
i += 1
if i == 3:
continue
print(i)
The else Statement
With the else statement we can run a block of code once when the condition no
longer is true:
Example
Print a message once the condition is false:
i=1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6")
The for loop
For loop is a control flow statement in programming that allows you to execute
a block of code repeatedly based on a specified condition. It is commonly used
when you know how many times you want to execute a block of code.
Syntax:
for (initialization; condition; increment/decrement)
// Code to be executed repeatedly
1. Initialization: This part of the loop is where you initialize a variable or
set a starting value for a counter variable that controls the loop. It
typically occurs before the loop starts and is executed only once.
2. Condition: The condition is a Boolean expression that determines
whether the loop should continue executing or not. If the condition
evaluates to true, the loop body is executed. If it evaluates to false, the
loop terminates.
3. Increment (or Decrement): This part of the loop is responsible for
updating the loop control variable after each iteration. It typically occurs
at the end of each iteration and is used to modify the loop control variable
to eventually make the condition false.
Print each fruit in a fruit list:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
Even strings are iterable objects, they contain a sequence of characters:
Example
Loop through the letters in the word "banana":
for x in "banana":
print(x)
The break Statement
With the break statement we can stop the loop before it has looped through all
the items:
Example
Exit the loop when x is "banana":
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
if x == "banana":
break
Example
Exit the loop when x is "banana", but this time the break comes before the
print:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
break
print(x)
The continue Statement
With the continue statement we can stop the current iteration of the loop, and
continue with the next:
Example
Do not print banana:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
continue
print(x)
The range() Function
To loop through a set of code a specified number of times, we can use
the range() function,
The range() function returns a sequence of numbers, starting from 0 by default,
and increments by 1 (by default), and ends at a specified number.
Example
Using the range() function:
for x in range(6):
print(x)
Example
Using the start parameter:
for x in range(2, 6):
print(x)
Increment the sequence with 3 (default is 1):
for x in range(2, 30, 3):
print(x)
Else in For Loop
The else keyword in a for loop specifies a block of code to be executed when the
loop is finished:
Example
Print all numbers from 0 to 5, and print a message when the loop has ended:
for x in range(6):
print(x)
else:
print("Finally finished!")
The else block will NOT be executed if the loop is stopped by a break statement.
for x in range(6):
if x == 3: break
print(x)
else:
print("Finally finished!")
#If the loop breaks, the else block is not executed.
The pass Statement
for loops cannot be empty, but if you for some reason have a for loop with no
content, put in the pass statement to avoid getting an error.
Example
for x in [0, 1, 2]:
pass