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

int myAge = 15;
int otherAge = 45;

myAge < 16; //true
myAge <= 15; //true
myAge == 15; //true
myAge > otherAge //false
myAge >= otherAge //false
myAge != otherAge //true

popcorn hack

whats wrong with this code? (below)


String myName = Alisha;

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

cannot find symbol

  symbol:   variable Alisha

The problem with the code is that the string is missing its quotation marks With the problem corrected it would become true on both lines.

String myName = "Alisha";
String otherName = "Anika";
myName != "Anika"; //true
myName == "Alisha"; //true

myName == otherName; //false

System.out.println(myName == "Alisha"); //true


true

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");
}

True

homework question

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

The precondition for num is for num to be more than 0 and less than or equal to six so

num >= 0 and num <= 6