Link for Control Flow Statements Programs in Python

  • Control flow statements in Python allow us to manage the order in which our code is executed.

Types of Control Flow Statements in Python

They are of three types –

  • Decision/Conditional Statements (if, else, elif)
  • Looping Statements (for, while)
  • Control Flow Altering/Jumping Statements (break, continue, pass)
1. Conditional Statements
  • Conditional statements allow us to execute code blocks based on certain conditions.
  • They are of the following types –
(a) if Statement
    • The if statement evaluates a condition. If the condition is True, the indented code block is executed.
    • For example – 
x = 10
if x > 5:
    print(“x is greater than 5”)
(b) if-else Statement
    • The ifelse statement provides an alternative block to execute if the condition is False.
    • For example –
x = 3
if x > 5:
    print(“x is greater than 5”)
else:
    print(“x is less than or equal to 5”)
(c) if-elif-else Statement
    • The ifelifelse structure allows checking multiple conditions.
    • In this, If one condition is True, its corresponding code block is executed.
    • For example –
x = 5
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”)
(d) Nested if Statements
    • We can nest if statements inside other if blocks to create more complex conditions.
    • For example –
x = 10
if x > 5:
    if x < 15:
        print(“x is between 5 and 15”)
2. Looping Statements
  • The concept of Loops allows us to execute a block of code multiple times, based on a condition or iterating over a sequence.
  • They are of the following types –
(i) for Loop
    • The for loop is used to iterate over a sequence (such as a list, tuple, or string) or any iterable object.
    • For example –
for i in [1, 2, 3]:
    print(i)
    • We can also loop through a range of numbers using the range() method.
for i in range(5):
    print(i)  # Output:  0, 1, 2, 3, 4
(ii) while Loop
    • The while loop continues to execute a block of code as long as the condition is True.
    • For example –
count = 0
while count < 5:
    print(count)
    count=count + 1
3. Control Flow Altering/Jumping Statements
  • These statements are used to modify the normal flow of loops or conditionals.
  • They are of the following types –
(I) break Statement
    • The break statement is used to exit a loop prematurely when a condition is met.
    • For example –
for i in range(10):
    if i == 5:
        break  # Exit loop when i is 5
    print(i)
(II) continue Statement
    • The continue statement skips the rest of the current iteration and moves on to the next iteration.
    • For example –
for i in range(5):
    if i == 2:
        continue  # Skip iteration when i is 2
    print(i)
(III) pass Statement
    • The pass statement is a placeholder that does nothing.
    • It‘s used where a statement is syntactically required but no action is needed.
    • For example – 
for i in range(5):
    if i == 2:
        pass  # Placeholder, does nothing
    print(i)

Loading

Categories: Python Theory

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.