If Statement

  • an if statement is a conditional piece of code that executes a certain procedure once a condition is met
if (condition) {
    //output
}
else {
    //ouput
}

Else If Statement

  • an if else statement is a conditional piece of code as well but if the condition is not met, then it executes a different procedure
if (condition) {
    //output
}
else if (other condition) {
    //ouput
}

If Elseif Else Statement

  • an if else-if else is a conditional piece of code that executes a certain procedure but if the condition is not met, it executes a different conditional, but if that is not met either, then it executes the last else
if (condition) {
    //output
}
else if (other condition) {
    //ouput
}
else {
    //output
}

If Statements CheckAnswer Function

class CheckAnswer {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("What is the country of the capital?");
        String answer = scanner.nextLine();
            if (answer.equals ("Santiago") ) {
                System.out.println("That is the capital of Chile!");
            }
            else if (answer.equals ("Bogota") ) {
                System.out.println("That is the capital of Columbia!");
            }
            else if (answer.equals ("Lima") ) {
                System.out.println("That is the capital of Peru!");
            }
            else if (answer.equals ("Buenos Aires") ) {
                System.out.println("That is the capital of Argentina!");
            }
            else if (answer.equals ("Quito") ) {
                System.out.println("That is the capital of Ecuador!");
            }
            else if (answer.equals ("Caracas") ) {
                System.out.println("That is the capital of Venezuela!");
            }
            else {
                System.out.println("This is not a valid capital!");
            }
    } 
}
CheckAnswer.main(null);
What is the country of the capital?
That is the capital of Peru!

Switch Case

  • Switch case is another type of conditional statement that is more prevalent in menus to select a certain value that executes a code. Can also be used for many elseifs all contingent on a singular variable.
public class SwitchCase {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter a single digit value"); //user input for array
        int x = scanner.nextInt();
        switch (x){
            case 0:
                //If case is 0, this block executes
                System.out.println("Absolute Value was 0");
                break;
            case -1:
                //If case is 1, this block executes
                System.out.println("Absolute Value was 1");
                break;
            case -2:
                //If case is 2, this block executes
                System.out.println("Absolute Value was 2");
                break;
            case -3:
                //If case is 3, this block executes
                System.out.println("Absolute Value was 3");
                break;
            case -4:
                //If case is 4, this block executes
                System.out.println("Absolute Value was 4");
                break;
            case -5:
                //If case is 5, this block executes
                System.out.println("Absolute Value was 5");
                break;
            case -6:
                //If case is 5, this block executes
                System.out.println("Absolute Value was 5");
                break;
            case -7:
                //If case is 5, this block executes
                System.out.println("Absolute Value was 8");
                break;
            case -8:
                //If case is 5, this block executes
                System.out.println("Absolute Value was 8");
                break;
            case -9:
                //If case is 5, this block executes
                System.out.println("Absolute Value was 9");
                break;
            default:
                //This block is similar to else statement, is the default operation
                System.out.println(x);
        }
    }
}

SwitchCase.main(null);
Enter a single digit value
8

De Morgan's Law

  • De Morgan's law is an action you can do to a conditional that if you take the not(!) of a condition, it flips all the trues to false and the and(&&) operators to or(||) or vice versa
!(a && b) == (!a || !b)
!(a || b) == (!a && !b)
if ((true) && !(false)) {
    System.out.println("valid");
}
valid

not operator on the whole expression causes the true and false to flip and the && and || to flip

if (!(false) || (true)) {
    System.out.println("opposite demonstrating de morgans law");
}
opposite demonstrating de morgans law