• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Enhanced For Loop Nested  RSS feed

 
raghav singh
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can someone explain to me how does this nested enhanced for loop execute? Sorry for the bad code indention. I dont know how to indent it.



Note:

exams,levels,grades are arraylist

exams contain Java,Oracle
levels contain Basic,Advanced
grades contain pass,fail
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
UseCodeTags ← click this
I have added code tags for you this time .

Enhanced for loop is a for-each loop.

for (String exam : exams) means:
For each element of type String (let's call it exam) in exams execute the body of the loop.
 
raghav singh
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:UseCodeTags ← click this
I have added code tags for you this time .

Enhanced for loop is a for-each loop.

for (String exam : exams) means:
For each element of type String (let's call it exam) in exams execute the body of the loop.


Yeah thank you very much for correcting the code indention.

Concerning the enhanced for loop i know now what it does... but dont know how the nested loop are executed that what i wanted to know....
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is easy to test what the code would print, is it not? Or is that not what you mean by "how the nested loop are executed"?
 
raghav singh
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:It is easy to test what the code would print, is it not? Or is that not what you mean by "how the nested loop are executed"?


I know already what the code will output but don't know how it reaches such output.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raghav singh wrote:
Ulf Dittmer wrote:It is easy to test what the code would print, is it not? Or is that not what you mean by "how the nested loop are executed"?


I know already what the code will output but don't know how it reaches such output.


The behavior of nested loops is the same for any loop -- and not just enhanced for loops. As for your example...

raghav singh wrote:



The for loop for exams, is started only once, and runs for two iterations since the exams list has two elements.

The for loop for levels, is started twice since it is part of the exams for loop that has two iterations. Each run has two iterations since the levels list has two elements. This means that there are a total of four iterations.

The for loop for grades, is stated four times since it is part of the levels for loop that has a total of four (two times two) iterations. Each run has two iterations since the grades lists has two elements. This means that there are a total of eight iterations.

And finally, this means that the println() is executed eight times.

Henry
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!