Boolean Expressions

  • Booleans are either one of two instances. True/False or On/Off
  • A boolean type is declared with the boolean keyword and can only take the values true or false
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");
}
True code block
True and Not False code block
True or False code block
Confusing code block
De Morgan's law in my head of confusing code block

If/Elseif/Else Statements

  • If statement to specify a block of Java code to be executed if a condition is true
  • Else statement to specify a block of code to be executed if the condition is false
  • Else if statement to specify a new condition if the first condition is false.
int time = 22;
// if statement
if (time < 10) {
  System.out.println("Good morning.");
} 
// else if statement
else if (time < 20) {
  System.out.println("Good day.");
}
// else statement
else {
  System.out.println("Good evening.");
}
Good evening.

De Morgan's Law

  • Two definitions
    • De Morgan's Law of Union: The complement of the union of the two sets A and B will be equal to the intersection of A' (complement of A) and B' (complement of B). This is also known as De Morgan's Law of Union. It can be represented as (A ∪ B)' = A' ∩ B'.
    • Laws that define how we can negate an AND statement and how we can negate an OR statement. De Morgan's Laws simply state: !( a && b) is equivalent to !a || !
if (!(!false && !true) || (!(!false || false))) {
    System.out.println("De Morgan's Law hurts my head!");
}
De Morgan's Law hurts my head!

Hack 2 - Add to lesson switch-case

public class Main {
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        
        // sets the variable "day" as user input
        int day = sc.nextInt();

        System.out.println("Pick an number between 1 and 7 " + day);
        
        // checks if the user inputted number is 1
        // If not, it runs through the rest of the statements
        if(num = 1){
            System.out.println("The day is Monday");
        }
        else if(num = 2){
            System.out.println("The day is Tuesday");
        }
        else if(num = 3){
            System.out.println("The day is Wednesday");
        }
        else if(num = 4){
            System.out.println("The day is Thursday");
        }
        else if(num = 5){
            System.out.println("The day is Friday");
        }
        else if(num = 6){
            System.out.println("The day is Saturday");
        }
        else if(num = 7){
            System.out.println("The day is Sunday");
        }
        else{
            System.out.println("Not a valid entry");
        }

    }
}
Main.main(null);
Pick a number: 26
26 is less than 30

Converted to switch-case-case-case-case-otherwise

public class Switch {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        
        int day = sc.nextInt();

        // Asks user for a number of their choice
        System.out.println("Pick an number between 1 and 7: " + day);

        // the day is the conditional variable that is tested
        switch (day) {

// The switch case checks multiple conditions at once.
// Switch is provided with an expression that can be a constant or literal expression that can be evaluated.
// The value of the expression is matched with each test case till a match is found.
// If there is no match then the "default" code runs.

            case 1:
                System.out.println("The first day is Monday");
                break;
            case 2:
                System.out.println("The second day is Tuesday");
                break;
            case 3:
                System.out.println("The third day is Wednesday");
                break;
            case 4:
                System.out.println("The fourth day is Thursday");
                break;
            case 5:
                System.out.println("The fifth day is Friday");
                break;
            case 6:
                System.out.println("The sixth day is Saturday");
                break;
            case 7:
                System.out.println("The seventh day is Sunday");
                break;
            // Our last test is if the number is not between 1 and 7.
            default:
                System.out.println("Not a valid entry. Choose a number between 1 and 7.");
        }

    }
}
Switch.main(null);
Pick an number between 1 and 7: 3
The third day is Wednesday