944,001 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 7310
  • Java RSS
Nov 16th, 2007
0

test condition in a for loop

Expand Post »
I have a question regarding the test condition of the for loop..

For example, if the loop is say:
Java Syntax (Toggle Plain Text)
  1. for ( int i=0 ; ( C1 && C2 ) ; i++ ) {// some code}

My question is, if either C1 or C2 is false then the test condition is false. So does the for loop end in this condition?

But what if in this particular iteration C1 is false, but in the next iteration both C1 and C2 will be true and now we want to execute the loop body.. How is it possible then if the loop ends as C1 is false before we get this scenario?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
kartik14 is offline Offline
32 posts
since Nov 2007
Nov 16th, 2007
0

Re: test condition in a for loop

Don't put conditions in for loops... Personally, I don't like how they read. It can be non-intuitive sometimes.
Last edited by iamthwee; Nov 16th, 2007 at 9:39 am.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Nov 16th, 2007
0

Re: test condition in a for loop

Click to Expand / Collapse  Quote originally posted by iamthwee ...
Don't put conditions in for loops...

Nothing wrong with it. However, you must be aware that if the overall condition returns false, the loop ends. If you wish to skip a single iteration of the loop and continue with the next, then use only the standard condition (i.e. i < whatever) and add an if statement inside the loop for the other conditions. If that if statement returns true, then use the continue keyword. E.G.

Java Syntax (Toggle Plain Text)
  1. for (int i = 0; i < someNumber; i++) {
  2. // maybe perform some partial loop actions
  3. if (someCondition) {
  4. continue;
  5. }
  6. // perform loop action
  7. }
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: servlets
Next Thread in Java Forum Timeline: Ordering HashMap values





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC