If Statement

The if statement is used to test whether a condition is met or not. If the condition is true, the following command will be run.

boolean a = true;
// Set the boolean to "true"

if (a = false) {
    System.out.println("This is false");
    // Will run if a is false
}

if (a = true) {
    System.out.println("This is true");
    // Will run if a is true
}
This is true

If-else Statement

The if-else statement will run a piece of code if a certain condition is met. If this condition is not met then another command will be run.

boolean b = false;
// Set the boolean to "false"

if (b = true) {
    System.out.println("b is true");
}
else {
    System.out.println("b is not true");
}
// The if condition is not met so the else statement is ran
b is true

If-elseif-else Statement

An if-elseif-else statement will determine whether a certain condition is true. If it isn't it will go to the next elseif statement and do the same thing. If this condition is also false then it will go down and continue to go through conditions until one is true.

boolean c = true;
// Sets the boolean to "true"

if (c = false) {
    System.out.println("The first if statement is true");
    // If the if condition is not true then it will not run
}
else if (c = !true) {
    System.out.println("This else if statement is true");
}
else if (c = false && true) {
    System.out.println("This else if statement is true");
}
else if (c = !(!false || !true)) {
    System.out.println("This else if statement is true");
    // If the else if condition is not true then then it will not run
}
else {
    System.out.println("None of the other conditions were true");
    // None of the if statements or else if statements are true so the else statement will run
}
None of the other conditions were true

If-elseif-else statement (5 or more conditions)

int stars = 3; // The integer "stars" is determined to be 3

if (stars <= 0) {
    System.out.println("You got ☆☆☆☆☆");
}
else if (stars <= 1) {
    System.out.println("You got ★☆☆☆☆");
}
else if (stars <= 2) {
    System.out.println("You got ★★☆☆☆");
}
else if (stars <= 3) {
    System.out.println("You got ★★★☆☆");
}
else if (stars <= 4) {
    System.out.println("You got ★★★★☆");
}
else {
    System.out.println("You got ★★★★★");
}
// The code goes through the if and else if conditions to figure out how many stars to show.
You got ★★★☆☆

Switch Case

A switch case is able to run one statement after choosing one from multiple statements. This makes it similar to an else-if ladder where it will go through mulitple conditions to see which is true.

When making a switch case code, it is important that you make sure no two cases have the same value and that the same variable is used for all the cases.

int stars = 4; // The integer "stars" is determined to be 3

switch (stars) { // A switch statement that takes in the "stars" integer
    case 1: // When there are 0 stars
        System.out.println("You got ☆☆☆☆☆");
        break;
    case 2: // When there is 1 star
        System.out.println("You got ★☆☆☆☆");
        break;
    case 3: // When there are 2 stars
        System.out.println("You got ★★☆☆☆");
        break;
    case 4: // When there are 3 stars
        System.out.println("You got ★★★☆☆");
        break;
    case 5: // When there are 4 stars
        System.out.println("You got ★★★★☆");
        break;
    default: //When none of the other cases are met so there are 5 stars
        System.out.println("You got ★★★★★");
    }
You got ★★★☆☆

De Morgan's Law

De Morgan's Law states that the "!" symbol will negate an And statement and an Or statement. This means that if a condition is written as a || b, !(a || b) would really be a && b. This goes for the other way around where !(a && b) is a || b. It will also cause a true statement to be false with !(true) and vice versa.

boolean child = true;
boolean hungry = false;

if (!(child && hungry)) {
    System.out.println("You have a hungry child");
}
else {
    System.out.println("Who knows what's going on?");
}
You have a hungry child
boolean fruit = true;
boolean red = true;

if (!(!(fruit) || !(red))) {
    System.out.println("It's probably a red apple");
}
else {
    System.out.println("You have an unknown food");
}
It's probably a red apple

Resources used

  1. CodeHS
  2. simplilearn
  3. w3schools