Basics - Control Statement

What is a Control Statement in Python?

In Python, a control statement is used to control the flow of execution of the program based on certain conditions or repetitions. These statements help in making decisions, looping through data, or altering the normal sequential flow of a program.

Types of Control Statements in Python:

Conditional Statements – used for decision-making:

  • if
  • if...else
  • if...elif...else

Example:

age = 18
if age >= 18:
    print("You are an adult.")
else:
    print("You are a minor.")

Looping Statements – used for repeating a block of code:

  • for loop
  • while loop

Example:

for i in range(5):
    print(i)

Loop Control Statements – used inside loops to control iteration:

  • break → exits the loop prematurely
  • continue → skips the current iteration
  • pass → a placeholder that does nothing

Example (break and continue):

for i in range(10):
    if i == 5:
        break  # exit loop when i is 5
    if i % 2 == 0:
        continue  # skip even numbers
    print(i)

In Simple Terms:

Control statements let your program:

  • Decide what to do next (if)
  • Do something many times (for, while)
  • Interrupt or skip actions (break, continue, pass)

Exercises:

Exercise 1: Odd or Even Checker

Write a Python program that reads an integer from the user and prints whether it’s “Even” or “Odd”.

Enter a number: 12  
Output: Even

See Solution

Exercise 2: Grade Classifier

Write a Python program that classifies a student’s grade based on the following rules:

Grade < 60: Fail

Grade ≥ 90: Excellent

Grade 80-89: Very Good

Grade 70-79: Good

Grade 60-69: Satisfactory

Enter grade: 85  
Output: Very Good

See Solution

Exercise 3: Sum of Positive Numbers

Write a Python program that continuously asks the user to enter positive numbers. If the user enters a negative number or zero, the loop stops, and the program prints the sum of all positive numbers entered.

Enter a number: 10  
Enter a number: 5  
Enter a number: 3  
Enter a number: -2  

Sum: 18

See Solution

Exercise 4: Factorial Calculator

Write a Python program that computes the factorial of a number entered by the user using a loop.
Hint: Factorial of n (n!) = n × (n-1) × (n-2) × … × 1

Enter a number: 5  
Factorial: 120

See Solution

Exercise 5: Multiplication Table

Write a Python program that displays the multiplication table of a number entered by the user, from 1 to 10, using a loop.

Enter a number: 7

7 × 1 = 7  
7 × 2 = 14  
...  
7 × 10 = 70

See Solution

Leave a Reply

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