If statement

If statements allow you to run a certain piece of code if a specified condition is true.

if (condition) {
    //run this code if the condition is true
}
// Here is an example
int num1 = 10;
int num2 = 20;

if (num1 < num2) {
    System.out.println("10 < 20 is true");
}


if (num1 > num2) {
    //this statement will not be printed as the condition is false
    System.out.println("10 > 20 is true");
}
10 < 20 is true

Else statement

Else statements allow you to run a certain piece of code if the specified condition is false.

if (condition) {
    //run this code if the condition is true
} else {
    // run this code if the condition is false
}
// Going back to the previous example
int num1 = 10;
int num2 = 20;

if (num1 < num2) {
    System.out.println("10 < 20 is true");
} else {
    //this statement will not be printed as the condition above was true so this code will never run
    System.out.println("10 < 20 is false");
}

if (num1 > num2) {
    System.out.println("10 > 20 is true");
} else {
    //this statement will  be printed as the condition above was false so this code will run
    System.out.println("10 > 20 is false");
}
10 < 20 is true
10 > 20 is false

Else-if statement

Else-if statements allow you to give backup conditions if the previous condition is false and run different pieces of code depending on if each of the conditions are true or false

if (firstCondition) {
    //run this code if firstCondition is true
} else if (secondCondition) {
    // run this code if firstCondition is false and secondCondition is true
} else {
    // run this code if firstCondition is false and secondCondition is false
}
// Going back to the previous example
int num1 = 10;
int num2 = 20;

if (num1 > num2) {
    // num1 > num2 is false so this code will not run and will move on to check the next condition
    System.out.println("10 > 20 is true");
} else if (num1 < num2) {
    // this statement will be printed as the condition above was false and this conditions is true 
    System.out.println("10 < 20 is true");
//any number of else-if statements can be used
} else {
    // this statement will never be printed as the previous condition was true
    System.out.println("10 = 20 is true");
}
10 < 20 is true

switch-case and switch statements

switch-case allows you to run certain blocks of code depending on the expression specified.

switch(expression) {
    case case1:
        //code
        break;
    case case2:
        //code
        break;
    case case3:
        //code
        break;
    //any number of case statements can be used
    default:
        //code
}

The expression specified is checked and compared with each case and if the expression matches a case, the code under that case will run.

break statements are often used with switch-case. break exits the loop it is in and move on to the next part of the code.

The code under default will run if none of the cases are true.

Below is an example of taking an if-elseif-else statement with multiple conditions and converting it to a switch-case statement

If-elseif-else

boolean raining = true;
boolean cold = false;
boolean windy = true;

if (cold) {
    //checks if cold is true but it's false so moves on to next condition
    System.out.println("It is cold");
} else if ( cold && windy ) {
    //cold is false and windy is true so the condition is false
    System.out.println("It is cold and windy");
} else if ( raining && cold) {
    // raining true but cold false so condition is false
    System.out.println("It is raining and it is cold");
} else if ( raining && cold && windy) {
    // again, cold is false so the condition is false
    System.out.println("It is raining, windy, and cold");
} else if ( windy && cold) {
    // again, cold is false so the condition is false
    System.out.println("It is windy and it is cold");
} else if ( raining && windy) {
    // prints out this statement since both conditions are true
    System.out.println("It is windy and it is raining");
}
else {
    // doesn't get to this part of the code since the previous condition was true
    System.out.println("It could be raining, cold, and/or windy");
}
It is windy and it is raining

Switch-case

String weather = "windy and raining";
switch (weather) {
    case "cold":
        // checks if weather has a value of "cold" --> it doesn't so moves on to check next case
        System.out.println("It is cold");
        break; 
    case "raining":
        System.out.println("It is raining");
        break;
    case "raining and cold":
        System.out.println("It is raining and it is cold");
        break; 
    case "raining and cold and windy":
        System.out.println("It is raining, windy, and cold");
        break;
    case "windy and cold":
        System.out.println("It is windy  and it is cold");
        break;
    case "windy and raining":
        // this case matches the String weather so this statement is printed out
        System.out.println("It is windy and it is raining");
        break;
    default:
        // doesn't run this code since a case matched the expression 
        System.out.println("It could be raining, cold, and/or windy");

}
It is windy and it is raining

De Morgan's Law

De Morgan's Law says that "the complement of the product of all terms is equal to the sum of the complement of each term" and shows how the NOT (!) operator can be distributed. For example, !(A && B) is the same as !A || !B. !(C > D) is the same as C <= D.

boolean cold = false;
boolean windy = true;

//this first if-else statement checks if both booleans are true and then reverse the result 
if (!(cold && windy)) {
    System.out.println("It is not both cold and windy");
} else {
    System.out.println("The condition is false");
}

//this first if-else statement checks if cold is not true or if windy is not true 
if (!cold || !windy) {
    System.out.println("It is not both cold and windy");
} else {
    System.out.println("The condition is false");
}

// Based on De Morgan's Law, both if-else statements provide the same output
It is not both cold and windy
It is not both cold and windy