• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help me put my confusion to rest

 
Ricky Rozay
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings, I have analyzed the code below a numerous times, but I was end up failing in understanding it completely.



This supposedly print out
0 4
0 3
1 4
1 3
3 4
3 3

And here is what I see:
v = 0, z = 4, then it prints out: 0 4
v = 1 (since it is v++ in the first for loop), z = 3 (same concept same the first for loop), and now that v == 1, add one (v++) to v = 1, then it prints out: 2 3
v = 3 but z = 2 so nothing happens because z does not satisfy z > 2 so it does not print anything out.

Therefore, it compiles to:
0 4
2 3

Thanks for helping!!

[Edit - added code tags - MB]
 
Nitin Surana
Ranch Hand
Posts: 129
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this one the correct output ?

 
Ricky Rozay
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya that's supposed to be the correct output.
 
Nitin Surana
Ranch Hand
Posts: 129
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


for(int i=0;i<10;i++) { //code within }

Execution Order :



I hope you understand the output now.


 
Ricky Rozay
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"then condition is checked" and "if condition is true"
What is this condition you are referring to?
i<10 is the condition right?

and how is v printing 0 twice and 1 twice?
 
Nitin Surana
Ranch Hand
Posts: 129
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, i<10 is the condition I am referring to.

 
Ricky Rozay
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for quickly replying, just in case you did not see my previous edited reply..

how is v printing 0 twice and 1 twice?

for v , after 0 prints out, then 3 should print out right?
 
Nitin Surana
Ranch Hand
Posts: 129
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ricky Rozay wrote:





For every single value of v , inner FOR loop is called twice.
they form nested loops (loops within loops).
 
Ricky Rozay
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nitin, I really tried......... but I still do not understand. I appreciate your contribution and if you don't feel like explaining it to me anymore, then don't and I will still thank you.
But if you don't mind helping me out, then these are what I do not get:

First of all, let me clarify, inner FOR loop is the second loop right?
If so or not, how is it called twice? That part I really don't understand.
 
Claudiu Chelemen
Ranch Hand
Posts: 75
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ricky,

Consider v to be a constant, e.g. 0, and look only at the inner loop:

This will always print out:
0 4
0 3


Do you agree?

Claudiu
 
Ricky Rozay
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I finally understand it!! thanks!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic