If Statement

  • Runs a specific set of code if a condition is met.

    • This conditions can be anything but it primarily composed of variables such as:
      • Booleans
      • Integers
      • Floats
      • Etc...
if(condition) {

    examplecode();
    
}

If/Else Statement

  • Runs a specific set of code if a condition is met. OTHERWISE if the condition is not met then it runs a separate set of code.

    • This conditions can be anything but it primarily composed of variables such as:
      • Booleans
      • Integers
      • Floats
      • Etc...
if(condition) {

    runprimarycodehere();

}else{

    runsecondarycodehere();

}

If/Else Example

  • This is an if statement testing if the variable is below 0. If it is below 0 then it prints a message stating it's negative. If it's not above 0 then it prints a message stating it's positive.
import java.util.Scanner;
Scanner scanner = new Scanner(System.in);

int number;

System.out.print("Enter a number: ");
// Get input
number = scanner.nextInt();

System.out.println(number);
// Check if number is less than 0
if(number < 0) {

    System.out.println(number + " is negative");

}else{

    System.out.println(number + " is positive");

}
Enter a number: 15
15 is positive

If-elseIf-else Statement

  • Runs an if statement for a condition. If the condition is met then a set of code is run. If the condition is not met then a separate if statement is run. If that condition is met then a set of code is ran but if not then the else code is run.

  • In this example, we made the positive negative calculator more specific. It now tests if the number is 0 first. If it is then it states that it's not negative or positive. While if it's not then it tests if the number is greater than 0 in which case it's positive. If not then it's negative.

import java.util.Scanner;
Scanner scanner = new Scanner(System.in);

int number;

System.out.print("Enter a number: ");

number = scanner.nextInt();

System.out.println(number);

//Check if number equals 0
if(number == 0) {

    System.out.println(number + " is not negative or positive");
    
//If it doesn't then check if number is greater than 0.
}else if (number > 0) {

    System.out.println(number + " is positive");

}

else {

    System.out.println(number + " is negative");

}
Enter a number: -25
-25 is negative

If/Else example with 5 or more Conditions

  • This is an example of a calculator that identifies if the number is positive or negative and if's it greater than or less than 100 relative to signs.
import java.util.Scanner;
Scanner scanner = new Scanner(System.in);

int number;

System.out.print("Enter a number: ");

try {
    
    // Get input
    number = scanner.nextInt();

    System.out.println(number);

    // Check if number is equal to zero
    if(number == 0) {

        System.out.println(number + " is 0");

    // If it's not then check if number is greater than 0
    }else if (number > 0) {

        if(number > 100) {

            System.out.println(number + " is positive and greater than 100");

        }
        else {

            System.out.println(number + " is positive and less than 100");

        }
    }

    // If number is less than 0...
    else if (number < 0) {

        if(number > -100) {

            System.out.println(number + " is negative and greater than -100");

        }
        else {

            System.out.println(number + " is negative and less than -100");

        }

    }

} catch (Exception e) {  
    System.out.println("Input must be an integer");
}
Enter a number: -250
-250 is negative and less than -100

Switch-case Example

  • In this example, we see how instead of using if statements, we use cases from inputs. We input a message and then select a number corresponding to a color. This final output then prints your message in your chosen color.
import java.util.Scanner;
Scanner scanner = new Scanner(System.in);

int number;
String message;

//Setup of COLOR variables
public static final String RED = "\u001B[31m";
public static final String GREEN = "\u001B[32m";
public static final String YELLOW = "\u001B[33m";
public static final String BLUE = "\u001B[34m";
public static final String PURPLE = "\u001B[35m";
public static final String CYAN = "\u001B[36m";
public static final String WHITE = "\u001B[37m";

// Message Input
System.out.print("Enter a message: ");
message = scanner.nextLine();
System.out.print(message + "\n\n");
// Menu for custom color.
System.out.print(RED + "0 - Red\n");
System.out.print(GREEN + "1 - Green\n");
System.out.print(YELLOW + "2 - Yellow\n");
System.out.print(BLUE + "3 - Blue\n");
System.out.print(PURPLE + "4 - Purple\n");
System.out.print(CYAN + "5 - Cyan\n");
System.out.print(WHITE + "6 - White\n\n");
System.out.print("Enter a number: ");
number = scanner.nextInt();
System.out.print(number + "\n\n");

// Switch-case related to the chosen number.
switch(number) {

    case 0:
        System.out.println(RED + message);
        break;

    case 1:
        System.out.println(GREEN + message);
        break;

    case 2:
        System.out.println(YELLOW + message);
        break;

    case 3:
        System.out.println(BLUE + message);
        break;

    case 4:
        System.out.println(PURPLE + message);
        break;

    case 5:
        System.out.println(CYAN + message);
        break;
    case 6:
        System.out.println(WHITE + message);
        break;

    }
Enter a message: This is a message

0 - Red
1 - Green
2 - Yellow
3 - Blue
4 - Purple
5 - Cyan
6 - White

Enter a number: 4

This is a message

De Morgan's Law

  • Not (a and b) is the same as (not a) or (not b). In Java this is written as !(a && b) == !a || !b
  • Not (a or b) is the same as (not a) and (not b). In Java this is written as !(a || b) == !a && !b
  • Or in other words it uses "nots" that are equivalent to other "not" equations.
int x = 10;
int y = 4;

if(!(x < 3) || !(y > 2) == !(x < 3 && y > 2)) {

    System.out.print("True");

}else {

    System.out.print("False");

}
True