Boolean expressions

Boolean expressions are something that can be TRUE or FALSE

For example, we can check if two things are equal, greater than eachother or less than eachother. Here I have some examples of how these expresisons could be used in a quiz context. Below I have some equality examples

char correctAnswer = 'A';
char user1Answer = 'B';
char user2Answer = 'A';

//Check if user 1's answer was equal to the correct answeer
System.out.println("Is user 1 right?");
System.out.println(user1Answer == correctAnswer);

//Check if user 2's answer was equal to the correct answer
System.out.println("Is user 2 right?");
System.out.println(user2Answer == correctAnswer);
Is user 1 right?
false
Is user 2 right?
true

We can also compare some values and see if they are greater or less than.

int user1Score = 100;
int user2Score = 50;
int targetScore = 75;

//Check if user 1 reached target score
System.out.println("Did user 1 reach the target score?");
System.out.println(user1Score >= targetScore);

//Check if user 2 reached target score
System.out.println("Did user 2 reach the target score?");
System.out.println(user2Score >= targetScore);
Did user 1 reach the target score?
true
Did user 2 reach the target
false

An IF statement utilizes boolean expressions and DOES SOMETHING if the expression is true. For example we can use this on the previos two examples

char correctAnswer = 'A';
char user1Answer = 'B';
char user2Answer = 'A';

//Check if user 1's answer was equal to the correct answeer
if (user1Answer == correctAnswer) {
  System.out.println("User 1 is right");
}

//Check if user 2's answer was equal to the correct answer
if (user2Answer == correctAnswer) {
  System.out.println("User 2 is right");
}
User 2 is right
int user1Score = 100;
int user2Score = 50;
int targetScore = 75;

//Check if user 1 reached target score
if (user1Score >= targetScore) {
  System.out.println("User 1 reached the target score");
}

//Check if user 2 reached target score
if (user2Score >= targetScore) {
  System.out.println("User 2 reached the target score");
}
User 1 reached the target score

Now, going further what if we wanted to complete some action if the user DID NOT reach the target score. In this case we can use an ELSE statement.

int user1Score = 100;
int user2Score = 50;
int targetScore = 75;

//Check if user 1 reached target score
if (user1Score >= targetScore) {
  System.out.println("User 1 reached the target score");
} else {
  System.out.println("User 1 failed");
}

//Check if user 2 reached target score
if (user2Score >= targetScore) {
  System.out.println("User 2 reached the target score");
} else {
  System.out.println("User 2 failed");
}
User 1 reached the target score
User 2 failed

Now, let's say that we want to give the points to answers, but there are different points for each answer choice. This would cause us to chain if logic, which is where we can use an ELSE IF statement.

char user1AnswerChoice = 'C';
char user2AnswerChoice = 'A';

char reallyCorrectAnswer = 'D';
char correctAnswer = 'B';
char partiallyCorrectAnswer = 'C';
char reallyWrongAnswer = 'A';


int user1Points = 0;
int user2Points = 0;

// process user 1's answer
if (user1AnswerChoice == reallyCorrectAnswer) {
  // really correct answer is 30 points
  user1Points += 30;
} else if (user1AnswerChoice == correctAnswer) {
  // correct answer is 20 points
  user1Points += 20;
} else if (user1AnswerChoice == partiallyCorrectAnswer) {
  // partially correct answer is 10 points
  user1Points += 10;
} else if (user1AnswerChoice == reallyWrongAnswer) {
  // a really wrong answer loses 10 points
  user1Points -= 10;
} else {
  // 1 point for trying and not choosing the really wrong answer
  user1Points += 1;
}

//process user 2's answer
if (user2AnswerChoice == reallyCorrectAnswer) {
  // really correct answer is 30 points
  user2Points += 30;
} else if (user2AnswerChoice == correctAnswer) {
  // correct answer is 20 points
  user2Points += 20;
} else if (user2AnswerChoice == partiallyCorrectAnswer) {
  // partially correct answer is 10 points
  user2Points += 10;
} else if (user2AnswerChoice == reallyWrongAnswer) {
  // a really wrong answer loses 10 points
  user2Points -= 10;
} else {
  // 1 point for trying and not choosing the really wrong answer
  user2Points += 1;
}

System.out.println("User 1 has " + user1Points + " points.");
System.out.println("User 2 has " + user2Points + " points.");
User 1 has 10 points.
User 2 has -10 points.

In this example, we see a lot of chaining with the if statements. This could be greatly simplified if we used a switch statement, which allows us to select code based on the value of a variable. See the example below the previous code transformed to switch cases.

Note that as shown below, the switch statement cases cannot have variables in them as they must be constant expressions.

char user1AnswerChoice = 'C';
char user2AnswerChoice = 'A';


int user1Points = 0;
int user2Points = 0;

// process user 1's answer
switch (user1AnswerChoice) {
  case 'D':
    user1Points += 30;
    break;
  case 'B':
    user1Points += 20;
    break;
  case 'C':
    user1Points += 10;
    break;
  case 'A':
    user1Points -= 10;
    break;
  default:
    user1Points += 1;
    break;
}

//process user 2's answer
switch (user2AnswerChoice) {
  case 'D':
    user2Points += 30;
    break;
  case 'B':
    user2Points += 20;
    break;
  case 'C':
    user2Points += 10;
    break;
  case 'A':
    user2Points -= 10;
    break;
  default:
    user2Points += 1;
    break;
}

System.out.println("User 1 has " + user1Points + " points.");
System.out.println("User 2 has " + user2Points + " points.");
User 1 has 10 points.
User 2 has -10 points.

Another thing to understand related to this is how to combine conditionals together using AND (&&) and OR (||). They mean what they say, AND returns true if both conditions is true while OR returns true if both or one of the conditions is true. See some examples below. The NOT (!) operator is also useful to flip a boolean value.

char correctAnswer = 'E';
char user1Answer = 'D';
char user2Answer = 'E';

// Using AND operator
if (user1Answer == correctAnswer && user2Answer == correctAnswer) {
  System.out.println("Both users got it right");
}

// Using OR operator
if (user1Answer == correctAnswer || user2Answer == correctAnswer) {
  System.out.println("At least one user got it right");
}

// Using AND + NOT operator
if (!(user1Answer == correctAnswer && user2Answer == correctAnswer)) {
  System.out.println("Both users DID NOT get it right");
}

// Using OR operator
if (!(user1Answer == correctAnswer || user2Answer == correctAnswer)) {
  System.out.println("NEITHER user1 nor user2 got it right");
}
At least one user got it right
Both users DID NOT get it right

The final relevant thing to understand for if statements & boolean expressions is De Morgan's Law. This is an important property that can allow you to switch your if statements with AND and OR. It states that NOT (A AND B) = (NOT A) OR (NOT B) and NOT (A OR B) = (NOT A) AND (NOT B). To illustrate this, I will simplify the last two if statements from the previous example using De Morgan's Law.

// Using first de morgan's law (Note the use of the != operator meaning NOT EQUAL TO)
if (user1Answer != correctAnswer || user2Answer != correctAnswer) {
  System.out.println("Both users DID NOT get it right");
}

// Using second de morgan's law
if (user1Answer != correctAnswer && user2Answer != correctAnswer) {
  System.out.println("NEITHER user1 nor user2 got it right");
}
Both users DID NOT get it right

Now we reach the end of this lesson. Here are some "hacks" I recommend you can do to further learn these concepts:

  • Create a quiz or other application that heavily utilizes if statements
  • Code a custom application in the spring app using if statements
  • Try to create a menu from scratch (without looking at Mr. M's code) as it is a great place to utilize switch cases