If Statements

This type of conditional statement is composed of the idea that if a specific condition is met, then an action/code will be executed. Just check to see if something is true and will execute the code accordingly.

if (score >= 90) {//  the condition here is the score received on the test
   grade = 'A'; // if the score is greater than 90, the action executed will be telling the user they got an A grade; if it's not true, the rest of the code will be executed and ignore what is between the brackets
}

If-else Statements

A conditional statement that runs a separate (else) code if the statement is false. It's a default action.

if (score >= 90) {//  the condition here is the score received on the test
    grade = 'A'; // if the score is greater than 90, the action executed will be telling the user they got an A grade; if it's not true, the rest of the code will be executed and ignore what is between the brackets
 }
 else {
    grade = 'Lower than an A'; // if the score is less than a 90, then this alternate code will run
 }

If-else If-else Statement

This conditional statement allows you to have a string of codes if all the specific conditions are false

if (time < 10) { // the condition here is the time 
    greeting = "Good morning"; // if the time is less than 10, the code will be executed
  } else if (time < 20) { // the second condition is here if the number is not less than 10; it must be less than 20 for the code to run
    greeting = "Good day"; // if the time is less than 20 but greater than 10, the code will be executed
  } else { // if the number is greater than 20, then this alternate code will run since the first two statements were false
    greeting = "Good evening"; // this code will be executed if the number is greater than 20
  }

If-else 5 conditions example

Here is a code I wrote

import java.util.Scanner;

public class IfElseStatements {
    public void go() {
        Scanner scan = new Scanner(System.in);
        int score = scan.nextInt();
        if (score <= 60) {
            System.out.println("You got an F");
        } else if (score <= 70) {
            System.out.println("You got a D");
        } else if (score <= 80) {
            System.out.println("You got a C");
        } else if (score <= 90) {
            System.out.println("You got a B");
        } else if (score <= 100) {
            System.out.println("You got an A");
        } else {
            System.out.println("You didn't even get a grade!");
        }
    }

    public static void main(String[] args) {
        IfElseStatements cond = new IfElseStatements();
        cond.go();
    }
}
IfElseStatements.main(null);
You got an A

Switch Case

A switch case is an if-else ladder that checks multiple conditions at once. The value that is inputted is checked with each given case until a match is found. Things to remember: two cases can't have the same value, data variable needs to be the same in all cases, and the value needs to be literal or constant not a variable.

import java.util.Scanner;

public class SwitchExample { // sets up class and objects for scanner
    public void go() {
        Scanner scan = new Scanner(System.in);
        int score = scan.nextInt();
 
        switch (score){ // switch statement with condition that goes through different cases to find a match
            case 1: // for the score of less than 60
                    System.out.println("You got an F");
                    break;    
            case 2: // for the score  between 60 and 70
                System.out.println("You got a D");
                break;
            case 3: // for a score between 70 and 80
                System.out.println("You got a C");
                break;
            case 4: // for a score between an 80 and a 90
                System.out.println("You got a B");
                break;
            case 5: // for a score between a 90 and 100
                System.out.println("You got an A");
                break;
            default: // if none of the cases are a match, this is what will be the code automatically executed
                System.out.println("You didn't even get a grade");
        }
    }

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

SwitchExample.main(null);
You got an F

De Morgan's Law

Allows for specific Boolean statements to be written in different ways to have the same effect. It's similar to probability in statistics. The || means "or" and the && means "and"

Written as: not (A or B) = (not A) and (not B)

not (A and B) = (not A) or (not B)

In code it's written as:

!(A && B) == !A || !B !(A && B) == !A && !B

boolean dog = false;
boolean labrador = true;

if (!(dog && windy)) { // using the "and" 
    System.out.println("You have a dog that's a labrador");
}
else {
    System.out.println ("Your dog is another breed");
}
// checks if both boolean expressions are true and then inverses the results
You have a dog that's a labrador
boolean dog = false;
boolean labrador = true;

if (!dog || !labrador) { // using the "or" 
    System.out.println("You have a dog that's a labrador");
}
else {
    System.out.println ("Your dog is another breed!");
}
// checks if dog is false or labrador is false
You have a dog that's a labrador