Boolean Expressions & If Statements

  • Boolean Expressions: represent logic of true or false
  • Truth Tables: A truth table has one column for each variable, one row for each possible combination of variable values, and a column that specifies the value of the function for that combination
  • breakdown of possible values that can be obtained using logic

De Morgans

  • 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"); //de morgans of above statement
}
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");
} //same output
I like chocolate and vanilla

Comparing

  • Comparing operators are: ==, !=, <, >, <=, >=
  • Can be used to compare strings, numbers, and objects
  • .equals operator can also be used
public class Compare { //program to compare strings
    public static void main(String args[])
    {
        String string1 = new String("Riya and Ridhi");
        String string2 = new String("riya and ridhi");
        String string3 = new String("Riya");
        String string4 = new String("Riya");
        String string5 = new String("riya");
  
        // Comparing for String 1 != String 2
        System.out.println("Comparing " + string1 + " and " + string2
                           + " : " + string1.equals(string2)); //use of .equals
  
        // Comparing for String 3 = String 4
        System.out.println("Comparing " + string3 + " and " + string4
                           + " : " + string3.equals(string4));
  
        // Comparing for String 4 != String 5
        System.out.println("Comparing " + string4 + " and " + string5
                           + " : " + string4.equals(string5));
  
        // Comparing for String 1 != String 4
        System.out.println("Comparing " + string1 + " and " + string4
                           + " : " + string1.equals(string4));
    }
}
Compare.main(null);
Comparing Riya and Ridhi and riya and ridhi : false
Comparing Riya and Riya : true
Comparing Riya and riya : false
Comparing Riya and Ridhi and Riya : false