- 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.
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.
- 1
Yes, you end up with 9 iterations.
- 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