0% found this document useful (0 votes)
7 views

Programming While Loop Worksheet

Uploaded by

Jay Gori
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Programming While Loop Worksheet

Uploaded by

Jay Gori
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Programming While Loop Worksheet

Instructions:

1. Replace the comments with the appropriate code in your chosen programming language.

2. Run the code and observe the output.

3. Experiment with different conditions and values to understand how while loops work.

1. Basic While Loop:

a. Create a while loop that prints numbers from 1 to 5.

2. While Loop with Condition:

a. Create a while loop that prints even numbers from 2 to 10.

3. Infinite While Loop:

a. Create an infinite while loop. Be cautious with infinite loops!

4. While Loop with User Input:

a. Ask the user to enter a number.

b. Create a while loop that prints the square of that number until the square is greater than 100.

5. While Loop with Break Statement:

a. Create a while loop that prints numbers from 1 to 10.

b. Use a break statement to exit the loop when the loop variable is 5.

6. Nested While Loop:

a. Create a nested while loop that prints a multiplication table for numbers 2 to 4.

7. While Loop with Continue Statement:

a. Create a while loop that prints numbers from 1 to 7.

b. Use a continue statement to skip printing the number 4.


8. While Loop with Counter:

a. Create a while loop that counts the number of times it runs.

b. Print the count after the loop completes.

9. While Loop with String Concatenation:

a. Initialize an empty string.

b. Inside a while loop, concatenate the string with the loop variable (e.g., "Iteration: 1").

c. Repeat the process for 3 iterations.

10. While Loop with List:

a. Create a list of your favorite fruits.

b. Use a while loop to iterate over the list and print each fruit.

# 1. Basic While Loop

# a. Create a while loop that prints numbers from 1 to 5.

counter = 1

while counter <= 5:

print(counter)

counter += 1

# 2. While Loop with Condition

# a. Create a while loop that prints even numbers from 2 to 10.

number = 2

while number <= 10:

print(number)

number += 2

# 3. Infinite While Loop

# a. Create an infinite while loop. Be cautious with infinite loops!

# Uncomment the code below cautiously, and manually interrupt the execution.

# while True:
# print("This is an infinite loop!")

# 4. While Loop with User Input

# a. Ask the user to enter a number.

# b. Create a while loop that prints the square of that number until the square is greater than 100.

user_input = int(input("Enter a number: "))

square = user_input ** 2

while square <= 100:

print(square)

user_input = int(input("Enter a number: "))

square = user_input ** 2

# 5. While Loop with Break Statement

# a. Create a while loop that prints numbers from 1 to 10.

# b. Use a break statement to exit the loop when the loop variable is 5.

counter = 1

while counter <= 10:

print(counter)

if counter == 5:

break

counter += 1

# 6. Nested While Loop

# a. Create a nested while loop that prints a multiplication table for numbers 2 to 4.

outer_counter = 2

while outer_counter <= 4:

inner_counter = 1

while inner_counter <= 10:

print(f"{outer_counter} x {inner_counter} = {outer_counter * inner_counter}")

inner_counter += 1

outer_counter += 1
# 7. While Loop with Continue Statement

# a. Create a while loop that prints numbers from 1 to 7.

# b. Use a continue statement to skip printing the number 4.

counter = 1

while counter <= 7:

if counter == 4:

counter += 1

continue

print(counter)

counter += 1

# 8. While Loop with Counter

# a. Create a while loop that counts the number of times it runs.

# b. Print the count after the loop completes.

loop_count = 0

while loop_count < 5:

loop_count += 1

print(f"The loop ran {loop_count} times.")

# 9. While Loop with String Concatenation

# a. Initialize an empty string.

# b. Inside a while loop, concatenate the string with the loop variable (e.g., "Iteration: 1").

# c. Repeat the process for 3 iterations.

iteration_count = 1

result_string = ""

while iteration_count <= 3:

result_string += f"Iteration: {iteration_count} "

iteration_count += 1

print(result_string)
# 10. While Loop with List

# a. Create a list of your favorite fruits.

# b. Use a while loop to iterate over the list and print each fruit.

favorite_fruits = ["Apple", "Banana", "Orange", "Grapes", "Strawberry"]

index = 0

while index < len(favorite_fruits):

print(favorite_fruits[index])

index += 1

You might also like