Any easy way to think of nested for loops is to ignore the fact that they are nested.
By convention, you will typically use i for the outer loop's increment counter and j for the inner loop's, which is the most important thing to keep straight in the beginning. If that is a point of confusion for you, it would likely benefit you to use more descriptive names for your increment variables than the letters 'i' and 'j', for example outer and inner.
At any given time when you are trying to structure your program's logic you only need to focus on the for loop that you are working most directly inside - at least when you are starting out and learning about them for the first time.
java - Nested for loops - Stack Overflow
can't understand nested for loops π (need help!)
nested For Loops in Java - Stack Overflow
How do nested loops in Java work? - Stack Overflow
Videos
Any easy way to think of nested for loops is to ignore the fact that they are nested.
By convention, you will typically use i for the outer loop's increment counter and j for the inner loop's, which is the most important thing to keep straight in the beginning. If that is a point of confusion for you, it would likely benefit you to use more descriptive names for your increment variables than the letters 'i' and 'j', for example outer and inner.
At any given time when you are trying to structure your program's logic you only need to focus on the for loop that you are working most directly inside - at least when you are starting out and learning about them for the first time.
I haven't done any JAVA but I know that C# is pretty much the same.
I would do like this:
int max = 30;
int value = 0;
int counter = 0;
int[] input[4] = new int[5, 9, 3, 21];
bool[] canAddInput[4] = new bool[false, false, false, false];
for(value; value <= max; )
{
for(counter; counter < 4; counter++)
{
value += input[i];
if(value<=max)
canAddInput[i] = true;
}
if(counter >= 4)
Break;
}
We have a practical exam coming up on my uni regarding nested for loop in which I struggle to. Can you help me understand how does it work. I know that it is a loop within a loop but I can't logically decide what values to put in the inner loop (the range and the increment). I can't wrap my head around it. π
My professor typically give us exercises like these:
Exercise 1: 9 7 7 7 5 5 5 5 5 3 3 3 3 3 3 3 Exercise 2: 8 6 6 4 4 4 2 2 2 2 0 0 0 0 0 Exercise 3: 4 4 4 4 4 4 4 4 3 3 3 3 3 3 2 2 2 2 1 1 Exercise 4: 2 2 2 2 2 2 4 4 4 4 6 6 Exercise 5: 1 1 1 1 1 5 5 5 9 Exercise 6: 5 5 10 10 10 10 15 15 15 15 15 15 Exercise 7: 6 444 22222 Exercise 8: 6 444 2222
for (int i = 1; i <= 5; i++) { // outer loop iterates 5 times.
for (int j = 1; j <= 10; j++) { // for each iteration of outer loop,
// inner loop iterates 10 times
System.out.print((i * j) + " ");
}
System.out.println();
}
First iteration of outer loop (10 iterations of inner loop)
i = 1, j = 1
i = 1, j = 2
...
i = 1, j = 10
Second iteration of outer loop (10 iterations of inner loop)
i = 2, j = 1
i = 2, j = 2
...
i = 2, j = 10
...
Last iteration of outer loop (10 iterations of inner loop)
i = 5, j = 1
i = 5, j = 2
...
i = 5, j = 10
It will do...
1*1 1*2 1*3 till it gets to 1*10, then on a new line
2*1 2*2 2*3 and it will go to all the way to
.
.
5*10
So it will print out 1 2 3 4 5 ... till 10, then do a new line. Output below.
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50