In Python, control flow refers to the order in which statements are executed in a program. Control flow allows you to make decisions and execute certain code blocks based on conditions. There are several control flow statements in Python, including if-else statements, while loops, and for loops. Let's go through each of these with examples:
1. if-else statement:
The if-else statement allows you to execute a block of code if a certain condition is true, and another block of code if the condition is false.
```python
x = 10
if x > 5:
print("x is greater than 5")
else:
print("x is not greater than 5")
# Output: x is greater than 5
```
2. Nested if-else statements:
You can also use nested if-else statements to handle multiple conditions.
```python
x = 7
if x > 5:
print("x is greater than 5")
elif x == 5:
print("x is equal to 5")
else:
print("x is less than 5")
# Output: x is greater than 5
```
3. while loop:
The while loop allows you to repeatedly execute a block of code as long as a certain condition is true.
```python
count = 1
while count <= 5:
print("Count is:", count)
count += 1
# Output:
# Count is: 1
# Count is: 2
# Count is: 3
# Count is: 4
# Count is: 5
```
4. for loop:
The for loop is used to iterate over elements in a sequence, such as lists, tuples, or strings.
```python
fruits = ["apple", "banana", "orange"]
for fruit in fruits:
print("I like", fruit)
# Output:
# I like apple
# I like banana
# I like orange
```
5. break and continue statements:
- The `break` statement is used to exit a loop prematurely when a certain condition is met.
```python
numbers = [1, 2, 3, 4, 5]
for num in numbers:
if num == 3:
break
print(num)
# Output:
# 1
# 2
```
- The `continue` statement is used to skip the rest of the current iteration and move to the next iteration of the loop.
```python
numbers = [1, 2, 3, 4, 5]
for num in numbers:
if num == 3:
continue
print(num)
# Output:
# 1
# 2
# 4
# 5
```
6. else with loops:
In Python, you can use the `else` statement with loops to specify a block of code that should be executed when the loop completes normally without encountering a `break` statement.
```python
numbers = [1, 2, 3, 4, 5]
for num in numbers:
print(num)
else:
print("Loop completed without encountering a break statement.")
# Output:
# 1
# 2
# 3
# 4
# 5
# Loop completed without encountering a break statement.
```
These control flow statements are essential for making decisions, iterating over data, and controlling the flow of execution in Python programs. They provide the flexibility needed to handle various scenarios and implement complex logic.
"Welcome to Bhuj Edu Info – your complete education guide for Bhuj and Kutch. Whether you are a student, parent, or teacher, here you will find the latest information on schools, colleges, admissions, university results, scholarships, and government education schemes. We also share updates on competitive exams, career opportunities, and student resources to help you achieve success in your academic and professional journey. With accurate, reliable, and timely updates
Comments
Post a Comment