Conditionals and Booleans

  • Conditionals with regards to programming are pieces of code that tell the computer which path to take in running a program
  • If a certain condition is met, the computer will run a certain section of code
  • if the condition is not met, the computer will run a different section of code
  • A conditional statement is a boolean expression, the segment evaluates to either true of false
  • conditionals can be used within other conditional statements depending on the outcome of code
  • keeping these expressions simple makes code easy to understand

If

  • an "If" statement is the simplest conditional. Usage below.
public class If{
    public static void main(String[] arg){
   
        int num = 20; //create a variable 
        
        if(num>18){ // check if condition is true of false (Boolean)

            System.out.println("num is greater than 18"); // print output if true
        }
    }
}

If.main(null);
num is greater than 18

Else

  • an "Else" statement is a conditional that is evaluated if the previous "if" conditional evaluates to false. Usage below.
public class IfandElse{
    public static void main(String[] arg){
   
        int num = 20; //create a variable 
        
        if(num>30){ // check if condition is true of false (Boolean)

            System.out.println("num is greater than 18"); // print output if true
        }
        else{
            System.out.println("num is not greater than 18"); // print output when "if" evaluates to false
        }
    }
}

IfandElse.main(null);
num is not greater than 18

ElseIf

  • an "ElseIf" conditional is used when more than 2 conditionals are required for a certain task.
  • And "ElseIf" will evaluate if the previous "if" of "elseif" statement evaluates to false.
  • can be used many many time in one segment usage below.
public class ElseIf{
    public static void main(String[] arg){
   
        int num = 4; //create a variable 
        
        if(num == 0){ // check if condition is true of false (Boolean)

            System.out.println("num is 00"); // print output if true
        }
        else if(num==1){
            System.out.println("num is 1"); // print output when previous conditional evaluates to false
        }
        else if(num==2){
            System.out.println("num is 2");// print output when previous conditional evaluates to false
        }
        else if(num==3){
            System.out.println("num is 3"); // print output when previous conditional evaluates to false
        }
        else if(num==4){
            System.out.println("num is 4"); // print output when previous conditional evaluates to false
        }
        else{
            System.out.println("num is none of the above numbers"); // print output when all previous conditionals are false
        }
    }
}
ElseIf.main(null);
num is 4

Switch Case

  • If - Elseif - Else ladder conditionals shown above can be converted to a Case - Switch format usage shown below.
public class SwitchCase{
    public static void main(String[] arg){
   
        int num = 4; //create a variable 
        

        switch (num) {
            case 0: // if num is 0
            System.out.println("num is 0"); // print output if true
                break;
            case 1: // if num is 1
            System.out.println("num is 1"); // print output if true
                break;
            case 2: // if num is 2
            System.out.println("num is 2"); // print output if true
                break;
            case 3: // if num is 3
            System.out.println("num is 3"); // print output if true
                break;    
            case 4: // if num is 0
            System.out.println("num is 4"); // print output if true
                break;    
            default:
            System.out.println("num is none of the options above"); // print output if true
                break;
        }
    }
}
SwitchCase.main(null);
num is 4

De Morgan's Law

  • Complex conditional statements can be hard to evaluate when looking at "!" or "not" operators, and comparison operators, like >, <, >=, <=, ||, &&, and more
  • De morgan's law helps by explaining how operators change when a "!" negation is present

Conversions:

  • < becomes >=
  • becomes <=

  • == becomes !=
  • <= becomes >
  • = becomes <

  • != becomes ==

Usage Example Below:

public class DeMorgans{
    public static void main(String[] arg){
        int x = 2;
        int y = 3;

        // Confusing expression, difficult to look at and evaluate outcome
        if (!(x > 3 && y < 2)){
            System.out.println("1. Condition is met");
        }

        // converting comparison operators and removing ! using DeMorgan's Law
        if (x <= 3 && y >= 2){ 
            System.out.println("2. By removing '!' negation and converting using DemMorgans law, this will also be true.");
        }
    }
}

DeMorgans.main(null);
1. Condition is met
2. By removing '!' negation and converting using DemMorgans law, this will also be true.

Simple Truth Table

  • helpful for determining outcomes when boolean values are changed
  • shows true and false outputs
  • example of simple "or" table below