Control Flow Statements
Control flow statements are used to control the flow of execution in a program. They allow you to make decisions, repeat actions, and break out of loops based on certain conditions. In this tutorial, we'll cover the following control flow statements in Python:
- Conditional Statements:
if,elif, andelsestatements - Looping Statements:
forandwhileloops - Control Statements:
break,continue, andpassstatements
Let's dive into each of these control flow statements and see how they work in Python.
Conditional Statements
Conditional statements are used to execute different blocks of code based on certain conditions. In Python, we have the following conditional statements:
ifstatement: Executes a block of code if a specified condition isTrue.elifstatement: Executes a block of code if the precedingiforelifcondition(s) areFalseand the current condition isTrue.elsestatement: Executes a block of code if all precedingifandelifconditions areFalse.
Here's an example of using conditional statements in Python:
x = 10
if x > 0:
print("x is positive")
elif x < 0:
print("x is negative")
else:
print("x is zero")x is positiveIn the example above, the if statement checks if x is greater than 0. If the condition is True, it prints "x is positive". If the condition is False, it moves to the elif statement and checks if x is less than 0. If the condition is True, it prints "x is negative". If both conditions are False, it executes the else statement and prints "x is zero".
Looping Statements
Looping statements are used to repeat a block of code multiple times. In Python, we have the following looping statements:
forloop: Executes a block of code for each item in a sequence (e.g., a list, tuple, or string).whileloop: Executes a block of code as long as a specified condition isTrue.
Here's an example of using looping statements in Python:
For Loop
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)apple
banana
cherryWhile Loop
i = 0
while i < 5:
print(i)
i += 10
1
2
3
4In the example above, the for loop iterates over each item in the fruits list and prints the item.
Control Statements
Control statements are used to alter the flow of execution in a program. In Python, we have the following control statements:
breakstatement: Terminates the loop it is in and transfers control to the statement immediately following the loop.continuestatement: Skips the rest of the code inside a loop for the current iteration and moves to the next iteration.passstatement: Does nothing and acts as a placeholder when a statement is required syntactically but you don't want any code to execute.
Here's an example of using control statements in Python:
for i in range(5):
if i == 3:
break
elif i == 1:
continue
else:
pass
print(i)0
2In the example above, the for loop iterates over the numbers 0 to 4. When i is 3, the break statement terminates the loop. When i is 1, the continue statement skips the rest of the code inside the loop for that iteration. The pass statement does nothing and allows the loop to continue executing.
Conclusion
Control flow statements are essential for writing dynamic and flexible programs in Python. By using conditional statements, looping statements, and control statements, you can create programs that respond to different conditions, repeat actions, and control the flow of execution. Practice using these control flow statements in your Python programs to become more proficient in programming. Happy coding!
Resources
- Python Documentation: Control Flow Statements (opens in a new tab)
- Real Python: Python Control Flow Statements (opens in a new tab)
- Programiz: Python if...elif...else Statement (opens in a new tab)
- Programiz: Python for Loop (opens in a new tab)
- Programiz: Python while Loop (opens in a new tab)
- Programiz: Python break, continue, and pass Statements (opens in a new tab)
- W3Schools: Python Control Flow (opens in a new tab)