In the for loop syntax for (int i = 0; i < n; i++), what does i < n specify?

Enhance your programming skills with the RECF Programming Test. Features flashcards and multiple choice questions with hints and explanations. Prepare for success!

Multiple Choice

In the for loop syntax for (int i = 0; i < n; i++), what does i < n specify?

Explanation:
i < n is the continuation test for the loop. It’s checked before each iteration, and as long as this condition is true, the loop runs; when i reaches n, the condition becomes false and the loop stops. Since i starts at 0 and increases by 1 each time (i++), the loop executes for i = 0, 1, 2, ..., n-1, doing something n times. The other descriptions don’t match how the loop is written: the increment is by 1, not by n; the initialization sets i to 0, not n; and termination happens when i reaches n, not when it becomes 0.

i < n is the continuation test for the loop. It’s checked before each iteration, and as long as this condition is true, the loop runs; when i reaches n, the condition becomes false and the loop stops. Since i starts at 0 and increases by 1 each time (i++), the loop executes for i = 0, 1, 2, ..., n-1, doing something n times. The other descriptions don’t match how the loop is written: the increment is by 1, not by n; the initialization sets i to 0, not n; and termination happens when i reaches n, not when it becomes 0.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy