IF-ELSE Explanations
Code for IF-ELSE
// All of the boolean expressions below evaluate to true
if (true) {
System.out.println("True code block");
}
if (true && !false) {
System.out.println("True and Not False code block");
}
if (true || false) {
System.out.println("True or False code block");
}
if ((true && !false) && (true || false)) {
System.out.println("Confusing code block");
}
if (!((false || !true) || (false && true))) {
System.out.println("De Morgan's law in my head of confusing code block");
}
// Can any of the above expression be simplified? What would they simplify to? Are any of these expressions useful?
They can all be simplified down to just System.out.println("True");
. Also, these are all useless. Wrapping code in a statment that is guaranteed to be true doesn't make sense. However, if you were to replace true/false with variables they may appear to be useful.
A combination of && and || and ! are useful for many different occasions.
Notes
If, else and booleans are useful. I use a complex set of IF and ELSE statments with boolean parameters in this project. This is an example of my extensive knowledge of if - else statments.
De Morgan's Law Complex conditional statements can be hard to evaluate when looking at "!" or "not" operators, and comparison operators, like >, <, >=, <=, ||, &&, and more
De morgan's law helps by explaining how operators change when a "!" negation is present
Conversions:
<= goes to >
>= goes to <
< goes to >=
> goes to <=
== goes to !=
!= becomes ==