Correct Answer : A
Execution of program starts from main
. The first statement of main
is a for
loop which sets variable i
to zero. The loop gets executed till the condition i <= 5
is satisfied and for every iteration (cycle) i
is incremented by 1 (since i++
). Inside the for
loop is a display statement which displays i
for each iteration. So initially i = 0
is displayed. i
is incremented till i = 5
each time and respective values of i
are displayed. The value after this iteration (having i = 5
) becomes 6 (i = 6
) and condition i <= 5
is violated. So for
loop is terminated. Thus values from 0
to 5
are displayed.
Total number of iterations: 6
Initialization: i = 0
Terminating condition: i <= 5
Increment value: 1 (i.e. i++)
Extra Note: In the increment part of for loop, it is also possible to use ++i
. The difference is that with ++i
(prefix incrementing) the one is added before the “for
loop” tests if i < 5
. With i++
(postfix incrementing) the one is added after the test i < 5
. In case of a for loop this make no difference, but in while
loop test it makes a difference.