Q: What will happen if the loop control variable in a for loop is modified inside the loop?
-
A
The loop will terminate
-
B
The loop may behave unexpectedly
-
C
The loop will skip the next iteration
-
D
The loop will not execute
B
Answer:
B
Explanation:
Modifying the loop control variable inside the loop can lead to unexpected behavior. For instance, if the variable controlling the number of iterations is altered in the body of the loop, it could cause the loop to terminate prematurely, skip iterations, or even result in an infinite loop. It is generally not recommended to modify the control variable inside a loop, as it can lead to logic errors and unpredictable outcomes.
Related Topic:
Share Above MCQ