Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Understading multidimensional arrays / nested for loops

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand how this nested for loop is printing each and every element in each row and column.

I need someone to explain to me how its going through all the indexes [0, 1] [0, 2] [1, 0] etc.

Aren't both loop being incremented at the same time, or does the inner loop get incremented all the way before the outer loop gets incremented again. Please explain someone.

 
Rancher
Posts: 220
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every line of code runs in order. Therefore, the code enters the first (outer) loop, and finds another loop. It will now run the entire second (inner) loop to completion until it proceeds to the next line, where it continues with the outer loop execution.

So, for the first iteration of the outer loop, i == 0.
Now it runs inner loop. The first iteration, j == 0. It runs code for [0,0].
Now second iteration for inner loop, j == 1. It runs code for [0,1].
Continuing, j == 2, runs code for [0,2]
Exits inner loop, because j < 3 won't be true anymore.

Then it proceeds to the second iteration of the outer loop, i == 1.
You can probably see how it goes from here.
 
Saloon Keeper
Posts: 7395
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the inner loop goes through all the columns before it breaks out of the inner loop. The next thing it encounters is a closing brace '}' which means go back up and get the next value in the outer loop.
Sometimes it is helpful to rename the variables like this:
 
Marshal
Posts: 70718
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try going through it with pencil and paper, remembering the inner loop iterates thrice for every one iteration of the outer loop, as you said. Don't hard‑code the numbers. Use the field of the arrays instead. Much less error‑prone:-Since you are iterating both arrays forwards and not omitting any elements a for‑each loop would be easier to write.
 
Carey Brown
Saloon Keeper
Posts: 7395
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A variation that will print a small 2D array so that it looks like a 2D array. This uses print() and println().
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic