Skip to main content

Control flow in python with example

 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.

Comments

Popular posts from this blog

Gujarati Keyboard layout (terafont-varun), Computer Short cut key, Tally short cut key

Word , Excel , Power Point Shortcut Key in Gujarati

Terafont-Varun (Gujarati Typing) Keyboard Layout by "Sama Soyab"

  For Gujarati Typing : Required : Terafont-Varun Font  After Successfully Installed Terafont Varun Open Any Text Editor or any program. Select Font Terafont-Varun -> Ok For more detail please watch below video. Search Topics : Learn terafont varun, Learn terafont chandan, Learn terafont gujarati to english translation, Learn terafont varun keyboard, Learn terafont converter, Learn terafont varun zip, Learn terafont keyboard, Learn terafont kinnari, Learn terafont akash, Learn terafont aakash, Learn terafont akash ttf, Learn terafont aakash gujarati download, Learn terafont akash keyboard, Learn terafont akash download for windows 10, Learn terafont akash font download, Learn terafont arun, Learn terafont border, Learn terafont chandan keyboard, Learn terafont-chandan font, Learn tera font chandana, Learn convert terafont to shruti, Learn convert terafont varun to shruti, Learn terafont varun chart, Learn terafont download, Learn terafont download for windows 10, Learn tera...