- 1

Question about nested for loops.

Let me get this straight, let's say you have the outer loop of, for(int i = 1; i<=3; i++), and an inner loop of, for(int j = 1; j<=3; j++), the outer loop will run 1 time and switch to the inner loop, which will run until False and go back to the outer loop, which will run 1 time again, then switch back to the inner loop and run until False again and repeat until the outer loop is False, is that right? Does the same apply for while/do-while loops.

28th Jul 2022, 1:52 PM
Tristan
3 Answers
0
Yes, let's say we have: int i = 1; int j= 1; while (i<=3) { while (j<=3) { j++; } i++; } You would get the same Outcome as with the for-loop. edit: sry I know that I had a flaw there. int j = 1; needs to be in the while (i<=3) Loop.
28th Jul 2022, 2:27 PM
Felix Alcor
Felix Alcor - avatar
- 1
Yes, you end up with 9 iterations.
28th Jul 2022, 3:28 PM
Solo
Solo - avatar
- 1
Tristan As the other stated, it is the same for while loops, but it might not be always the case for do-while loops since if the loop were to be false at Thema start, you would still run the code in the do section at least once
28th Jul 2022, 6:47 PM
YuGiMob
YuGiMob - avatar