Ok, i am building program to check many fields. If at least 1 field is not ok then i don't want my program to spend time to check other fields. So let look at this code:
// Util.isReadyToUse method return true if the string is ready for using, & return false if it is not.
boolean isOK=true;
if(!Util.isReadyToUse(firstName)){
isOK=false;
}
else if(isOK && !Util.isReadyToUse(lastName)){
isOK=false;
}
else if(isOK && !Util.isReadyToUse(email)){
isOK=false;
}
.....more checking
if(isOK) {
//do sthing
}
Ok, when running, the program will first check !Util.isReadyToUse(firstName)
. Suppose it returns (isOK=false). Next the program will check isOK && !Util.isReadyToUse(lastName)
.
So my question here is that Since the isOK
currently false, then will the program spend time to check the condition !Util.isReadyToUse(lastName)
after &&
?
Ok, As a human being, if you see isOK=false
and now you see isOK && !Util.isReadyToUse(email)
, then you don't want to waste time to look at !Util.isReadyToUse(email)
since isOK=false and u saw &&
after isOK
.
Will machine also work like that?
I am thinking to use break
but why people say break
doesn't work in if statement:
if(!Util.isReadyToUse(firstName)){
isOK=false;
break;
}
else if(isOK && !Util.isReadyToUse(lastName)){
isOK=false;
break;
}......
What is the best solution in this situation?