java - how many times is the println statement executed -
i kind of don't nested loops, can explain me why following code executed 45 times please? shouldn't i++ , j++ increment 1? first loop should (0 * 0), (1 * 1), (2 * 2) , on.
for(int = 0; < 10; i++) for(int j = 0; j < i; j++) system.out.println(i * j);
that not how nested loops work, -
- first enter outer loop, execute inner loop till inner loop exits (that j's value becomes equal i) , , after again increment i.
so example , if i 5 , enter the outer loop, , j start 0 till 4 (as 5) , values calculated (5*0) , (5*1) , (5*2) , (5*3) , (5*4) , , after exiting inner loop, again increment i 1 , i become 6 , repeat (that j start 0 till 5).
Comments
Post a Comment