| | |
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 |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree trolltech unlimited utility webservices windows






