Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

using "for loops"  RSS feed

 
zawani ahmad
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i need help to understand using "for loops" in java,because i cannot understand with condition.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

Basically, the condition in a for loop is evaluated to a boolean at the "top" (beginning) of each iteration. If true, then the loop body executes. If false, then the loop body does not execute.

See this Iteration section from Thinking in Java, and this section on The for Statement from The Java Tutorial.
 
Sunil Kumar Gupta
Ranch Hand
Posts: 824
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by zawani ahmad:
i need help to understand using "for loops" in java,because i cannot understand with condition.


In for loop there is an initializantion part, a condition and an increment/decrement part. while the condition is satisfied. for loop executes it's body, As soon as the condition fails , it simply comes out from it's body.
[ May 27, 2006: Message edited by: Sunil Kumar Gupta ]
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Till the condition is satisfied.

Not "till," but "while."

You set up a loop with a condition like i < 12; and as long a i is less than 12, the loop is executed. When i reaches 12, then the loop is finished.
Is that any help?

CR

Additional: Remember the "condition" is the middle bit in the (), after the first ; and before the second ;.
[ May 27, 2006: Message edited by: Campbell Ritchie ]
 
Sunil Kumar Gupta
Ranch Hand
Posts: 824
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell, I have corrected that.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!