Interview Questions, Answers and Tutorials

Loops: While, For, Do-While, and for-each loops

Loops: While, For, Do-While, and for-each loops

Welcome to the world of loops! In this course, you’ll learn about a fundamental concept in programming: looping. Loops allow us to repeat actions multiple times, saving us from writing the same code over and over again. Think of it like telling a robot to do something repeatedly without getting tired!

What are Loops?

Imagine you have a task to write numbers from 1 to 10. You can either write each number one by one, or you can use a loop to do it for you. A loop is like a magical spell that tells the computer to do something again and again until a certain condition is met.

Types of Loops:

There are four types of loops we’ll learn about: While Loops, For Loops, do while loops, and for each loops. Sounds exciting? Let’s jump right in!

1. While Loops: Think of a while loop like a friendly robot who keeps doing something for you as long as a condition is true. Here’s how it works in Python:

# Example of a while loop
count = 0
while count < 5:  # As long as count is less than 5
    print("Count is", count)
    count += 1  # Increment count by 1

Practice Question: Write a while loop that prints numbers from 1 to 10.

Solution:

number = 1
while number <= 10:
    print(number)
    number += 1

2. For Loops: For loops are like magic wands! They help you do something for a specific number of times. Check out this example:

# Example of a for loop
for i in range(5):  # Goes from 0 to 4
    print("Current number is", i)

Practice Question: Write a for loop that prints even numbers from 2 to 10.

Solution:

for number in range(2, 11, 2):  # Starts from 2, ends at 10, and increments by 2
    print(number)

3. do while Loops: Okay, imagine you have a task and you want to do it at least once, then check if you should do it again. That’s what a do-while loop does! But Python doesn’t have a built-in do-while loop. However, we can create a similar effect using a while loop like this:

# Example of a do while loop
count = 0
while True:  # This will always be true
    print("Count is", count)
    count += 1
    if count >= 5:
        break  # Stop the loop if count is greater than or equal to 5

Practice Question: Create a do-while loop that asks the user to enter their age until they enter a valid age (between 1 and 100).

Solution:

while True:
    age = int(input("Enter your age: "))
    if 1 <= age <= 100:
        print("Valid age entered!")
        break
    else:
        print("Please enter a valid age between 1 and 100.")

4. For Each Loops: For each loop is like a magic wand that helps you go through each item in a list or collection. Let’s see how it works:

# Example of a for each loop
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print("Current fruit is", fruit)

Practice Question: Create a list of your favorite colors and use a for each loop to print each color.

Solution:

favorite_colors = ["blue", "green", "red", "yellow"]
for color in favorite_colors:
    print(color)

Great job! You’ve now mastered the art of loops in Python! Loops are incredibly powerful tools that help us save time and make our code efficient. Keep practicing and exploring, and soon you’ll be looping like a pro! If you have any questions, feel free to ask. Happy coding! 🌟