This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Help me put my confusion to rest Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help me put my confusion to rest" Watch "Help me put my confusion to rest" New topic
Author

Help me put my confusion to rest

Ricky Rozay
Greenhorn

Joined: Dec 11, 2011
Posts: 7
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

Joined: Jan 21, 2011
Posts: 129

Is this one the correct output ?

Ricky Rozay
Greenhorn

Joined: Dec 11, 2011
Posts: 7
Ya that's supposed to be the correct output.
Nitin Surana
Ranch Hand

Joined: Jan 21, 2011
Posts: 129



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

Execution Order :



I hope you understand the output now.


Ricky Rozay
Greenhorn

Joined: Dec 11, 2011
Posts: 7
"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

Joined: Jan 21, 2011
Posts: 129

yes, i<10 is the condition I am referring to.

Ricky Rozay
Greenhorn

Joined: Dec 11, 2011
Posts: 7
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

Joined: Jan 21, 2011
Posts: 129

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

Joined: Dec 11, 2011
Posts: 7
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

Joined: Mar 25, 2011
Posts: 71

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

Joined: Dec 11, 2011
Posts: 7
I finally understand it!! thanks!!
 
Consider Paul's rocket mass heater.
 
subject: Help me put my confusion to rest
 
Similar Threads
operator
Hai How does this go
Need Help Reviewing For Exam
Arrays
Explicit initialization: It comes before the constructor is invoked, right?