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?

Member Avatar for iamthwee

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

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
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.