Boolean operators

Boolean Operators

A Boolean operator combines or modifies Boolean values (True / False) to produce a new Boolean result. AQA requires three operators: NOT, AND, and OR. You must be able to use them individually and in combination inside selection (IF) and iteration (WHILE) conditions.

Operator Meaning AQA pseudocode Python VB.NET C#
NOT Inverts the value NOT not Not !
AND True only if both sides are True AND and And &&
OR True if at least one side is True OR or Or ||

Operators in Conditions

The tabs below show each operator used in a realistic program. Notice how combining operators with relational expressions lets a single condition capture complex real-world rules.

AND requires every condition to be True. Use it when a result is only valid if all criteria are met simultaneously - e.g. a user must be logged in and have the correct role to access a page.

AQA pseudocode:

IF age >= 18 AND hasTicket = TRUE THEN
    OUTPUT "Entry permitted"
ELSE
    OUTPUT "Entry refused"
ENDIF
# Python
age = int(input("Age: "))
has_ticket = input("Ticket? (yes/no): ") == "yes"
if age >= 18 and has_ticket:          # AND -- both must be True
    print("Entry permitted")
else:
    print("Entry refused")

' VB.NET
' Dim age As Integer = CInt(Console.ReadLine())
' Dim hasTicket As Boolean = (Console.ReadLine() = "yes")
' If age >= 18 And hasTicket Then     ' And -- both must be True
'     Console.WriteLine("Entry permitted")
' Else
'     Console.WriteLine("Entry refused")
' End If

// C#
// int age = int.Parse(Console.ReadLine());
// bool hasTicket = Console.ReadLine() == "yes";
// if (age >= 18 && hasTicket)         // && -- both must be True
//     Console.WriteLine("Entry permitted");
// else
//     Console.WriteLine("Entry refused");

OR requires at least one condition to be True. Use it when any of several alternatives should trigger the same outcome - e.g. a discount applies if the customer is a student or a senior citizen.

AQA pseudocode:

IF isStudent = TRUE OR isSenior = TRUE THEN
    OUTPUT "Discount applied"
ELSE
    OUTPUT "Full price"
ENDIF
# Python
is_student = input("Student? (yes/no): ") == "yes"
is_senior  = input("Senior?  (yes/no): ") == "yes"
if is_student or is_senior:           # OR -- at least one must be True
    print("Discount applied")
else:
    print("Full price")

' VB.NET
' Dim isStudent As Boolean = (Console.ReadLine() = "yes")
' Dim isSenior  As Boolean = (Console.ReadLine() = "yes")
' If isStudent Or isSenior Then       ' Or -- at least one must be True
'     Console.WriteLine("Discount applied")
' Else
'     Console.WriteLine("Full price")
' End If

// C#
// bool isStudent = Console.ReadLine() == "yes";
// bool isSenior  = Console.ReadLine() == "yes";
// if (isStudent || isSenior)          // || -- at least one must be True
//     Console.WriteLine("Discount applied");
// else
//     Console.WriteLine("Full price");

NOT inverts a Boolean value. It is often used in WHILE conditions and can be combined with AND or OR to form complex conditions. Here a login loop runs while the password is wrong and there are attempts remaining:

AQA pseudocode:

WHILE NOT correct AND attempts < 3 DO
    INPUT password
    IF password = "secret" THEN
        correct ← TRUE
    ENDIF
    attempts ← attempts + 1
ENDWHILE
# Python
correct  = False
attempts = 0
while not correct and attempts < 3:   # NOT + AND combined
    password = input("Password: ")
    if password == "secret":
        correct = True
    attempts += 1
print("Access granted" if correct else "Locked out")

' VB.NET
' Dim correct  As Boolean = False
' Dim attempts As Integer = 0
' Do While Not correct And attempts < 3   ' Not + And combined
'     Dim password As String = Console.ReadLine()
'     If password = "secret" Then correct = True
'     attempts += 1
' Loop
' Console.WriteLine(If(correct, "Access granted", "Locked out"))

// C#
// bool correct  = false;
// int  attempts = 0;
// while (!correct && attempts < 3)    // ! + && combined
// {
//     string password = Console.ReadLine();
//     if (password == "secret") correct = true;
//     attempts++;
// }
// Console.WriteLine(correct ? "Access granted" : "Locked out");

 Key Takeaways

  • AND returns True only when both operands are True. One False side makes the whole expression False.
  • OR returns True when at least one operand is True. Both sides must be False for OR to return False.
  • NOT inverts a single Boolean value: NOT True gives False, and NOT False gives True.
  • Operators differ across languages: Python uses and / or / not (keywords); C# uses && / || / ! (symbols); VB.NET uses And / Or / Not.
  • Boolean operators can be combined with relational operators to build complex conditions inside IF statements and WHILE loops.