aspose file tools*
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 Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Expression in for loop" Watch "Expression in for loop" New topic
Author

Expression in for loop

Vamshi Vrukodar
Greenhorn

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

System.out.println(j);
the output is 0 where I expected it to be 3.
Please can you explain the above behaviour.
Regards
Vamshi
Vamshi Vrukodar
Greenhorn

Joined: May 25, 2003
Posts: 6
I am sorry. excuse me for this post. I misunderstood the question.
Thank you
Vamshi
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.

Jack
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++)
to
for(int i=0;j < 3;i++)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Expression in for loop