Skip to the content.
3.1: Boolean Expressions 3.2: If Control Flow 3.3: If Else 3.4: Else If 3.5: Compound Booleans 3.6: Equivalent Booleans 3.7: Comparing Objects 3.8: Homework

Unit 3 Boolean Expressions - 3.1

Unit 3 Team Teach

AP CSA

3.1 Boolean Expressions

Java’s relational operators

  • equal to: ==
  • not equal to: !=
  • less than: <
  • greater than: >
  • less than or equal to: <=
  • greater than or equal to >=

Hack!

int myAge = 15;
int otherAge = 45; 

using these integers, determine weather the following statements are True or False

Screenshot 2024-09-15 at 10 00 54 PM

Strings

popcorn hack

This is a string type. You cannot use the same operators as integer comparisons for comparison. Also, the syntax for the strings are wrong (quotation marks missing).


String myName = Alisha;

myName != Anika;
myName == Alisha ;
|   String myName = Alisha;

cannot find symbol

  symbol:   variable Alisha

comparison of string objects should be done using String methods, NOT integer methods.

  • .equal
  • compare to
String myName = "Alisha";
boolean areNamesEqual = myName.equals("Alisha");  

if (areNamesEqual) {
    System.out.println("True");
} else {
    System.out.println("False");
}

homework question

Screenshot 2024-09-16 at 8 05 24 AM what is the precondition for num?

public class CheckDigit {
    public static int getCheck (int num) {
    
        if (num>=1 && num<=6) {
            System.out.println("You can vote this year.");
        }
    }

}