# Compound Boolean Expressions¶

Just before we looked at the If … Then statement we looked at Boolean expressions. Boolean expressions have two and only two potential answers, either they are true or false. So far we have looked at just simple Boolean expression, with just one comparison. A Boolean expression can actually have more than one comparison and be quite complex. A compound Boolean expression is generated by combining more than one simple Boolean expression together with a logical operator NOT, AND & OR. NOT is used to form an expression that evaluates to True only when the operand is false. AND is used to form an expression that evaluates to True only when both operands are true. OR is used to form an expression that evaluates to true when either operand is true.

Here is a truth table for each operator:

NOT Truth Table

A

NOT A

True

False

False

True

AND Truth Table

A

B

A AND B

True

True

True

True

False

False

False

True

False

False

False

False

OR Truth Table

A

B

A OR B

True

True

True

True

False

True

False

True

True

False

False

False

The general form in a programming language for a compound Boolean expression is:

## NOT¶

IF not(Boolean expression #1) THEN
Statements to be performed …
ELSE
Statements to be performed …
ENDIF

## AND¶

IF ((Boolean expression #1) and (Boolean expression #2)) THEN
Statements to be performed
ELSE
Statements to be performed …
ENDIF

## OR¶

IF ((Boolean expression #1) or (Boolean expression #2)) THEN
Statements to be performed
ELSE
Statements to be performed …
ENDIF

In some programming languages the operators are simply the words not, and & or. In others they are “!” for NOT, “&&” for AND & “||” for OR.

In this example program, the user is asked to enter a term mark and a final project mark. The program then tells the user if they passed the course or not. The rule for passing the course is that the student must have a term mark of at least 50% and a final project mark of at least 50. The program uses a compound Boolean expression to determine if the student passed the course.

## Pseudocode for Compound Boolean Expression statement¶

GET term_mark
GET project_mark
IF ((term_mark >= 50) and (project_mark >= 50)) THEN
SHOW “You passed the course.”
ELSE
SHOW “You failed the course.”
ENDIF

## Code for Compound Boolean Expression statement¶

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30``` ```// Copyright (c) 2020 Mr. Coxall All rights reserved. // // Created by: Mr. Coxall // Created on: Sep 2020 // This program uses a compound boolean statement #include int main() { // this function uses a compound boolean statement int termMark; int projectMark; // input printf("Enter term mark (as %%): "); scanf("%d", &termMark); printf("Enter project mark (as %%): "); scanf("%d", &projectMark); printf("\n"); // process & output if (termMark >= 50 && projectMark >= 50) { printf("You passed the course.\n"); } else { printf("You did not pass the course.\n"); } printf("\nDone.\n"); return 0; } ```
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30``` ```// Copyright (c) 2020 St. Mother Teresa HS All rights reserved. // // Created by: Mr. Coxall // Created on: Sep 2020 // This program uses a compound boolean statement #include int main() { // this function uses a compound boolean statement int termMark; int projectMark; // input std::cout << "Enter term mark (as %): "; std::cin >> termMark; std::cout << "Enter project mark (as %): "; std::cin >> projectMark; std::cout << std::endl; // process & output if (termMark >= 50 && projectMark >= 50) { std::cout << "You passed the course." << std::endl; } else { std::cout << "You did not pass the course." << std::endl; } std::cout << "\nDone." << std::endl; return 0; } ```
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31``` ```/* Created by: Mr. Coxall * Created on: Sep 2020 * This program uses a compound boolean statement */ using System; /* * The Program class */ class Program { static void Main() { // this function uses a compound boolean statement // input Console.Write("Enter term mark (as %): "); int termMark = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter project mark (as %): "); int projectMark = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(); // process & output if (termMark >= 50 && projectMark >= 50) { Console.WriteLine("You passed the course."); } else { Console.WriteLine("You did not pass the course."); } Console.WriteLine("\nDone."); } } ```
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34``` ```/** * Created by: Mr. Coxall * Created on: Sep 2020 * This program uses a compound boolean statement */ package main import ( "fmt" ) func main() { // this function uses a compound boolean statement // input var termMark int var projectMark int fmt.Print("Enter term mark (as %): ") fmt.Scanln(&termMark) fmt.Print("Enter project mark (as %): ") fmt.Scanln(&projectMark) fmt.Println() // process & output if termMark >= 50 && projectMark >= 50 { fmt.Println("You passed the course.") } else { fmt.Println("You did not pass the course.") } fmt.Println("\nDone.") } ```
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36``` ```/* * This program uses a compound boolean statement * * @author Mr Coxall * @version 1.0 * @since 2020-09-01 */ import java.util.Scanner; public class Main { public static void main(String[] args) { // this function uses a compound boolean statement // create Scanner object for user input Scanner scanner = new Scanner(System.in); // input System.out.print("Enter term mark (as %): "); int termMark = scanner.nextInt(); System.out.print("Enter project mark (as %): "); int projectMark = scanner.nextInt(); System.out.println(); // process & output if (termMark >= 50 && projectMark >= 50) { System.out.println("You passed the course."); } else { System.out.println("You did not pass the course."); } // close the Scanner object scanner.close(); System.out.println("\nDone."); } } ```
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20``` ```/* Created by: Mr. Coxall * Created on: Sep 2020 * This program uses a compound boolean statement */ const prompt = require("prompt-sync")() // input const termMark = parseInt(prompt("Enter term mark (as %): ")) const projectMark = parseInt(prompt("Enter project mark (as %): ")) console.log(""); // process & output if (termMark >= 50 && projectMark >= 50) { console.log("You passed the course.") } else { console.log("You did not pass the course.") } console.log("\nDone.") ```
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27``` ```#!/usr/bin/env python3 """ Created by: Mr. Coxall Created on: Sep 2020 This module uses a compound boolean statement """ def main() -> None: """The main() function uses a compound boolean statement, returns None.""" # input term_mark = int(input("Enter term mark (as %): ")) project_mark = int(input("Enter project mark (as %): ")) print("") # process & output if term_mark >= 50 and project_mark >= 50: print("You passed the course.") else: print("You did not pass the course.") print("\nDone.") if __name__ == "__main__": main() ```