If and Else Statements (Booleans)
boolean a = true;
boolean b = false;
System.out.println("variable a: " + a + "\nvariable b: " + b);
Boolean Operators
There are three basic boolean operators. They are AND (&&), OR (||), and NOT (!).
With AND, both (all) statements must be true to produce and output of true.
With OR, only one of the statements must be true to produce and output of true.
With NOT, boolean outputs are reversed (true is false, false is true).
There are also comparison operators, which include the mathematical symbols >, <, >=, <=, and ==. These operators compare the numerical value of two numbers (integers or doubles), and provides a boolean output as to whether or not the statement is true or not.
Let's see these operators in some code.
boolean a = true && true; //both are true
boolean b = true && false; //not both are true
boolean c = true || false; //one is true
boolean d = false || false; //neither are true
boolean e = !true; // opposite of true
System.out.println("variable a: " + a + "\nvariable b: " + b + "\nvariable c: " + c + "\nvariable d: " + d + "\nvariable e: " + e);
boolean a = 5 > 1; //true statement
boolean b = 10 < 3; //false statement
boolean c = 7 >= 7; //true statement
System.out.println("variable a: " + a + "\nvariable b: " + b + "\nvariable c: " + c);
If and Else Statements
If statements are statements that check if a condition is true or false. If a condition is true, it will perform an action/line of code it contains, 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 and only if the condition for the if statement is not true (false).
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.
int a = 3;
int b = 8;
int c = 10;
if (a > b) { //this is false, so this code is ignored, else statement is followed
if (a > c){
System.out.println("a is the greatest number");
}
else {
System.out.println("c is the greatesst number");
}
}
else { //since if was false, "else" is true
if (b > c) {//this is false, so this code is ignored, else statement is followed
System.out.println("b is the greatest number");
}
else { //if was false, this code is followed
System.out.println("c is the greatest number");
}
}
Switch Case Statements
Switch Case Statements help simplify/manage the complexity of if else statements. Unlike if-then and if-then-else statements, the switch statement can have a number of possible execution paths.
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 condition is met. If it is met, the case's block of code will run and ignore the rest of the Switch Case statements (assuming each case is ended with a break statement).
int n = 3;
String output;
switch (n) { //variable passed in
case 1: //condition checked if equals
output = "the number is 1";
break;
case 2:
output = "the number is 2";
break;
case 3:
output = "the number is 3";
break;
case 4:
output = "the number is 4";
break;
}
System.out.println(output);
The break statement is crucial, as it terminates the enclosing switch statement. The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered
This is an example without break statements.
int n = 3;
String output;
switch (n) {
case 1:
output = "the number is 1";
case 2:
output = "the number is 2";
case 3:
output = "the number is 3";
case 4:
output = "the number is 4";
}
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.
De Morgan's Law defines how we can negate an AND statement and how we can negate an OR statement.
!(a && b) is equivalent to !a || !b !(a || b) is equivalent to !a && !b
where a and b are boolean values
For example, the statements "I don't like chocolate or vanilla'' and "I do not like chocolate and I do not like vanilla'' clearly express the same thought.
Example code:
boolean a = true;
boolean b = false;
if (!(a || b)){
System.out.println("A and B both are false");
}
else{
System.out.println("A and B are true");
}
boolean a = true;
boolean b = false;
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.