65.0k views
5 votes
State whether it is infinite loop, finite loop, no loop at all, or compiler error.

for (int i = 0; i < 10 ; i+1) cout << "wow";
A. Infinite loop
B. Finite loop
C. No loop at all
D. Compiler error

1 Answer

4 votes

Final answer:

The code snippet creates an infinite loop because the loop increment expression i+1 does not actually change the value of i. To create a finite loop, the increment should be i++ or i += 1.

Step-by-step explanation:

The code snippet provided: for (int i = 0; i < 10 ; i+1) cout << "wow"; will lead to an infinite loop. This is because the loop increment expression i+1 does not actually increment the variable i. Instead, it merely evaluates the expression i+1 without changing the value of i. As a result, the condition i < 10 will always be true, and the loop will execute indefinitely, printing "wow" without stopping.

To fix this and turn it into a finite loop, the increment expression should be i++ or i += 1, which would correctly increment the value of i after each iteration of the loop.

User Andros
by
8.7k points