KnowledgeBoat Logo

Computer Applications

Predict the Output of the following Java program:

class dkl
{
public static void main(String args[])
{
int i;
for(i = -1;i<10;i++)
{
System.out.println(++i);
} 
} 
}

Java

Java Iterative Stmts

96 Likes

Answer

0
2
4
6
8
10

Working

This table shows the changes in the value of i as the for loop iterates:

iRemarks
-1Initial value
01st Iteration — i is -1, ++i makes it 0
22nd Iteration — i becomes 1, ++i makes it 2
43rd Iteration — i becomes 3, ++i makes it 4
64th Iteration — i becomes 5, ++i makes it 6
85th Iteration — i becomes 7, ++i makes it 8
106th Iteration — i becomes 9, ++i makes it 10
11Once i becomes 11, condition is false and loop stops iterating.

Answered By

48 Likes


Related Questions