Q: What is the correct syntax for a while loop in Python?
-
A
while i < 10:
-
B
while (i < 10);
-
C
while (i < 10)
-
D
while i < 10 {}
A
Answer:
A
Explanation:
In Python, the correct syntax for a while loop includes the keyword while followed by a condition and a colon (:). The loop's body is indented and will keep executing as long as the condition is true. Option B represents incorrect syntax due to the semicolon, which is not required in Python. Option C is incorrect because it lacks the colon, and Option D is incorrect because it uses curly braces, which are not used for loops in Python. The while loop in Python is widely used for running a block of code as long as a given condition remains true, making it useful for repeating tasks without knowing in advance how many iterations are needed.
Related Topic:
Share Above MCQ