• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Expression in for loop

 
Vamshi Vrukodar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry. excuse me for this post. I misunderstood the question.
Thank you
Vamshi
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Vamshi.
You can delete this post if you want via the editing icon (the one with the pencil on it)
 
Jack Lau
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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++)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic