Loops in Programming
שאלות 10
מפתח התשובות מוצג למורים מחוברים.
-
-
-
Identifying an infinite loop
Which of the following loops never terminates?
for i in range(10): print(i)
for i in range(10, 0): print(i)
i = 0; while i < 10: print(i)
i = 10; while i > 0: i = i - 1
Ask whether the condition of the while loop can ever become false, meaning whether the variable inside the condition changes. -
-
When a while condition is checked
In a standard while loop, when is the loop condition evaluated?
After each iteration, so the body always runs at least once
Only once, after the loop body has finished running
Before each iteration, so the body may run zero times
Only when the loop body contains a break statement
Think about what happens when a while loop's condition is False the very first time it is reached. -
-
Loops that run at least once
Which type of loop guarantees that its body is executed at least once, because its condition is checked after the body?
A post-test loop such as do-while
A while loop whose condition is initially False
A for loop over range
Python's while loop
Consider which loop structure places its condition check at the end of the loop instead of at the beginning. -
-
-
Preventing an infinite while loop
The following code is meant to print the numbers 1 to 5, but it currently never ends. Which line should be added inside the loop body to fix it?
n = 1
while n <= 5:
print(n)n == n + 1
n = 5
n = n + 1
print(n + 1)
The loop condition depends on n, so the body must change n so that the condition can eventually become False.
פרטים
- מקצועות
- שפות תכנות
- קהל יעד
- תיכון · כיתות י׳–י״ב
- רמת קושי
- 6 מתוך 10
- שפה
- אנגלית
- מי יכול לראות
- כולם
- נוצר באמצעות AI
- Claude Haiku 5.5
- נוצר
תגובות