Home

AAP-2.F For relationships between Boolean values: write and evaluate expressions using logical operators

Learning Objective: Using Logical Operators with Boolean Values

Introduction

In programming, we often need to make decisions based on certain conditions. For example, you might want to check if a user is both logged in and has permission to view a page. To handle such situations, we use Boolean values and logical operators.

What are Boolean Values?

Boolean values are simple: they can only be either true or false. These values are named after George Boole, a mathematician who worked with logic.

Logical Operators

Logical operators are used to combine or modify Boolean values. The main logical operators in Java are:

Examples

AND (&&) Operator

The AND operator checks if both conditions are true.

boolean isRaining = true;
boolean haveUmbrella = false;

boolean goOutside = isRaining && haveUmbrella; // goOutside will be false

In this example, goOutside will be false because haveUmbrella is false, even though isRaining is true.

OR (||) Operator

The OR operator checks if at least one condition is true.

boolean isWeekend = true;
boolean isHoliday = false;

boolean canRelax = isWeekend || isHoliday; // canRelax will be true

In this example, canRelax will be true because isWeekend is true, even though isHoliday is false.

NOT (!) Operator

The NOT operator flips the value of a Boolean.

boolean isTired = true;

boolean isEnergetic = !isTired; // isEnergetic will be false

In this example, isEnergetic will be false because isTired is true.

Real-World Example

Imagine you are building a simple security system. You want to check if a person can enter a room. The person must know the correct password and must have an access card.

boolean knowsPassword = true;
boolean hasAccessCard = false;

boolean canEnterRoom = knowsPassword && hasAccessCard; // canEnterRoom will be false

Full Program Example

Let's write a Java program that uses logical operators to determine if someone can access a website based on age and membership status.

public class WebsiteAccess {
    public static void main(String[] args) {
        int age = 16;
        boolean isMember = true;
        
        // Check if the user can access the website
        boolean canAccess = (age >= 18) && isMember;

        // Print the result
        if (canAccess) {
            System.out.println("Access granted.");
        } else {
            System.out.println("Access denied.");
        }
    }
}

Explanation:

Programming Problem

Write a program that determines if a student can participate in a school trip. To participate, the student must have both a signed permission slip and have paid the trip fee.

  1. Define two boolean variables: hasPermissionSlip and hasPaidFee.

  2. Use the AND operator to determine if the student can go on the trip.

  3. Print "Trip confirmed." if both conditions are true; otherwise, print "Cannot go on the trip."

Example:

public class SchoolTrip {
    public static void main(String[] args) {
        boolean hasPermissionSlip = true;
        boolean hasPaidFee = false;

        boolean canGoOnTrip = hasPermissionSlip && hasPaidFee;

        if (canGoOnTrip) {
            System.out.println("Trip confirmed.");
        } else {
            System.out.println("Cannot go on the trip.");
        }
    }
}

In this example, the output will be "Cannot go on the trip." because hasPaidFee is false.

Conclusion

Logical operators are essential for making decisions in programming. By understanding how to use &&, ||, and !, you can create complex conditions and control the flow of your programs effectively. Practice writing and evaluating expressions using these operators to strengthen your programming skills!