Notes on Conditionals

  • De Morgan's law - opposite of everything in the statement
  • Example:
    if (!((false || !true) || (false && true))) {
  • When there are too many conditions in the if statement itself, then it becomes poor code
  • &&, ||, ! - And, or, and Not symbols respectively

Hacks

  • if is the simple statement with a conditional within it, if this condition is met, the code inside is run
  • if-else a second conditional statement on top of the already existing if
  • if-else if-else are multiple conditional statements on top of the existing if statement
import java.util.*;
class Main{

    public static void main(String args[]){
        boolean position = true;
        boolean velocity = true;
        boolean acceleration = true;
        Scanner input = new Scanner(System.in);
        for(int i = 0; i < 2; i++){
            System.out.print("velocity or no: ");
            String words = input.nextLine();
            System.out.println(words);
 
            if(words.equals("yes")){
                velocity = false;
                position = true ;
            }else {
                acceleration = true;
                position = true;
                velocity = true;
            }

            if (position && velocity && acceleration) {
                System.out.println("it gets there");
            }
            else if (!position || !velocity) {
                System.out.println("it doesn't really get there");
            }
            else if (velocity && !acceleration) {
                System.out.println("it stand still");
            }
            else if(!position){
                System.out.println("is breaking");
            }
        }
    }
}
Main.main(null);
velocity or no: 
it gets there
velocity or no: 
it gets there

Using Switches

import java.util.*;
class Main{

    public static void main(String args[]){
        boolean position = true;
        boolean velocity = true;
        boolean acceleration = true;
        for(int i = 0; i < 2; i++){

            

            Scanner input = new Scanner(System.in);
            System.out.print("velocity or no: ");
            String words = input.nextLine();
            System.out.println(words);

            switch(words){
                case ("yes"):
                    velocity = false;
                    position = true ;
                    break;

                case ("no"):
                    acceleration = true;
                    position = true;
                    velocity = true;
                    break;
                default:
                    System.out.print("Try a different input");
                    break;

            }

            
            if (position && velocity && acceleration) {
                System.out.println("it gets there");
            }
            else if (!position || !velocity) {
                System.out.println("it doesn't really get there");
            }
            else if (velocity && !acceleration) {
                System.out.println("it stand still");
            }
            else if(!position){
                System.out.println("is breaking");
            }
        }

    }
}

Main.main(null);
velocity or no: Ok
Try a different inputit gets there
velocity or no: ok
Try a different inputit gets there

DeMorgan's Law

  • Basically the usage of negation in front of a conditional
  • Not (A and B) is the same as Not A or Not B.
  • Not (A or B) is the same as Not A and Not B.
  • *Use ! for not
int x = 1;
int y = 2;
if(!(x < 2 && y > 1)){
   System.out.println("Hello");
}   
else if(!(x < 2 || y > 1)){
   System.out.println("Good Day");
}