NEW
Font size
WorksheetsUnderstanding Python While Loops
Total questions: 15
Worksheet time: 8mins
What is the basic syntax of a while loop in Python?
repeat condition: # code to execute
for condition: # code to execute
do while condition: # code to execute
while condition: # code to execute
How do you start a while loop in Python?
while condition:
do while condition:
repeat until condition:
for condition in iterable:
What keyword is used to end a while loop prematurely?
exit
return
continue
break
What happens if the condition in a while loop is always true?
The program enters an infinite loop.
The program skips the loop entirely.
The program terminates immediately.
The loop runs a fixed number of times.
Can you use a while loop without an initial variable?
You need a counter variable
No, it's not possible
Only with a defined condition
Yes
What is the difference between a while loop and a for loop?
A while loop iterates a fixed number of times, while a for loop continues until a condition is met.
A while loop is used for iterating over a range, while a for loop checks the condition before each iteration.
A while loop checks the condition before each iteration, while a for loop is used for iterating over a range or collection with a defined structure.
A while loop is more efficient than a for loop, which is slower in execution.
How do you ensure that a while loop will eventually stop?
Ensure the loop condition will eventually become false by modifying a variable within the loop.
Avoid modifying any variables inside the loop.
Use a break statement without any conditions.
Keep the loop condition constant throughout the execution.
What is a common use case for a while loop in programming?
To iterate over a list of items.
To execute a block of code as long as a condition is true.
To define a function that returns a value.
To create a static array of values.
What will happen if you forget to update the variable in a while loop?
The loop will run indefinitely.
The loop will execute only once.
The loop will execute a fixed number of times.
The program will throw an error.
What is the output of the following code?
x = 1
while x < 4:
print(x)
x += 1
1
123
1234
234
What does this code print?
i = 5
while i > 2:
print("Hi")
i -= 1
Hi
HiHi
HiHiHi
nothing
What is the output?
num = 0
while num <= 3:
print(num)
num = num + 2
0 1 2 3
0 2 4
0 2
1 3
What does the loop print?
count = 3
while count != 0:
print(count)
count = count - 1
3 2 1 0
2 1 0
3 2 1
1 2 3
Find the output:
x = 1
while x < 10:
x = x * 2
print(x)
2
4
8
16
what will be the output :
i = 10
while i > 8:
print(i)
i -= 1
10 9 8
10 9
9 8
9 8 7
