2.3 - Control Flow Statements
Control flow statements are an essential part of any programming language, including Python. They allow you to control the execution flow of your program based on certain conditions or loops. In this guide, we will cover the various control flow statements available in the latest version of Python (as of September 2021) and provide detailed explanations and examples for each of them. Please note that this guide assumes you have a basic understanding of Python syntax and programming concepts.
2.3.1 - Conditional Statements
Conditional statements allow you to execute different blocks of code based on specific conditions. Python provides several types of conditional statements:
2.3.1.1 - if Statement
The if
statement is used to execute a block of code only if a certain condition is true.
Syntax:
if condition:
# code block
Example:
x = 10
if x > 0:
print("x is positive")
2.3.1.2 - if-else Statement
The if-else
statement allows you to execute different code blocks based on the condition's result. If the condition is true, the code block under the if
statement is executed. Otherwise, the code block under the else
statement is executed.
Syntax:
if condition:
# code block for true condition
else:
# code block for false condition
Example:
x = -5
if x > 0:
print("x is positive")
else:
print("x is non-positive")
2.3.1.3 - if-elif-else Statement
The if-elif-else
statement is used when you have multiple conditions to check. It allows you to execute different code blocks based on the first condition that evaluates to true.
Syntax:
if condition1:
# code block for condition1
elif condition2:
# code block for condition2
else:
# code block for false conditions
Example:
x = 0
if x > 0:
print("x is positive")
elif x < 0:
print("x is negative")
else:
print("x is zero")
2.3.1.4 - Nested if Statements
You can nest if
statements within other if
statements to handle more complex conditions.
Syntax:
if condition1:
if condition2:
# code block for condition1 and condition2
else:
# code block for condition1 and not condition2
else:
# code block for false condition1
Example:
x = 10
y = 5
if x > 0:
if y > 0:
print("Both x and y are positive")
else:
print("x is positive, but y is non-positive")
else:
print("x is non-positive")
2.3.1.5 - Ternary Operator (Conditional Expression)
The ternary operator allows you to write shorter versions of if-else statements in a single line.
Syntax:
expression_if_true if condition else expression_if_false
Example:
x = 10
message = "Positive" if x > 0 else "Non-positive"
print(message)
2.3.2 - Looping Statements
Looping statements are used to repeat a block of code multiple times. Python provides different types of looping statements:
2.3.2.1 - while Loop
The while
loop is used to repeat a block of code as long as a condition is true.
Syntax:
while condition:
# code block
Example:
count = 0
while count < 5:
print("Count:", count)
count += 1
2.3.2.2 - for Loop
The for
loop is used to iterate over a sequence (such as a list, tuple, or string) or other iterable objects.
Syntax:
for item in iterable:
# code block
Example:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
2.3.2.3 - range() Function
The range()
function is often used with for
loops to generate a sequence of numbers.
Syntax:
range(start, stop, step)
Example:
for i in range(1, 10, 2):
print(i)
2.3.2.4 - Nested Loops
You can nest loops within other loops to perform more complex iterations.
Syntax:
for item1 in iterable1:
for item2 in iterable2:
# code block
Example:
for i in range(1, 4):
for j in range(1, 4):
print(i * j)
2.3.2.5 - Loop Control Statements
Loop control statements allow you to control the execution flow within loops.
2.3.2.5.1 - break Statement
The break
statement is used to exit a loop prematurely.
Syntax:
while condition:
if condition:
break
Example:
count = 0
while count < 5:
if count == 3:
break
print("Count:", count)
count += 1
2.3.2.5.2 - continue Statement
The continue
statement is used to skip the rest of the code block and move to the next iteration of the loop.
Syntax:
while condition:
if condition:
continue
# code block
Example:
count = 0
while count < 5:
count += 1
if count == 3:
continue
print("Count:", count)
2.3.2.5.3 - pass Statement
The pass
statement is a placeholder that does nothing. It is used when you need a statement syntactically but don't want any code to execute.
Syntax:
if condition:
pass
Example:
x = 10
if x > 0:
pass # placeholder for future code
else:
print("x is non-positive")
2.3.3 - Summary
Control Flow Statement | Keyword | Description |
---|---|---|
if Statement | if | Executes a block of code if a condition is true. |
if-else Statement | if-else | Executes a block of code if a condition is true, otherwise executes another block of code. |
if-elif-else Statement | if-elif-else | Executes different blocks of code based on multiple conditions. |
Nested if Statements | if (nested) | Allows nesting if statements to handle complex conditions. |
Ternary Operator (Conditional Expression) | expression_if_true if condition else expression_if_false | Provides a shorthand notation for if-else. |
while Loop | while | Repeats a block of code as long as a condition is true. |
for Loop | for | Iterates over a sequence or iterable object. |
range() Function | range() | Generates a sequence of numbers for looping. |
Nested Loops | Nested for or while loops | Nesting loops for performing complex iterations. |
break Statement | break | Exits a loop prematurely. |
continue Statement | continue | Skips the rest of the code block and moves to the next iteration. |
pass Statement | pass | Placeholder statement for future code. |
By mastering these control flow statements, you can make your Python programs more dynamic and flexible, allowing them to respond to different conditions and iterate over sequences efficiently.