If Else Statements and Boolean Logic
The logic behind booleans if else statements switch case and De Morgan's Law
Booleans
Booleans are one of the primitive data types that Java supports and are an integral data type to all of computer science. In fact, it's the idea of booleans that actually makes up an integral part of computers which we know as 0s and 1s (on or off) which in computer speak is called binary.
Established by 19th century mathematician George Bool, booleans are at its core data for logic simply put in two types: true or false. With this you can create a complex structure of computing code and all other manners of code.
Let's see how they are when we assign them as variables!
boolean a = true;
boolean b = false;
System.out.println("variable a: " + a + "\nvariable b: " + b);
Boolean Operators
Booleans, interestingly enough, have operators to them just as math does. The most common operators you'll hear about are the comparison operators: and (&&), or (||), >, <, >=, <=, ==. Another common operator is not (!).
The and operator compares two boolean statements and if both are true, then it will return true, otherwise all other combinations of the boolean statements return false.
The or operator also compares two boolean statements and if one of them is true, it returns true while if both of them is false, it will return false.
The not operator is applied to a boolean statement and switches true to false and false to true.
Let's see them in action!
// examples of some of the operators
boolean a = true && true;
boolean b = false && true;
boolean c = 100 < 150;
boolean d = 20 >= 1;
boolean e = !b;
boolean f = false || true;
System.out.println("variable a: " + a + "\nvariable b: " + b + "\nvariable c: " + c + "\nvariable d: " + d + "\nvariable e: " + e + "\nvariable f: " + f);
If and Else Statements
If statements are, at their core, statements that check the conditions of true or false. If a condition is true, it will perform an action/line of code, if it is false, it will skip over the action/code block under the if statement.
An Else statements is an action/code block that performs if the condition for the if statement is not true.
An Else If statement is a combination of If and Else statements where if a condition is not true, it will move into the else statement which has another if statement. At its core. it is a nested If Else statement.
Let's see them in action.
int a = 11;
int b = 6;
int c = 9;
if (a > b){ //since a is greater than c, the else statement is ignored
if (a < c){
System.out.println("c is the largest number");
}
else{ //since a is greater than c, the else statement's code block is called because the if statement is false
System.out.println("a is the largest number");
}
}
else{
if (b < c){
System.out.println("c is the largest number");
}
else{
System.out.println("b is the largest number");
}
}
Switch Case Statements
Switch Case Statements help simplify/manage the complexity of if else statements which can in many circumstance become overly long especially when comparing by a case by case basis.
It works by stating a variety of Case statements which all have conditions. The code blocks under each case are then run if the condition to one of the cases is met. Just like if else statements, Switch Case statements will go down each case to see if a conditon is met. If it is met, the case's block of code will run and ignore the rest of the Switch Case statements.
In the examples below, the first will be a chain of If Else statements while the second example will use switch case to demonstrate how Switch Case statements simplify multiple If Else statements.
int n = 7;
if (n == 4){
System.out.println("4 is considered unlucky in Japan because of its pronounciation's (shi) similarity to the word for death.");
}
else if (n == 7){
System.out.println("7 is considered lucky in many cultures because its believed to be associated with its propagation as a positive value in folklores and myths.");
}
else if (n == 8){
System.out.println("8 is considered a lucky number in China and Japan because of its relation to the word for prosperity in their cultures.");
}
else if (n == 9){
System.out.println("9 is considered unlucky in both China and Japan because both associate it with the word for sorrow, grief, and suffering.");
}
else if (n == 13){
System.out.println("13 is considered unlucky in Western society due to its association with the Christian Bible where the 13th to sit at the Last Supper was Judas who is known as the betrayer of Jesus Christ.");
}
int n = 7;
String output;
switch (n){ //variable passed in
case 4: //condition checked if equals
output = "4 is considered unlucky in Japan because of its pronounciation's (shi) similarity to the word for death.";
break;
case 7:
output = "7 is considered lucky in many cultures because its believed to be associated with its propagation as a positive value in folklores and myths.";
break;
case 8:
output = "8 is considered a lucky number in China and Japan because of its relation to the word for prosperity in their cultures.";
break;
case 9:
output = "9 is considered unlucky in both China and Japan because both associate it with the word for sorrow, grief, and suffering.";
break;
case 13:
output = "13 is considered unlucky in Western society due to its association with the Christian Bible where the 13th to sit at the Last Supper was Judas who is known as the betrayer of Jesus Christ.";
break;
}
System.out.println(output);
De Morgan's Law
De Morgan's Law states that in a boolean statement, the not operator ! will reverse all the operators in that statement. For instance, true becomes false, false becomes true, and becomes or, or becomes and, > becomes <=, <= becomes >, etc. A tangible example of this is 'I will not drink water and eat a table' which means the same thing as 'I will either not drink water or eat a table.'
Here's some examples!
boolean a = true;
boolean b = true;
if (!(A && B)){
System.out.println("A and B both are false");
}
else{
System.out.println("A and B are true");
}
if (!A || !B){
System.out.println("A and B both are false");
}
else{
System.out.println("A and B are true");
}
As shown above, both statements are equal given De Morgan's Law. It's important because of how they can break inversions as a complement of a complex boolean expression.