Skip to content

Instantly share code, notes, and snippets.

@raineorshine
Created March 20, 2013 00:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raineorshine/5201426 to your computer and use it in GitHub Desktop.
Save raineorshine/5201426 to your computer and use it in GitHub Desktop.
Logic Practice: Equivalent Conditionals (beginner)
// 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