• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Java For Dummies: nested loops

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am into my second programming course. The first was Beginners Java. Before I started I took Java For Dummies out of the library and went through some of it. It really helped me since the textbook and the professor were really hard to understand for such a newbie.
I returned the book before I got to loops but I found that concept really hard to get. I'm still reusing code when I need a loop.
How can you explain the logic of a loop in 50 words or less?
 
Ray Bell
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry I meant nested loops
 
Greenhorn
Posts: 5
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
webpage
visit this page to learn basics of Nested loops.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ray Bell wrote: I'm still reusing code when I need a loop.
How can you explain the logic of a loop in 50 words or less?



Hey Ray, welcome to the Ranch.  Not sure about the 50 words. Let me try my best to explain.

The nested loops are useful when you want to iterate over two levels. Say for example, a 2D (Two dimensional) array which will be across rows and columns, for which you need two rounds ofi iteration. At first you would iterate on the row level. Inside each row, you still need to iterate each column on the same row (think of an excel spreadsheet or a table). You need to hold the position of row at the outer layer, until all the columns are visited in the inner layer. The looping is done here at more than one level for doing any operation on the data which is of more than one dimension.

Say for example, you have a two dimensional array of 3 rows 3 columns (3 X 3 array) having 9 elements in total. You need to visit each cell (an intersection of a row and column is called a cell) to find out the occurrence of a particular number. For which you need to do the following.

Set Count = 0
Set NumberToSearch = 5
Iterate Rows from 1 To M -- OUTER LOOP controlling the rows
 Iterate Columns from 1 to N -- INNER loop controlling the columns per row
     If Value at the cell (R, C) = 5 Then
        Increment count (set count = count + 1)
     Else
        Do Nothing. Continue with the next column in the same row if any pending
End.

Hope this helps  you get the concept. Let us know if you have any other questions.
     
 
Author
Posts: 160
11
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ray,
Sometimes it helps to just walk through existing loops and checking to make sure that you understand why they give the output that they give.
Here's a program with nested loops:



Make yourself a chart with columns labeled "i", "j" and "output" and step through each line of the program as it would be executed, asking yourself what the line does to the value of i or j, or to the output. Keep track in the chart of the updated values of these things. When you're done making the chart, run the program to find out if it gives you the output that you predicted. If it does, you understand something about nested loops. If it doesn't do what you predicted, ask yourself why the output that you predicted isn't really what the program does.
Ask me some follow-up questions if you have any.
 
Create symphonies in seed and soil. For this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic