+ 1
Why is this the outcome?
The outcome of this code is 012024, but I really don't understand why. Could someone explain it to me? for(int i = 1; i < 3; i++){ for(int j = 0; j < 3; j++){ if(i + j > 5) break; System.out.print(i*j); } }
3 Antworten
+ 2
1st outer loop:
1st inner loop: i=1, j=0, i*j=0
2nd inner loop: i=1, j=1, i*j=1
3rd inner loop: i=1, j=2, i*j=2
4th inner loop: j would no longer be <3, so the requirement is not fulfilled
2nd outer loop:
1st inner loop: i=2, j=0, i*j=0
2nd inner loop: i=2, j=1, i*j=2
3rd inner loop: i=2, j=2, i*j=4
4th inner loop: same as above
3rd outer loop: requirement is not fulfilled, so your program will not enter the inner loop anymore and your break-condition is never reached!
So your program is done.
+ 4
1. i=1, j=0 . Here i+j is not > 5, Hence break is not executed. i*j is printed = 0
2. i=1 j=1 same blah blah, i*j = 1 . NET OUT= 01
3. i=1, j= 2 same blah blah , i*j = 2 NET OUT=012
Inner Loop terminated,outer incremented
4. i=2 j=0 same , i*j=0 NET = 0120
5 i=2 j=1 same , i*j = 2 net=01202
6. i=2 j=2 same , i*j =4 net = 012024
inner terminated, outer terminated.
Loops terminated.
Answer: 012024
If statement never works.
+ 1
Thanks, it all makes sense now!