If Statements

Code within an if statement if the condition presented is true.

boolean alcaraz=true;
if (alcaraz=true){ //if Alcaraz wins the US Open mens final
    System.out.print("woohoo! youngest champion since 1990!"); //Alcaraz will become the US open champion since 
}
woohoo! youngest champion since 1990!

If-else Statements

Code within an if statement will execute if the condition is true, otherwise the code within the else statement will execute.

boolean alcaraz=true;
if (alcaraz=true){ //if Alcaraz wins the US Open mens final
    System.out.print("woohoo! alcaraz is youngest champion since 1990!"); //Alcaraz will become the youngest US open champion since Sampras
}
else { //otherwise (if Alcaraz loses)
    System.out.print("Ruud is the first US open champion from Norway!!"); //Ruud will become the first major champion winner from Norway
}
woohoo! alcaraz is youngest champion since 1990!

Else-if Statements

First condition checked with the if statement, if not true, check for condition in the else-if statement and execute if true. Finally, if first two conditions are false, check the else statement and execute code within.

int temp=15;
if (temp<=10) { //if the temperature is less than or equal to 10 degrees following will output
    System.out.print("snuggle up with hot tea and watch the Sound of Music");
}
else if (temp>10 && temp<=25) {//if the temperature if greater than 10 degreed but less than or equal to 25 degrees following will output
    System.out.print("a hike in the wild wild wilderness");
}
else { //any other temperature (greater than 25 degrees) will output
    System.out.print("beach day!");
}
a hike in the wild wild wilderness

5 Condition If

import java.util.Scanner;

public class Greetings {
    public static void main(String[] args) {
        String userInputStr;
        double time;
        Scanner inputStream = new Scanner(System.in);

        System.out.print("What time is it? \n");
        userInputStr = inputStream.nextLine();
        time = Double.parseDouble(userInputStr);

        if (time>=6 && time<=10) {
            System.out.print("Good Morning Sunshine!");
        }
        else if (time>10 && time<=13) {
            System.out.print("Lunch Time!");
        }
        else if (time>13 && time<=17) {
            System.out.print("Good Afternoon!");
        }
        else if (time>17 && time<=20) {
            System.out.print("Dinner Time!");
        }
        else {
            System.out.print("Good Night, Sweet Dreams!");
        }
    }
}

Greetings.main(null);
What time is it? 
Good Night, Sweet Dreams!

Switch Code

The switch expression is evaluated once and value of the expression is compared with the values of each case. If there is a match, the associated block of code is executed.

import java.util.Scanner;

public class Day {
    public void go() {
        Scanner scan = new Scanner(System.in);
        int day = scan.nextInt();

        switch (day) {
            case 1: // case 1, if matches print following
              System.out.println("Monday");
              break;
            case 2:
              System.out.println("Tuesday");
              break;
            case 3:
              System.out.println("Wednesday");
              break;
            case 4:
              System.out.println("Thursday");
              break;
            case 5:
              System.out.println("Friday");
              break;
            case 6:
              System.out.println("Saturday");
              break;
            case 7:
              System.out.println("Sunday");
              break;
            default: // other cases print out following
              System.out.println("That is not a day of the week");
          }
    }

    public static void main(String[] args) {
        Day cond = new Day();
        cond.go();
    }
}

Day.main(null);
Thursday

De Morgan's Law

Writing two statements that mean the same thing. Not is indicated with the "!" symbol

Not (a and b) is the same as Not (a) or Not (b)

!(a && b) = !a || !b

Not (a or b) is the same Not (a) and Not (b)

!(a||b) = !a && !b

if (!((1 == 1) && (1 == 2))) { // if either 1 does not equal 1 or 1 does not equal 2, will print
    System.out.println("1 does not equal 2");
}
else {
    System.out.println ("a miracle has occurred");
}
1 does not equal 2
if (!((1 == 1) || (1 == 2))) { // if 1 is equal to 1 or equal to 2 will print
    System.out.println("a miracle has occurred");
}
else {
    System.out.println ("1 does equal 1");
}
1 does equal 1