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? | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 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?

31st Aug 2016, 1:42 AM
David Khan
9 Respuestas
+ 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));
31st Aug 2016, 9:35 AM
Zen
Zen - avatar
+ 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...
31st Aug 2016, 3:58 AM
Henil Mamaniya
Henil Mamaniya - avatar
+ 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.
5th Sep 2016, 3:05 PM
noorudeen
noorudeen - avatar
0
you my know the wrong side of something for you to understand it better
3rd Sep 2016, 8:28 PM
Daniel kabeya kakona
Daniel kabeya kakona - avatar
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???!!
17th Oct 2016, 11:44 AM
Jorgie Mathew
Jorgie Mathew - avatar
0
E.g. You are checking age of user. If he/she is >= 18 He/she is adult Else he/she is not adult
29th Nov 2016, 2:05 AM
Syed NomanulHasan
Syed NomanulHasan - avatar
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.
20th Dec 2016, 3:01 PM
Stefaan Doomen
Stefaan Doomen - avatar
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.
20th Dec 2016, 11:00 PM
Robert Jones
Robert Jones - avatar
0
returning false on if statements are mostly used for form validation, where you are actually testing for incorrect values.
13th Jan 2017, 10:08 AM
Andre van Rensburg
Andre van Rensburg - avatar