Different types of loops.
Explore the for loop and the while loop.
1. Explain when to use these different types of loops.
2. How do you include a 'loop' structure programming in Python?
3. How do you control a loop?
4. Provide sample code to demonstrate loops
. When to Use for vs. while Loops:
forLoop:- Use a
forloop when you know in advance how many times you need to iterate. - It's ideal for iterating over a sequence (like a list, tuple, string, or range) or any iterable object.
- Examples:
- Processing each item in a list.
- Repeating an action a specific number of times.
- Traversing the characters in a string.
- Use a
whileLoop:- Use a
whileloop when you need to repeat a block of code until a certain condition is met. - The number of iterations is not known beforehand.
- Examples:
- Repeating an action until a user enters a specific input.
- Continuing a process until a calculation reaches a desired value.
- Creating a game loop that runs until the player loses.
- Use a
2. How to Include Loop Structures in Python:
forLoop:- The
forloop uses theforkeyword, followed by a variable name, theinkeyword, and the iterable object. - Syntax:
for variable in iterable:
- The
whileLoop:- The
whileloop uses thewhilekeyword, followed by a condition. - Syntax:
while condition:
- The
3. How to Control a Loop:
breakStatement:- The
breakstatement immediately terminates the loop, regardless of whether the loop's condition is still true. - It's often used to exit a loop when a specific condition is met within the loop's body.
- The
continueStatement:- The
continuestatement skips the rest of the current iteration and moves to the next iteration of the loop. - It's useful for skipping certain iterations based on specific conditions.
- The
- Loop Control Variable:
- In a
whileloop, the condition that controls the loop must eventually become false. This is typically done by modifying a variable that is part of the conditional statement. - In a
forloop, the loop control is managed by the iterable object.
- In a
4. Sample Code Demonstrations:
Python
# for loop example: iterating over a list
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
# for loop example: using range() to repeat a task
for i in range(5): # range(5) generates numbers 0, 1, 2, 3, 4
print("Iteration:", i)
# while loop example: repeating until a condition is met
count = 0
while count < 5:
print("Count:", count)
count += 1
# while loop example: with user input and break
while True:
user_input = input("Enter 'exit' to quit: ")
if user_input.lower() == "exit":
break
print("You entered:", user_input)
# for loop example: with continue
for i in range(10):
if i % 2 == 0: # Skip even numbers
continue
print("Odd number:", i)
# while loop example with break
number = 0
while number <10:
number += 1
if number == 5:
break;
print(number)
Key takeaways from the examples:
- The
forloop is excellent for iterating through collections. - The
range()function is very useful to create a sequence of numbers for aforloop. - The
whileloop continues as long as its condition remains true. breakandcontinueprovide fine-grained control over loop execution.- It is critical to be sure that
whileloops will eventually end.