+ 1
I dont understand yet why would you want your"if" to fail when you are really coding? Can someone give me a scenario of when i would want it to fail?
9 Réponses
+ 5
Actually, most of the time, you won't know whether the condition you are testing is true or not, and will need to use else when you have to do something different depending of the condition. Also, don't forget that you can use the same if several times in one execution of the code, with variables involved in the condition changed.
function isEven(n) {
if (n % 2 == 0) {
return true;
else
return false;
}
document.write("Is 42 even? " + isEven(42));
document.write("<br>");
document.write("Is 1337 even? " + isEven(1337));
+ 1
there are certain conditions when it is not possible prove ur if condition true like if our x=1 & we code if(x%2==0) then this time the condition has to be false...
+ 1
maybe you want to use the 'if' statement for a password to display a page and when the password is entered incorrectly the 'else' statement will display a sign saying incorrect password.
0
you my know the wrong side of something for you to understand it better
0
it very simple
if (guy understood)
guy passes test
else if(guy didn't understand)
guy fails test
else
why is he even here???!!
0
E.g. You are checking age of user.
If he/she is >= 18
He/she is adult
Else he/she is not adult
0
The if statement doesn't 'fail' in the classical way. There's no negative connotation for code that gets executed when a condition is not met. It is in fact very usefull to know.
I might create a script detecting what browser the user has in case I offer no support for certain browser versions. if thar condition (older browser) returns false, my warning doesn't pop up.
0
You would want if to fail when checking dynamic variables such as user input. If you were using static variables (a variable that you define and does not change) wouldn't usually require and if / else statement anyway.
0
returning false on if statements are mostly used for form validation, where you are actually testing for incorrect values.