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 Programmer Certification (SCJP/OCPJP) and the fly likes Expression in for loop 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Expression in for loop" Watch "Expression in for loop" New topic

Expression in for loop

Vamshi Vrukodar

Joined: May 25, 2003
Posts: 6
in the following code,
int j=0;
for(int i=0;j>3;i++)

the output is 0 where I expected it to be 3.
Please can you explain the above behaviour.
Vamshi Vrukodar

Joined: May 25, 2003
Posts: 6
I am sorry. excuse me for this post. I misunderstood the question.
Thank you
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Welcome to the Ranch Vamshi.
You can delete this post if you want via the editing icon (the one with the pencil on it)

SCJP2. Please Indent your code using UBB Code
Jack Lau
Ranch Hand

Joined: Aug 30, 2002
Posts: 166
If you replace j>3 to i>3, the output is still 0. That means when the condition not match, the statement j=j+1; in the for loop will not be executed.

Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
What your code is saying is that 'while j>3 then increment j by 1'. Of course this will not happen because j started out as 0.
You need to change your code from
for(int i=0;j > 3;i++)
for(int i=0;j < 3;i++)
It is sorta covered in the JavaRanch Style Guide.
subject: Expression in for loop
Similar Threads
Int array to 3D array
why does 'break' traverse one more time
Please Explain
Flow control
increment operator