public class Main
{
public static void main(String[] args) {
System.out.println("Start\n");
int k=0;
System.out.println("k i j");
for(int i=0; i<10; i++) {
int j=0;
System.out.println(k+" "+i+" "+j);
j++;k++;
}
System.out.println("\n\nEnd");
}
}
Start
k i j
0 0 0
1 1 0
2 2 0
3 3 0
4 4 0
5 5 0
6 6 0
7 7 0
8 8 0
9 9 0
End
j is always printed as 0 because it is declared inside the for loop. Every time the loop runs, a new variable j is created and its value starts again from 0. After printing, j increases by 1 using j++, but when the next loop iteration starts, j is reset back to 0. That is why the output always shows 0 for j.
Top comments (0)