1 of 25

AP Computer Science A:

Compound Boolean Expressions

2 of 25

Logical Operators

We can incorporate additional boolean expressions to our if statements with logical operators.

Logical operators allow programs to make decisions based on multiple conditions

3 of 25

Logical Operators

NOT !

AND &&

OR ||

4 of 25

The !(not) Operator

x

!x

true

false

false

true

! evaluates a condition to the opposite boolean value

5 of 25

The && (and) Operator

x

y

x && y

true

true

true

true

false

false

false

true

false

false

false

false

&& evaluates whether all conditions are true

6 of 25

The || (or) Operator

x

y

x || y

true

true

true

true

false

true

false

true

true

false

false

false

|| evaluates whether one condition is true

7 of 25

Operator Shortcuts!

true

false

||

is

true

If any boolean expression evaluates to true in an || expression, the final expression will be true!

true

false

&&

is

false

If any boolean expression evaluates to false in an && expression, the final expression will be false!

8 of 25

Operator Shortcuts!

true

anything

||

is

true

If any boolean expression evaluates to true in an || expression, the final expression will be true!

anything

false

&&

is

false

If any boolean expression evaluates to false in an && expression, the final expression will be false!

9 of 25

Operator Shortcuts!

anything

true

anything

||

is

true

Because these expressions will always evaluate to false and true regardless of what the other expression evaluates to, the boolean expression doesn’t evaluate the second expression.

false

&&

is

false

10 of 25

Operator Shortcuts!

true

anything

||

is

true

Evaluating only the first boolean expression is called short circuit evaluation.

anything

false

&&

is

false

11 of 25

Short Circuit Evaluation

  • first && second
    • if first is false, then the whole expression must be false. Don’t bother evaluating second.

  • first || second
    • if first is true, then the whole expression must be true. Don’t bother evaluating second.

12 of 25

Short Circuit Example

int numSlices = 10;

int numPeople = 0;

if(numSlices / numPeople > 0)

{

System.out.println(“There’s enough pizza!”);

}

else

{� System.out.println(“Not enough pizza.”);

}

13 of 25

Short Circuit Example

int numSlices = 10;

int numPeople = 0;

if(numPeople != 0 && numSlices / numPeople > 0)

{

System.out.println(“There’s enough pizza!”);

}

else

{� System.out.println(“Not enough pizza.”);

}

false

not evaluated

14 of 25

Example

int numSlices = 10;

int numPeople = 5;

if(numPeople != 0 && numSlices / numPeople > 0)

{

System.out.println(“There’s enough pizza!”);

}

else

{� System.out.println(“Not enough pizza.”);

}

15 of 25

Short Circuit Example

int numSlices = 10;

int numPeople = 5;

if(numPeople != 0 && numSlices / numPeople > 0)

{

System.out.println(“There’s enough pizza!”);

}

else

{� System.out.println(“Not enough pizza.”);

}

true

evaluates!

16 of 25

int total=2;

boolean flipper = true;

if(flipper || total>4)

{

out.println("short");

}

out.println("check");

OUTPUT

short�check

Short Circuit Evaluation

17 of 25

int total=9, num=13;�

if (total>4 ||++num>15 && total>0)

{

out.println("short");

}

out.println(num);

OUTPUT

short�13

The && never happens!

Short Circuit Evaluation

18 of 25

Order of Operations

Boolean Operators also have an order of operations!

19 of 25

Order of Operations

Boolean operators also have an order of operations:

! is evaluated before &&

&& is evaluated before ||

NOT !

AND &&

OR ||��NAO = “NOW”

20 of 25

Nesting if Statements

Nesting if statements means we can place if statements within other if statements!

21 of 25

num = 67;�if(num>50)

if(num>60)

if(num>70)

System.out.println(">70");

else

System.out.println(">60 and <70");

else

System.out.println(">50 and <60");

else

System.out.println("<50");

OUTPUT

>60 and <70

Nested Ifs

22 of 25

int p = 74;

if(p > 30)

{

System.out.println("aplus");

if(p > 40)

System.out.println("comp");

}

else

System.out.println("sci");

OUTPUT

aplus�comp

Nesting ifs

23 of 25

int num=1;�if(num>2)

{

if(num<10)

System.out.println(">2<10");

}

else{

System.out.println("<2");

}

OUTPUT

<2

Nesting ifs

24 of 25

int num=11;�if(num>2)

if(num<10)

System.out.println(">2<10");

else

System.out.println("<2");

OUTPUT

<2

Which if-statement does the else belong to?

“Dangling” Else

25 of 25

Logical Operators

Can be used to connect boolean expressions to make more complex expression.

NOT !

AND &&

OR ||

Short Circuit Evaluation

When the result of a logical expression using && or || can be determined by evaluating only the first Boolean operand, the second is not evaluated.

Nested if Statements

The process of placing if statements within if statements.

Concepts Learned this Lesson

Term

Definition