AP Computer Science A:
Compound Boolean Expressions
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
Logical Operators
NOT !
AND &&
OR ||
The !(not) Operator
x | !x |
true | false |
false | true |
! evaluates a condition to the opposite boolean value
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
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
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!
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!
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
Operator Shortcuts!
true
anything
||
is
true
Evaluating only the first boolean expression is called short circuit evaluation.
anything
false
&&
is
false
Short Circuit Evaluation
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.”);
}
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
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.”);
}
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!
int total=2;
boolean flipper = true;
if(flipper || total>4)
{
out.println("short");
}
out.println("check");
OUTPUT
short�check
Short Circuit Evaluation
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
Order of Operations
Boolean Operators also have an order of operations!
Order of Operations
Boolean operators also have an order of operations:
! is evaluated before &&
&& is evaluated before ||
NOT !
AND &&
OR ||��NAO = “NOW”
Nesting if Statements
Nesting if statements means we can place if statements within other if statements!
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
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
int num=1;�if(num>2)
{
if(num<10)
System.out.println(">2<10");
}
else{
System.out.println("<2");
}
OUTPUT
<2
Nesting ifs
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
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 |