Created
March 20, 2013 00:37
-
-
Save raineorshine/5201426 to your computer and use it in GitHub Desktop.
Logic Practice: Equivalent Conditionals (beginner)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// all three of the below code snippets are equivalent! | |
// #1: If you notice the body of your 'if' is empty but your else has code, then convert it to #2 | |
if(A && B) | |
{ | |
// do nothing | |
} | |
else | |
{ | |
doSomething(); | |
} | |
// #2: By negating the condition, we get rid of the empty 'if' and move the else into the if. | |
if(!(A && B)) | |
{ | |
doSomething(); | |
} | |
// #3 - We can also distribute the ! operator- just remember to reverse the &&! This is called DeMorgan's Law. | |
if(!A || !B) | |
{ | |
doSomething(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment