Programming While Loop Worksheet
Programming While Loop Worksheet
Instructions:
1. Replace the comments with the appropriate code in your chosen programming language.
3. Experiment with different conditions and values to understand how while loops work.
b. Create a while loop that prints the square of that number until the square is greater than 100.
b. Use a break statement to exit the loop when the loop variable is 5.
a. Create a nested while loop that prints a multiplication table for numbers 2 to 4.
b. Inside a while loop, concatenate the string with the loop variable (e.g., "Iteration: 1").
b. Use a while loop to iterate over the list and print each fruit.
counter = 1
print(counter)
counter += 1
number = 2
print(number)
number += 2
# Uncomment the code below cautiously, and manually interrupt the execution.
# while True:
# print("This is an infinite loop!")
# b. Create a while loop that prints the square of that number until the square is greater than 100.
square = user_input ** 2
print(square)
square = user_input ** 2
# b. Use a break statement to exit the loop when the loop variable is 5.
counter = 1
print(counter)
if counter == 5:
break
counter += 1
# a. Create a nested while loop that prints a multiplication table for numbers 2 to 4.
outer_counter = 2
inner_counter = 1
inner_counter += 1
outer_counter += 1
# 7. While Loop with Continue Statement
counter = 1
if counter == 4:
counter += 1
continue
print(counter)
counter += 1
loop_count = 0
loop_count += 1
# b. Inside a while loop, concatenate the string with the loop variable (e.g., "Iteration: 1").
iteration_count = 1
result_string = ""
iteration_count += 1
print(result_string)
# 10. While Loop with List
# b. Use a while loop to iterate over the list and print each fruit.
index = 0
print(favorite_fruits[index])
index += 1