Control Flow Structures in Python: if, for, and while Loops
Control flow structures are an important part of any programming language. In Python, three of the most commonly used control flow structures are the if statement, the for loop, and the while loop. In this blog post, we will cover the basics of these control flow structures, including:
- The
ifstatement and its use in controlling program flow based on conditions - The
forloop and its use in iterating over a sequence of elements - The
whileloop and its use in executing code repeatedly while a condition is true
The if Statement
The if statement is used to execute a block of code if a condition is true. Here is the basic syntax of the if statement:
if condition:
# Code to execute if the condition is true
Here is an example of the if statement in action:
x = 5
if x > 0:
print('x is positive')
In this example, the print() function only executes if the condition x > 0 is true.
The if-else Statement
The if-else statement is used to execute one block of code if a condition is true, and another block of code if the condition is false. Here is the basic syntax of the if-else statement:
if condition:
# Code to execute if the condition is true
else:
# Code to execute if the condition is false
Here is an example of the if-else statement in action:
x = -5
if x > 0:
print('x is positive')
else:
print('x is non-positive')
In this example, the print() function executes the second block of code because the condition x > 0 is false.
The if-elif-else Statement
The if-elif-else statement is used to execute one of several blocks of code based on multiple conditions. Here is the basic syntax of the if-elif-else statement:
if condition1:
# Code to execute if condition1 is true
elif condition2:
# Code to execute if condition1 is false and condition2 is true
else:
# Code to execute if both condition1 and condition2 are false
Here is an example of the if-elif-else statement in action:
x = 0
if x > 0:
print('x is positive')
elif x < 0:
print('x is negative')
else:
print('x is zero')
In this example, the print() function executes the third block of code because the condition x > 0 is false and the condition x < 0 is also false, so the code in the else block is executed.
The for Loop
The for loop is used to iterate over a sequence of elements. Here is the basic syntax of the for loop:
for variable in sequence:
# Code to execute for each element in the sequence
Here is an example of the for loop in action:
fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
print(fruit)
In this example, the print() function is executed for each element in the fruits list.
The range() Function
The range() function is commonly used with the for loop to generate a sequence of numbers. Here is the basic syntax of the range() function:
range(start, stop, step)
Here are some examples of the range() function:
for i in range(5):
print(i) # Output: 0 1 2 3 4
for i in range(1, 6):
print(i) # Output: 1 2 3 4 5
for i in range(0, 10, 2):
print(i) # Output: 0 2 4 6 8
In the first example, the range() function generates a sequence of numbers from 0 to 4, which are printed to the console. In the second example, the range() function generates a sequence of numbers from 1 to 5, which are printed to the console. In the third example, the range() function generates a sequence of even numbers from 0 to 8, which are printed to the console.
The while Loop
The while loop is used to execute a block of code repeatedly while a condition is true. Here is the basic syntax of the while loop:
while condition:
# Code to execute while the condition is true
Here is an example of the while loop in action:
i = 0
while i < 5:
print(i)
i += 1
In this example, the print() function is executed for each value of i from 0 to 4.
The break Statement
The break statement is used to exit a loop prematurely. Here is an example of the break statement in a while loop:
i = 0
while i < 5:
print(i)
i += 1
if i == 3:
break
In this example, the print() function is executed for each value of i from 0 to 2, because the break statement is encountered when i == 3, causing the loop to terminate prematurely.
The continue Statement
The continue statement is used to skip the current iteration of a loop and move on to the next one. Here is an example of the continue statement in a while loop:
i = 0
while i < 5:
i += 1
if i == 3:
continue
print(i)
In this example, the print() function is executed for each value of i from 1 to 5, except for the value of 3, which is skipped because of the continue statement.
Conclusion
Control flow structures are an essential part of programming in Python. The if statement is used to control program flow based on conditions, the for loop is used to iterate over a sequence of elements, and the while loop is used to execute code repeatedly while a condition is true. Understanding these control flow structures is crucial for writing efficient and effective Python code.
