| preferred AP College board partner for AP classes
easy Solved by 3 students
For Loop Termination Condition
< Prev
Next >

The intended behavior is to print numbers 1 through 10, but “10” is never printed. What is the error in the loop?

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 loop variable i is not correctly declared within the loop.

C

The print statement should use println instead of print.

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.

No comments yet. Be the first to comment!

AI Tutor

How can I help?

APFIVE © 2020.
Email: [email protected]|Privacy Policy