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
117 Likes
Answer
0 2 4 6 8 10
Working
This table shows the changes in the value of i as the for loop iterates:
i | Remarks |
---|---|
-1 | Initial value |
0 | 1st Iteration — i is -1, ++i makes it 0 |
2 | 2nd Iteration — i becomes 1, ++i makes it 2 |
4 | 3rd Iteration — i becomes 3, ++i makes it 4 |
6 | 4th Iteration — i becomes 5, ++i makes it 6 |
8 | 5th Iteration — i becomes 7, ++i makes it 8 |
10 | 6th Iteration — i becomes 9, ++i makes it 10 |
11 | Once i becomes 11, condition is false and loop stops iterating. |
Answered By
54 Likes
Related Questions
State one similarity and one difference between while and for loop.
Give two differences between Step loop and Continuous loop.
Predict the Output of the following Java program:
class dk2 { public static void main(String args[]) { int i=2,k=1; while (++i<6) k *= i; System.out.println(k); } }
Predict the Output of the following Java program:
class dk3 { public static void main(String args[]) { int m=2,n=15; for(int i=1;i<=5;i++) { m++;--n; System.out.println("m="+m); System.out.println("n="+n); } } }