| | |
test condition in a for loop
Thread Solved |
I have a question regarding the test condition of the for loop..
For example, if the loop is say:
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?
For example, if the loop is say:
Java Syntax (Toggle Plain Text)
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?
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)
for (int i = 0; i < someNumber; i++) { // maybe perform some partial loop actions if (someCondition) { continue; } // perform loop action }
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
----------------------------------------------
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
![]() |
Similar Threads
- problem in displayin records on a form (VB.NET)
- Cannot Test For Condition (C#)
- Fill a combobox (Visual Basic 4 / 5 / 6)
- Loops (C++)
- While loop not ending when reading from file (C++)
- Help w/ for loop. (C++)
- Breaking out of a Loop (Java)
- Simple C++ program terminate prematurely (C++)
- error mgs when trying to compute "for loop" (C++)
- While and Do While Help (C++)
Other Threads in the Java Forum
- Previous Thread: servlets
- Next Thread: Ordering HashMap values
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary block bluetooth character chat class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game gameprogramming givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing test textfields threads time title tree tutorial-sample ubuntu update windows working






