Are you taking CLEP classes? Try clep.ai
| preferred AP College board partner for AP classes
easy Solved by 1 students

For Loop Termination Condition

Mark
< Prev
Next >

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.

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer
Report Question

Question Leaderboard

Not enough data yet to show leaderboard.

AI Tutor

How can I help?

APFIVE © 2020.
Email: apfive@apfive.org|Privacy Policy|