OCJP 6 (93%)
Lorand Komaromi wrote:error < 3 && continueLoop
John de Michele wrote:'S':
No, logical AND (&&) will fail if either the left or right side is false. AND is only true if both sides are true.
John.
John de Michele wrote:'S':
That's why you want to use AND. An AND will fail (== false) if one or the other (or both) is false. So, if your error count is three or more it will fail. It will also fail if continueLoop is false. If both of them are false, it will fail, too. If you use OR, your check will fail only if both your error count is three or more and continueLoop is false.
John.
S Gregg wrote:
John de Michele wrote:'S':
That's why you want to use AND. An AND will fail (== false) if one or the other (or both) is false. So, if your error count is three or more it will fail. It will also fail if continueLoop is false. If both of them are false, it will fail, too. If you use OR, your check will fail only if both your error count is three or more and continueLoop is false.
John.
ok. Point taken but does anyone know why error will not increment when the exception is caught
Steve
Steve Luke wrote:
Why do you think error isn't incrementing with the exception? Nothing in the code you provided tests it.
<edit>
Ok, I see, because when you have while(error<3 || continueLoop), and that condition never returned false to stop the loop. That isn't because error isn't incrementing, it is because continueLoop is true. With the || (OR) operator, the expression is true as long as at least one of the conditions is true. In this case, when error < 3 returns false continueLoop is still true so the loop is allowed to continue. This is why you need to may the comparison using && (AND) because you only want the loop to continue when there are both < 3 errors AND continueLoop is true.
To err is human,
To forgive is not company policy
PrasannaKumar Sathiyanantham wrote:thats great.....
Be sure to design the program first and then code it....(it saves a lot of time)
Somnath Mallick wrote:Would like to ask that you want to give the user 2 tries per entry or two tries on a whole. If you are giving two attempts per entry then the value of error should be initialized to 0 every time the user enters a correct value.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |