What is an If Statement

If statements are blocks of code that allow you to make decisions in your code. They allow you to test for different conditions, and run code depending on the output of those conditions. If the statement that is put into an if is true, it will execute the code in the block, if it is not true, it will run the code in the else block.

if(condition) {
    // code block that will run if condition being tested for is true
} else {
    // code block for that will run if condition being tested for is not true(false)
}

Else If

Else ifs allow you to add multiple cases that can be true, rather than just the conventional if and else blocks.

if (condition) {
    // runs if condition 1 is true
} else if (condition2) {
    // runs if condition 2 is true
}else if (condition 3) {
    // runs if condition 3 is true
} else {
    // runs if none of the above code blocks evaluate to true
}

If Else and Switch Statements

You can also use something called a switch statements, which takes different cases, similar to how an if- if else- else statement works.

import java.util.Random;

public class IfStatement {
    public static void main(String[] args) {
        //Created random and getting random number
        Random rand = new Random();
        int randNum =  rand.nextInt(10);
        if (randNum == 0) {
            //If number is 0, this block executes
            System.out.println("Random number was 0");
        } else if (randNum == 1) {
            //If number is 1, this block executes
            System.out.println("Random number was 1");
        } else if (randNum == 2) {
            //If number is 2, this block executes
            System.out.println("Random number was 2");
        } else if (randNum == 3) {
            //If number is 3, this block executes
            System.out.println("Random number was 3");
        } else if (randNum == 4) {
            //If number is 4, this block executes
            System.out.println("Random number was 4");
        } else if (randNum == 5) {
            //If number is 5, this block executes
            System.out.println("Random number was 5");
        } else {
            //If none of the above conditions are true, this block will execute
            System.out.println("Random number was not 1-5");
        }
    }
}


IfStatement.main(null);
Random number was 3

Same code, but with a switch statement

import java.util.Random;

public class SwitchStatement {
    public static void main(String[] args) {
        //Created random and getting random number
        Random rand = new Random();
        int randNum =  rand.nextInt(10);
        switch (randNum){
            case 0:
                //If case is 0, this block executes
                System.out.println("Random number was 0");
                break;
            case 1:
                //If case is 1, this block executes
                System.out.println("Random number was 1");
                break;
            case 2:
                //If case is 2, this block executes
                System.out.println("Random number was 2");
                break;
            case 3:
                //If case is 3, this block executes
                System.out.println("Random number was 3");
                break;
            case 4:
                //If case is 4, this block executes
                System.out.println("Random number was 4");
                break;
            case 5:
                //If case is 5, this block executes
                System.out.println("Random number was 5");
                break;
            default:
                //This block is similar to else statement, is the default operation
                System.out.println("Random number was not 1-5");
        }
    }
}


SwitchStatement.main(null);
Random number was 4

De Morgan's Law

De Morgan's Law is a concept that shows that concepts in mathematics are related through their opposites. For example: not (A and B) -- not A or not B not (A or B) -- not A and not B

!(a && b) == (!a || !b)
!(a || b) == (!a && !b)
if(!(true)&&(true==false)){
    System.out.println("Demonstrates De Morgans law");
}