test condition in a for loop

Thread Solved

Join Date: Nov 2007
Posts: 27
Reputation: kartik14 is an unknown quantity at this point 
Solved Threads: 0
kartik14's Avatar
kartik14 kartik14 is offline Offline
Light Poster

test condition in a for loop

 
0
  #1
Nov 16th, 2007
I have a question regarding the test condition of the for loop..

For example, if the loop is say:
  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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: test condition in a for loop

 
0
  #2
Nov 16th, 2007
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.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,355
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: test condition in a for loop

 
0
  #3
Nov 16th, 2007
Originally Posted by iamthwee View Post
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.

  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. }
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC