Control Structures in Python
Control Structures in Python
Control structures determine the flow of execution of the program. They include:
- Sequential Control: The default mode where statements are executed one after another.
- Decision-Making Statements: These allow the program to take different paths based on conditions.
- Looping Statements: These allow the program to execute a block of code multiple times.
Decision-Making Statements
if Statement:
- Definition: In Python, an
if
statement is used to execute a block of code only if a specified condition is true. It’s a fundamental control structure that allows you to make decisions in your code. Here’s a basic overview - Syntax:
if condition: # block of code
- Flowchart:
- Example:
number = 10 if number > 5: print("Number is greater than 5")
- Definition: In Python, an
if-else Statement:
- Definition: An
if-else
statement in Python allows you to execute one block of code if a condition is true and another block if the condition is false. It’s a fundamental control structure for decision-making in your code. - Syntax:
if condition: # block of code if condition is true else: # block of code if condition is false
- Flowchart:
- Example:
number = 3 if number > 5: print("Number is greater than 5") else: print("Number is not greater than 5")
- Definition: An
if-elif-else Statement:
- Definition: An
if-elif-else
statement in Python allows you to check multiple conditions and execute different blocks of code based on which condition is true. This is useful when you have more than two possible outcomes. - Syntax:
if condition1: # block of code if condition1 is true elif condition2: # block of code if condition2 is true else: # block of code if none of the conditions are true
- Flowchart:
- Example:
number = 7 if number > 10: print("Number is greater than 10") elif number > 5: print("Number is greater than 5 but less than or equal to 10") else: print("Number is 5 or less")
- Definition: An
Nested if-else Statement:
- Definition: A nested if-else statement in Python is an if-else statement inside another if or else block. This structure allows you to test multiple conditions and execute different blocks of code based on those conditions.
- Syntax:
if condition1:
# Executes when condition1 is True if condition2: # Executes when condition2 is True print("Both condition1 and condition2 are True") else: # Executes when condition2 is False print("Condition1 is True but condition2 is False") else: # Executes when condition1 is False
if condition3: # Executes when condition3 is True print("Condition3 is True but condition1 is False")
")
else: # Executes when condition3 is False print("Both condition1 and condition3 are False - Flowchart:
- Example:
condition1 = True # You can change this to False to test different scenarios condition2 = False # You can change this to True to test different scenarios condition3 = True # You can change this to False to test different scenarios if condition1: # Executes when condition1 is True if condition2: # Executes when condition2 is True print("Both condition1 and condition2 are True") else: # Executes when condition2 is False print("Condition1 is True but condition2 is False") else: # Executes when condition1 is False if condition3: # Executes when condition3 is True print("Condition3 is True but condition1 is False") else: # Executes when condition3 is False print("Both condition1 and condition3 are False")
- Definition: A nested if-else statement in Python is an if-else statement inside another if or else block. This structure allows you to test multiple conditions and execute different blocks of code based on those conditions.
Looping Statements
for Loop:
- Definition: A
for
loop in Python is used to iterate over a sequence (like a list, tuple, dictionary, set, or string) and execute a block of code for each item in the sequence. It’s a powerful tool for repetitive tasks. - Syntax:
for variable in iterable: # block of code
- Flowchart:
- Example:
for i in range(5): print(i)
- Definition: A
while Loop:
- Definition: A
while
loop in Python repeatedly executes a block of code as long as a specified condition is true. It’s useful for situations where you don’t know in advance how many times you’ll need to iterate. - Syntax:
while condition: # block of code
- Flowchart:
- Example:
count = 0 while count < 5: print(count) count += 1
- Definition: A
Nested Loops:
- Definition: A nested loop in Python is a loop inside the body of another loop. The “inner loop” will be executed completely for each iteration of the “outer loop.” This structure is useful for working with multi-dimensional data structures, such as lists of lists, or for tasks that require multiple levels of iteration.
- Syntax:
for variable1 in iterable1: for variable2 in iterable2: # block of code
- Flowchart:
- Example:
for i in range(3): for j in range(2): print(f"i: {i}, j: {j}")
Control Statements:
break
Statement
The break
statement is used to exit a loop prematurely when a certain condition is met.
Syntax:
for/while loop:
# statement(s)
if condition:
break
# statement(s)
Example:
for i in range(10):
if i == 5:
break
print(i)
Output:
0
1
2
3
4
In this example, the loop terminates when i
equals 5.
continue
Statement
The continue
statement is used to skip the current iteration of the loop and proceed to the next iteration.
Syntax:
for/while loop:
# statement(s)
if condition:
continue
# statement(s)
Example:
for i in range(10):
if i == 5:
continue
print(i)
Output:
0
1
2
3
4
6
7
8
9
In this example, the loop skips the iteration when i
equals 5.
Combined Example
Here’s a short program that uses both break
and continue
statements:
for i in range(10):
if i == 3:
continue # Skip the iteration when i is 3
if i == 7:
break # Exit the loop when i is 7
print(i)
Output:
0
1
2
4
5
6
In this program, the loop skips printing 3
and stops entirely when it reaches 7
.
Comments
Post a Comment