954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

test condition in a for loop

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

For example, if the loop is say:

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?

kartik14
Light Poster
32 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

Don't put conditions in for loops... Personally, I don't like how they read. It can be non-intuitive sometimes.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 
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.

for (int i = 0; i < someNumber; i++) {
  // maybe perform some partial loop actions
  if (someCondition) {
    continue;
  }
  // perform loop action
}
masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You