If Statements

If statements are run if the function is true to a particular condition. For a code to execute, the condition must be true.

int x = 20; // the integer given in the variable is 20
if (x > 18) { //since this condition is true, the statement will print
    System.out.println(x+" is greater than 18.");
  }
20 is greater than 18.

If-Else Statement

If-else statements are run if one condition is met. If that condition is not met however, it will perform another specified action under the else part of the conditional.

int x = 8; // the integer given in the variable is 20
if (x > 18) { //since this condition is true, the statement will print
    System.out.println(x+" is greater than 18.");
  } 
else { //this is what will print if the "if" statement is false
    System.out.println(x+ " is less than 18.");
}
8 is less than 18.

If-Elseif-Else

This function runs if the first condition is met, and if that condition is not true, it will check if the next else-if condition is met. This can be done any number of times, and if those conditions are all not met, the else is run.

int x = 8; // the integer given in the variable is 20
if (x > 18) { //since this condition is true, the statement will print
    System.out.println(x+" is greater than 18.");
  } 
else if (x>9) { //this is what will print if the "if" statement is false
    System.out.println(x+ " is greater than 9.");
}
else{
    System.out.println(x+" is smaller than 9."); //what will print if nothing above is true
}
8 is smaller than 9.

Switch Case

A switch case is when the if-elseif-else condition is repeated a numerous amount of times, with more elseif statements that checks different conditions.

int x = 13; // the integer given in the variable is 20
if (x > 18) { //since this condition is true, the statement will print
    System.out.println(x+" is greater than 18.");
  } 
else if (x>16) { //this is what will print if the "if" statement is false
    System.out.println(x+ " is greater than 16.");
}
else if (x>14) {
    System.out.println(x+" is greater than 14"); 
}
else if (x>12) {
    System.out.println(x+" is greater than 12"); 
}
else{
    System.out.println(x+" is smaller than 12."); //what will print if nothing above is true
}
13 is greater than 12

You can write this as a switch case.

int x = 13; // the integer given in the variable is 20
String output;
switch (x) { 
    case 10:
        output = x + " is equal to 10";
        break;
    case 11:
        output = x + " is equal to 11";
        break;
    case 12:
        output = x + " is equal to 12";
        break;
    case 13:
        output = x + " is equal to 13";
        break;
}
System.out.println(output);
13 is equal to 13

De Morgans Law

This law says that "the complement of two union sets is the intersection of their complements". It means that saying "not and " is the same as saying those statements separately.

boolean vanilla = true;
boolean chocolate = true;

if (!(chocolate && vanilla)){
    System.out.println("I do not like chocolate or vanilla");
}
else{
    System.out.println("I like chocolate and vanilla");
}
I like chocolate and vanilla
if (!chocolate || !vanilla){
    System.out.println("I do not like chocolate or vanilla");
}
else{
    System.out.println("I like chocolate and vanilla");
}
I like chocolate and vanilla

Both the statements have the same output.