Post

Python - Control Statements

Control Statements:

  1. Conditional Statements
Conditional StatementDescription
ifAn if statement is used to test an expression for truthiness and execute some code if the expression is true
elifAn elif (else if) statement is used to chain multiple conditions in an if statement. It executes its block of code if its condition is true and all previous conditions have been false
elseAn else statement catches anything which isn’t caught by the preceding conditions
if...elseAn if...else statement provides an alternate block of code that is executed if the if condition is false
if...elif...elseAn if...elif...else statement allows you to chain multiple conditions and provide an alternate block of code that is executed if all conditions are false
  1. Looping Statements
Looping StatementDescription
forA for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string)
whileA while loop can execute a set of statements as long as a condition is true
nested loopsA nested loop is a loop inside a loop. The for loop and the while loop can be nested
breakThe break statement can stop the loop before it has looped through all the items
continueThe continue statement can stop the current iteration of the loop, and continue with the next
passThe pass statement is used as a placeholder for future code. When the pass statement is executed, nothing happens, but you avoid getting an error when empty code is not allowed

Examples:

  1. If elif else Statement:

    1
    2
    3
    4
    5
    6
    7
    8
    
     age = 18
    
     if age < 13:
         print("You are a child.")
     elif 13 <= age < 18:
         print("You are a teenager.")
     else:
         print("You are an adult.")
    
  2. For Loop :

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
     # List of numbers
     numbers = [1, 2, 3, 4, 5]
    
     # variable to store the sum
     sum = 0
    
     # iterate over the list
     for num in numbers:
         sum = sum + num
    
     # print the sum
     print("The sum is", sum)
    
  3. While Loop:

    1
    2
    3
    4
    5
    6
    7
    
     # initial value of counter
     counter = 0
    
     # while loop
     while counter < 5:
         print("Counter is", counter)
         counter = counter + 1
    
  4. Nested Loops:

    1
    2
    3
    4
    5
    6
    7
    
     # Outer loop
     for i in range(3):
         print("Outer loop iteration:", i)
            
         # Inner loop
         for j in range(3):
             print("   Inner loop iteration:", j)
    
This post is licensed under CC BY 4.0 by the author.