Control flow and loops are fundamental concepts in Python programming. They allow you to control the execution of your code and iterate over data structures. This blog post will cover the basics of control flow, loops, and how to use the break and continue commands, along with some real-time examples.
Control Flow in Python
Control flow refers to the order in which individual statements, instructions, or function calls are executed or evaluated. Python supports several control flow statements:
if Statement:
age = 18
if age >= 18:
print("You are an adult.")if-else Statement:
age = 16
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")if-elif-else Statement:
score = 85
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
else:
print("Grade: C or below")Loops in Python
Loops allow you to execute a block of code repeatedly. Python supports two types of loops: for and while.
for Loop:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)while Loop:
count = 0
while count < 5:
print(count)
count += 1The break Command
The break command is used to exit a loop prematurely when a certain condition is met.
The break Command
The break command is used to exit a loop prematurely when a certain condition is met.
for number in range(10):
if number == 5:
break
print(number)Output:
0 1 2 3 4
The continue command
The continue command is used to skip the current iteration of the loop and continue with the next iteration.
for number in range(10):
if number % 2 == 0:
continue
print(number)
output:
1 3 5 7 9
Real-Time Examples
Example 1: Searching for a Value in a List
Suppose you are searching for a specific value in a list. You can use a loop with the break command to exit the loop once the value is found.
numbers = [1, 3, 5, 7, 9, 11, 13, 15]
search_value = 7
for number in numbers:
if number == search_value:
print(f"Value {search_value} found in the list.")
break
else:
print(f"Value {search_value} not found in the list.")Example 2: Skipping Specific Values
If you want to process a list of numbers but skip certain values, you can use the continue command.
numbers = range(1, 11)
for number in numbers:
if number % 3 == 0:
continue
print(number)Output:
1 2 4 5 7 8 10
Example 3: User Input Validation
A real-time example of using while loop with break for input validation.
while True:
user_input = input("Enter a positive number: ")
if user_input.isdigit() and int(user_input) > 0:
print(f"Thank you! You entered {user_input}.")
break
else:
print("Invalid input. Please try again.")Example 4: Summing Numbers Until Zero is Entered
total = 0
while True:
number = int(input("Enter a number (0 to stop): "))
if number == 0:
break
total += number
print(f"The total sum is {total}.")Conclusion
Understanding control flow and loops, along with break and continue, is essential for writing efficient and effective Python code. These constructs allow you to manage the flow of your programs and handle repetitive tasks with ease. By mastering these concepts, you’ll be able to solve complex problems and write more sophisticated programs.
Stay tuned for more tutorials to deepen your understanding of Python and other programming languages. Happy coding!




