Logical Operators

Report 6 Downloads 109 Views
 

TURN INTO YOUR CLASS’ BASKET BY THE BEGINNING OF CLASS ON _________________ Name:

Date:

Period:

Logical Operators Logical operators​ , often referred to as conditional operators,​ are the symbols we use that allow ​ us to address multiple conditions at the same time. The logical operators in the Java language are: Operator

What it means...

How to think of it...

&& 

and

Both things must be true

|| 

or

At least one of the things must be true



not

The opposite

Example 1:

On the first line, a variable named ​ isFreshman​ of type ​ boolean​ with a value of ​ true​ is declared. Then, there is an ​ if ​ statement that tests whether the value of ​ isFreshman ​ is ​ true (also called the condition). What the code says is “if the value of the ​ isFreshman​ variable is true​ , then execute the code within the curly braces.” Here, call a method named ​ goAway​ . Example 2:

This time, there is an ​ if ​ statement that tests whether the value of the variable ​ isFreshman ​ is NOT​ true​ ​ . What the code says is “if the value of the ​ isFreshman​ variable is ​ NOT ​ true​ , then execute the code within the curly braces.” In this case, call a method named ​ stay.  In this example, ​ !isFreshman​ literally means “where the value of the ​ isFreshman variable is ​ NOT​ ​ true​ , i.e., ​ false​ .” The code in the ​ if ​ block will only execute if i ​sFreshman​ is ​ false​ . In this case, the method ​ stay ​ will ​ NOT ​ be called because the value of the variable ​ isFreshman ​ is  true​ . If the value was ​ false​ , then the code within the curly braces would execute. Page 1 of 4

 

TURN INTO YOUR CLASS’ BASKET BY THE BEGINNING OF CLASS ON _________________ Example 3:

On the first line, a variable named ​ isFreshman​ of type ​ boolean​ with a value of ​ true​ is declared. On the second line, another variable named ​ isCool​ , also of type ​ boolean​ with a value of ​ true​ is declared. Then, there is an ​ if ​ statement that tests whether the value of both the variables ​ isFreshman  AND ​ isCool​ are ​ true​ . What the code says is “if the value of the ​ isFreshman​ variable is ​ true AND ​ the value of the ​ isCool​ variable is ​ true​ , then execute the code within the curly braces.” In this case, call a method named ​ stay​ ! Example 4:

With the same variables as before, there is now an ​ if ​ statement that tests whether the value of the variable ​ isFreshman ​ is ​ NOT ​ true​ OR ​ ​ if the value of the variable​  isCool​ is ​ true​ . What the code says is “if the value of the ​ isFreshman​ variable is N ​OT ​ true​ OR ​ ​ the value of the isCool​ variable is ​ true​ , then execute the code within the curly braces.” In this case, call a method named ​ stay​ . In this example, ​ !isFreshman​ literally means “where the value of the ​ isFreshman variable is ​ NOT​ ​ true​ , i.e., ​ false​ .” The code in the ​ if ​ block will still execute, however, because the value of the isCool​ variable is ​ true​ . In this case, the method ​ stay ​ will b ​e called because ​ at least one​ of the parts of the condition is met.

As you can see, there are a lot of different ways to specify or create a compound condition.

Page 2 of 4

 

TURN INTO YOUR CLASS’ BASKET BY THE BEGINNING OF CLASS ON _________________ Answer the following questions while referring to the code below:

1. What ​ type of variable​ is found in the code? 2. What is the ​ condition​ that is being tested in the first ​ if ​ block? 3. What is the ​ condition ​ that is being tested in the second ​ if ​ block? 4. What is the ​ name of the method​ that is called in the first ​ if b​ lock? 5. What is the ​ name of the method​ that is called in the second ​ if ​ block? 6. What is the ​ conditional operator​ that is used to test whether a condition is ​ NOT ​ met? 7. Describe the entire block of code ​ in your own words​ .

Page 3 of 4

 

TURN INTO YOUR CLASS’ BASKET BY THE BEGINNING OF CLASS ON _________________ Now, follow the model to complete the descriptions of each code block.   

1. 

If you are in the Top Five, then    

2. 

your tardies are forgiven.

If ___________________________________ and ___________________________________,  

then ___________________________________.

    3. 

If ___________________________________ and ___________________________________,    

4. 

then ___________________________________. If ___________________________________ and

   

___________________________________, then ___________________________________.

  One of the ​ if ​ conditions above will not be met and its block of code will never run.  Which  one?  Why?  

Page 4 of 4