For Loop Termination Condition
The following main method is intended to print the integers from 1 to 10, inclusive. The code compiles and runs, but it fails to print the number 10. What is the error in the code?
public class PrintNumbers {
public static void main(String[] args) {
for (int i = 1; i < 10; i++) {
if(i == 10)
System.out.print("10");
else
System.out.print(i + " ");
}
}
}
A
The if condition inside the loop wrongly tests for i equal to 10.
B
The print statement should use println instead of print.
C
The loop variable i is not correctly declared within the loop.
D
The loop condition should be i <= 10 instead of i < 10.
Question Leaderboard
Not enough data yet to show leaderboard.
