Simple nested for loop example

后端 未结 7 513
后悔当初
后悔当初 2021-01-18 01:15

Currently I am studying for my Java test. Whist studying I\'ve come across a small problem.

In this for loop:

for ( int i=1; i <= 3 ; i++ ) {
            


        
相关标签:
7条回答
  • 2021-01-18 01:47

    Let's look at what this would look like if we changed from loops to repeated code. First the outer loop:

    for (int j=1; j <= 3 ; j++ ) {
        System.out.println( 1 + " " + j );
    }
    for (int j=1; j <= 3 ; j++ ) {
        System.out.println( 2 + " " + j );
    }
    for (int j=1; j <= 3 ; j++ ) {
        System.out.println( 3 + " " + j );
    }
    

    Now the inner loop:

    // First loop
    System.out.println( 1 + " " + 1 );
    System.out.println( 1 + " " + 2 );
    System.out.println( 1 + " " + 3 );
    // Second loop
    System.out.println( 2 + " " + 1 );
    System.out.println( 2 + " " + 2 );
    System.out.println( 2 + " " + 3 );
    // Third loop
    System.out.println( 3 + " " + 1 );
    System.out.println( 3 + " " + 2 );
    System.out.println( 3 + " " + 3 );
    

    Does it make sense now?

    0 讨论(0)
  • 2021-01-18 01:47

    Every time you nest for loops (that's what it's called when you put one inside of another), it basically adds another "dimension". If you have a single for loop, it's like a straight line. So, if our first loop is from 1 to 5 it would be:
    1 2 3 4 5

    If you add a second loop, (lets say 1-3) (You read top to bottom left to right, first number represents the first variable, etc)

    11 21 31 41 51
    12 22 32 42 52
    13 23 33 43 53

    And if you add a third loop (let's say 1-2) (I obviously can't make 3 dimensions here, so bear with me)

    111 211 311 411 511 || 112 212 312 412 512
    121 221 321 421 521 || 122 222 322 422 522
    131 231 331 431 531 || 132 232 332 432 532

    So, the total number of iterations (how many times you go through the loops) is the product of the loops. This one is a 3 * 5 * 2, so it will iterate 30 times.

    0 讨论(0)
  • 2021-01-18 01:50

    Each iteration of i, you're starting a completely new iteration of j.

    So, you start with i==1, then j==1,2,3 in a loop. Then i==2, then j==1,2,3 in a loop, etc.

    Step through it one step at a time, and it will make sense.

    0 讨论(0)
  • 2021-01-18 01:58

    For each iteration of outer loop, complete inner loop will execute. For example when i= 1, inner loop will execute 3 times and you will get 1 1 1 2 1 3

    0 讨论(0)
  • 2021-01-18 02:00

    What you have is one loop inside of another. Think of it like the minute hand and the hour hand on a clock. The minute hand is going to go around (1,2,3,4...59) while the hour hand is still on 1. So, if we treat hours as i, and minutes as j, we have:

    1 1
    1 2
    1 3
    ...
    1 60

    And then the hours change to 2, and the minute hand keeps going around:
    2 1
    2 2
    2 3
    ...
    2 60

    And we finish once we get to

    12 1
    12 2
    12 3
    ...
    12 60

    which is where the loop ends. Your example is much like this.

    (For the pedantic, I know it's zero-based, but in order to illustrate, this may be easier to understand)

    0 讨论(0)
  • 2021-01-18 02:02

    You have a loop inside another loop.

    So for every iteration of the outer loop, you will run the inner loop to completion, starting at 1 ending at 3.

    So you end up printing j=1,2,3, for each value of i. In this case i is 1,2, and 3.

    0 讨论(0)
提交回复
热议问题