Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Understanding the nested for loop  RSS feed

 
Adetunji Ojekunle
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody,
Just got here by referral,
I really need to understand the nested for loop, I have been staring at my computer trying to understand it but no avail. Take for example this little code(the one I have been trying to understand)

Any help will be appreciated.
Thank you.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to Coderanch! Above your code is same like When a for loop doesn't have { } braces then it can execute or we can say scope of that loop is only one line of code beneath that for loop statement.
Example:
It is same as below code
In your code outer i loop block scope contains executed 5 times and inner j loop scope which contains executed 15 times so output you get is 15 times ". "(dots)
Output:
.
.
.
.
.

.
.
.
.

.
.
.

.
.

.

Explanation:
When outer i loop starts executing i = 0 and then it executes inner j loop where it assigns j = i so now j =0, then j loop executes till j is less than 5 i.e. j < 5 returns true, so prints "." 5 times each dot on new line. When j becomes 5 then control comes out of inner j loop and executes System.out.println(" "); and prints blank space.
Now increments i so i becomes 1, again starts executing inner j loop by assigning j = i i.e. j = 1 again inner loop executed 4 times. Same process happens till i is less than 5 i.e. i <5 returns true.
Loop execution steps with values and condition check:
StepsValue of iValue of ji < 5 ?j < 5 ?
1 st time execution of outer i loop, At begining i = 0
10---Yes---
2, Assigns j = i, Now j = 000---Yes
301---Yes
402---Yes
503---Yes
604---Yes
705---No
Executes System.out.println(" "); which is in outer i loop
2nd time execution of outer i loop, Increments i, Now i = 1
81---Yes---
9, Assigns j = i, Now j = 111---Yes
1012---Yes
1113---Yes
1214---Yes
1315---No
Executes System.out.println(" "); which is in outer i loop
3rd time execution of outer i loop, Increments i, Now i = 2
142---Yes---
15, Assigns j = i, Now j = 222---Yes
1623YesYes
1724YesYes
1825YesNo
Executes System.out.println(" "); which is in outer i loop
4th time execution of outer i loop, Increments i, Now i = 3
193---Yes---
20, Assigns j = i, Now j = 333---Yes
2134---Yes
2235---No
Executes System.out.println(" "); which is in outer i loop
5th time execution of outer i loop, Increments i, Now i = 4
234---Yes---
24, Assigns j = i, Now j = 444---Yes
2545---No
Executes System.out.println(" "); which is in outer i loop
6th time execution of outer i loop, Increments i, Now i = 5
215---No---
Control comes out of outer i loop b'cause i <5 returns false
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dunno who gave but made my day. Thank you
 
Liutauras Vilda
Marshal
Posts: 4635
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganish, I gave you 1 cow for a putted effort and as a result for a great output post
And I see somebody else gave you 1 before, it seems you come up with really great post
 
Stephan van Hulst
Saloon Keeper
Posts: 7805
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Liutauras said.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Means a lot to me and also makes me feel more responsible while answering the questions although they are basic. Once again thank you Stephan van Hulst and Liutauras Vilda
 
Adetunji Ojekunle
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much Ganish
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My pleasure
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!