Could someone explain how the flow of control works with a nested for loop?

for (initialize; condition; increment) 
    for (initialize; condition; increment)

I know a single for loop is:
initialize --> condition --> statement --> increment--> condition -->.......

The contents of a for loop can be a statement or a block. Either way the contents are executed as many times as the (initialize; condition; increment) specifies.

for (initialize1; condition1; increment1)
    for (initialize2; condition2; increment2)

Here the first for executes the following code as many times as it should:

for (initialize2; condition2; increment2)

which inturn executes statement appropriately before going back to the outer for loop for another execution.

for (int i = 0; i<3)
   for (int j = 0; j<2; j++)
      System.out.println(i + " " + j);

will give the following output
0 0
0 1
1 0
1 1
2 0
2 1

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.