Meaningless Drivel is fun!
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 Java Interview Guide this week in the Jobs Discussion 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: 168
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++)
I agree. Here's the link:
subject: Expression in for loop
It's not a secret anymore!