If Statement

// will always print out true as the output since 20 will always be greater than 18
if (20 > 18) { // condition in the parenthesis
    // the code that is executed if the condition is proved true (the return)
  System.out.println("True, 20 is greater than 18");
}
// if statements can also be used to test and/or compare variables
int a = 20;
int b = 18;
if (a > b) {
  System.out.println("True, variable a is greater than variable b");
}
True, 20 is greater than 18
True, variable a is greater than variable b

Else Statement

// will always print out "Good Bye" since the time is 20 and 20 is always greater than 18, which means the condition is alwaysfalse
int time = 20;
if (time < 18) { // the condition is in the parenthesis
  System.out.println("Good day."); // the return if the code executed is true under the condition
} else {
  System.out.println("Good bye."); // the return if the code executed is false under the condition mentioned
}
Good bye.

Else If Statement

// will always print out you won since 17 is greater than 10 and 15
int score = 17;
if (score < 10) { // in the parenthesis is the first condition
  System.out.println("You lost the game."); // what return follows if the executed code is true
} else if (score < 15) { // in the parenthesis is the second condition
  System.out.println("You might win the game."); // what return follows if the executed code is false for the first condition but true for the second condition
} else {
  System.out.println("You won the game."); // what return follows if the executed code is false for both the first and second condition
}
You won the game.

If-ElseIf Statements

The if statement is the first condition in the Java program that starts the management of the return. If this first condition in the if statement is true, the program will print out the statement coded inside it. If the condition is false, the execution moves on to the else condition and prints out the statement written inside this condition if it is true. If not, then the program repeats the process of moving on and checking the next condition until the code is executed.

int x;
 
        x=105;
 
        if (x <= 10) { // condition 1 in the parenthesis
 
           System.out.println("X is less than or equal to 10, it is a pretty small number!"); // if the value of x is less than or equal to 10, the system will return this statement
 
        }
 
        else if (x <= 20) { // condition 2 in the parenthesis
 
           System.out.println("X is less than or equal to 20!"); // if the value for x fails to meet all of the above conditions, but is less than or equal to 20, the system will print out this statement
 
        }
 
        else if (x <= 30) { // condition 3 in the parenthesis
 
            System.out.println("X is less than or equal to 30!"); // if the value for x fails to meet all of the above conditions, but is less than or equal to 30, the system will print out this statement
 
         }       
 
        else if (x <= 40) { // condition 4 in the parenthesis
 
            System.out.println("X is less than or equal to 40!"); // if the value for x fails to meet all of the above conditions, but is less than or equal to 40, the system will print out this statement
 
         }        
 
        else if (x <= 50) { // condition 5 in the parenthesis
 
            System.out.println("X is less than or equal to 50!"); // if the value for x fails to meet all of the above conditions, but is less than or equal to 50, the system will print out this statement
 
         }   
         
        else if (x <= 60) { // condition 6 in the parenthesis
            System.out.println("X is less than or equal to 60!"); // if the value for x fails to meet all of the above conditions, but is less than or equal to 60, the system will print out this statement
        }
 
        else { // if the value for x fails to meet all of the above conditions, it returns the statement below
 
                System.out.println("X is greater than 60, it's a pretty big number!");
 
        }
X is greater than 60, it's a pretty big number!

Switch-Case Statements

In switch-case statements, it is possible to write as many cases as you want, as long as they are all relevant to the problem. Break statements and default statements are both optional; but if you include the default statement in some place besides the end of the program, you must make sure that the break statement follows right after the default statement. Variables aren't allowed in these cases so the value for a case must be literal or constant.

int day = 5;
        String dayString;
  
        // This is the switch statement that includes what the data type is, in this case it is int. The value written above must be of the same data type to prevent any errors in the program
        switch (day) {
  
        // Case
        case 1: // this is case value 1
            dayString = "It's Monday today."; // this is the statement that prints out if the day value matches case #1 value
            break; // this is the break statement (THIS IS OPTIONAL, but I included it)
  
        // Case
        case 2: // this is case value 2
            dayString = "It's Tuesday today."; // this is the statement that prints out if the day value matches case #2 value
            break;
  
            // Case
        case 3: // this is case value 3
            dayString = "It's Wednesday today."; // this is the statement that prints out if the day value matches case #3 value
            break;
  
            // Case
        case 4: // this is case value 4
            dayString = "It's Thursday today."; // this is the statement that prints out if the day value matches case #4 value
            break;
  
        // Case
        case 5: // this is case value 5
            dayString = "It's Friday today."; // this is the statement that prints out if the day value matches case #5 value
            break;
  
            // Case
        case 6: // this is case value 6
            dayString = "It's Saturday today."; // this is the statement that prints out if the day value matches case #6 value
            break;
  
            // Case
        case 7: // this is case value 7
            dayString = "It's Sunday today."; // this is the statement that prints out if the day value matches case #7 value
            break;

// we can continue to add as many case statements as we want (I stopped since there are only 7 days of the week) but make sure the number of cases is relevant to your program

        // This is the default case for when the case statements don't apply
        default:
            dayString = "That's not a day bro."; // this is the statement that prints out if the day value doesn't match any of the case values
        }
        System.out.println(dayString);
It's Friday today.

De Morgan's Law

This law came after the man named Augustus De Morgan who developed it in the 1800s. The law states how to determine what the code will execute when negation rules are applied. For example, if a and b are both boolean values, then:

  • !(a && b) is equivalent to !a || !b
  • !(a || b) is equivalent to !a && !b

The following negation conditions will also apply under De Morgan's law:

  • < becomes >=
  • ">" becomes <=
  • == becomes !=
  • <= becomes >
  • = becomes <
  • != becomes ==
String state = "HI";

// !(a || b) is equivalent to !a && !b
if (!(state.equals("TX") || state.equals("HI"))) {
    System.out.println("The state is not Texas or Hawaii");
}
else {
    System.out.println("The state is either Texas or Hawaii");
}

// !(a && b) is equivalent to !a || !b
if (!(state.equals("TX") && state.equals("HI"))) {
    System.out.println("The state is either Texas or Hawaii");
}
else {
    System.out.println("The state is not Texas or Hawaii");
}
The state is either Texas or Hawaii
The state is either Texas or Hawaii