Conditionals Explanations

  • If Statement
    • Executes the code inside when the condition is true.
  • If-else statement
    • Executes the code inside the if when the condition is true, otherwise executes the code in the else.
  • If-elseif-else statements
    • Executes the code inside the if when the condition is true, otherwise continues through the if-else blocks until a condition is met. If no conditions are met, then it ends by executing the code in the else block.
import java.util.Scanner;

// Let's try out the conditional statements by playing a small game.
public class AmongUsConditionals {
    public static boolean inCafeteria = false;
    public static boolean inComms = false;
    public static boolean inElectrical = false;
    public static boolean inMedbay = true;
    public static boolean terminate = false;

    public void GiveInfo() {
        // Testing to see where the character is
        if(inCafeteria) {
            System.out.println("Your character is in the cafeteria.");
        }
        else if(inComms) {
            System.out.println("Your character is in the communications room.");
        }
        else if(inElectrical) {
            System.out.println("Your character is in the electrical room.");
        }
        else if(inMedbay) {
            System.out.println("Your character is in the medbay.");
        }
        else {
            System.out.println("Error lol");
            System.exit(0);
        }

        System.out.println("Type cafeteria, communications, electrical, or medbay to move somewhere. Otherwise, you may terminate the function by typing 'exit' ");
    }

    public void MakeFalse() {
        inComms = false;
        inCafeteria = false;
        inElectrical = false;
        inMedbay = false;
    }

    public void Move(String choice) {
        choice = choice.toLowerCase();
        this.MakeFalse();
        if (choice.equals("cafeteria")) {
            inCafeteria = true;
        }
        else if (choice.equals("communications") || choice.equals("comms")){
            inComms = true;
        }
        else if (choice.equals("electrical")){
            inElectrical = true;
        }
        else if (choice.equals("medbay")) {
            inMedbay = true;
        }
        else if (choice.equals("exit") || choice.equals("terminate")) {
            System.out.println("Goodbye.");
            terminate = true;
        }
        else {
            System.out.println("Please choose one of the valid options.");
        }
    }

    public static void main(String[] args) {
        AmongUsConditionals amongUs = new AmongUsConditionals();

        while (!terminate) {
            Scanner sc = new Scanner(System.in);
            amongUs.GiveInfo();
            String choice = sc.nextLine();
            amongUs.Move(choice);
            sc.close();
        }

    }
    
}

AmongUsConditionals.main(null);
Your character is in the medbay.
Type cafeteria, communications, electrical, or medbay to move somewhere. Otherwise, you may terminate the function by typing 'exit' 
Your character is in the electrical room.
Type cafeteria, communications, electrical, or medbay to move somewhere. Otherwise, you may terminate the function by typing 'exit' 
Your character is in the communications room.
Type cafeteria, communications, electrical, or medbay to move somewhere. Otherwise, you may terminate the function by typing 'exit' 

Using Switch Cases

  • Using the switch-case-case-case-... format allows for selection like if/else while making it simpler for those who only want one selection
  • Example by converting the code above to switch-case:
import java.util.Scanner;

// Let's try out the conditional statements by playing a small game.
public class AmongUsConditionals {
    public static boolean inCafeteria = false;
    public static boolean inComms = false;
    public static boolean inElectrical = false;
    public static boolean inMedbay = true;
    public static boolean terminate = false;

    public void GiveInfo() {
        // Prints info for inputting character movement
        System.out.println("Type cafeteria, communications, electrical, or medbay to move somewhere. Otherwise, you may terminate the function by typing 'exit' ");
    }

    public void Move(String choice) {
        choice = choice.toLowerCase();

        switch(choice) {
            case "cafeteria":
            System.out.println("Your character is in the cafeteria.");
                break;
            
            case "communications":
                System.out.println("Your character is in the communications room.");
                break;

            case "comms":
                System.out.println("Your character is in the communications room.");
                break;
            case "electrical":
                System.out.println("Your character is in the electrical room.");
                break;
            case "medbay":
                System.out.println("Your character is in the medbay.");
                break;
            case "terminate":
                System.out.println("Goodbye");
                terminate = true;
                break;
            case "exit":
                System.out.println("Goodbye");
                terminate = true;
                break;
            default:
                System.out.println("Unexpected input, please try again.");
                break;
        }
    }

    public static void main(String[] args) {
        AmongUsConditionals amongUs = new AmongUsConditionals();

        while (!terminate) {
            Scanner sc = new Scanner(System.in);
            amongUs.GiveInfo();
            String choice = sc.nextLine();
            amongUs.Move(choice);
            sc.close();
        }

    }
    
}

AmongUsConditionals.main(null);
Type cafeteria, communications, electrical, or medbay to move somewhere. Otherwise, you may terminate the function by typing 'exit' 
Unexpected input, please try again.
Type cafeteria, communications, electrical, or medbay to move somewhere. Otherwise, you may terminate the function by typing 'exit' 
Your character is in the cafeteria.
Type cafeteria, communications, electrical, or medbay to move somewhere. Otherwise, you may terminate the function by typing 'exit' 
Your character is in the communications room.
Type cafeteria, communications, electrical, or medbay to move somewhere. Otherwise, you may terminate the function by typing 'exit' 
Goodbye

De Morgan's Law

  • Showing off de morgan's law with inputs
import java.util.Scanner;

boolean A = false;
boolean B = false;

Scanner sc = new Scanner(System.in);
try {
    System.out.println("Please choose a boolean value for A");
    A = sc.nextBoolean();
    sc.close();
    sc = new Scanner(System.in);
    System.out.println("Please choose a boolean value for B");
    B = sc.nextBoolean();
    System.out.println("Your selection: A is " + A + " and B is " + B);
}
catch (Exception e) {
    System.out.println("Error, defaulting to false for both variables.");
}

System.out.println("De Morgan's Law Showcase:");

if (!(A && B)) {
    System.out.println("NOT(A and B) = True");
}
else {
    System.out.println("NOT(A and B) = False");
}

if ((!A) || (!B)) {
    System.out.println("NOT(A) or NOT(B) = True");
}
else {
    System.out.println("NOT(A) or NOT(B) = False");
}

if (!(A || B)) {
    System.out.println("NOT(A or B) = True");
}
else {
    System.out.println("NOT(A or B) = False");
}

if ((!A) && (!B)) {
    System.out.println("NOT(A) and NOT(B) = True");
}
else {
    System.out.println("NOT(A) and NOT(B) = False");
}

System.out.println("As you can see, the first and second outputs are the same and the third and fourth outputs are the same.")
Please choose a boolean value for A
Please choose a boolean value for B
Your selection: A is true and B is false
De Morgan's Law Showcase:
NOT(A and B) = True
NOT(A) or NOT(B) = True
NOT(A or B) = False
NOT(A) and NOT(B) = False
As you can see, the first and second outputs are the same and the third and fourth outputs are the same.