java has the logical operators and, or, and not, symbolized by &&, ||, and !, respectively. ...

20
6.3 LOGICAL OPERATORS, COMPOUND EXPRESSIONS, AND BOOLEAN VARIABLES

Upload: abel-hopkins

Post on 16-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java has the logical operators and, or, and not, symbolized by &&, ||, and !, respectively.  Logical or means one or the other or both conditions hold

6.3 LOGICAL OPERATORS, COMPOUND EXPRESSIONS, AND BOOLEAN VARIABLES

Page 2: Java has the logical operators and, or, and not, symbolized by &&, ||, and !, respectively.  Logical or means one or the other or both conditions hold

Logical Operators

Java has the logical operators and, or, and not, symbolized by &&, ||, and !, respectively.

Logical or means one or the other or both conditions hold true.

It is also possible to devise logical expressions which have the meaning of exclusive or, which means one or the other, but not both hold true.

Parentheses can be used in logical expressions to clarify the grouping of different conditions.

Once the logical operators are available it becomes possible to construct complex conditions.

Page 3: Java has the logical operators and, or, and not, symbolized by &&, ||, and !, respectively.  Logical or means one or the other or both conditions hold

if(condition1){ if(condition2) { statement1; } else { statement2; }}else{ if(condition3) { statement3; } else { statement4; }}

Remember: Nested If Structure

statement1 is executed if condition1 is true and condition2 is true.

statement2 is executed if condition1 is true and condition2 is not true.

statement3 is executed if condition1 is not true and condition3 is true.

statement4 is executed if condition1 is not true and condition3 is not true.

The combination of conditions for the previous example is repeated here:

Page 4: Java has the logical operators and, or, and not, symbolized by &&, ||, and !, respectively.  Logical or means one or the other or both conditions hold

Nested ifs VS logical operators

The code given below, using the logical operators, is equivalent to the code given previously, using nested ifs:

if(condition1 && condition2){ statement1;}else if(condition1 && !condition2){ statement2;}else if(!condition1 && condition3){ statement3;}else if(!condition1 && !condition3){ statement4;}else{}

statement1 is executed if condition1 is true and condition2 is true.

statement2 is executed if condition1 is true and condition2 is not true.

statement3 is executed if condition1 is not true and condition3 is true.

statement4 is executed if condition1 is not true and condition3 is not true.

Page 5: Java has the logical operators and, or, and not, symbolized by &&, ||, and !, respectively.  Logical or means one or the other or both conditions hold

It’s your choice

It is the programmer’s choice whether to use nested ifs or logical operators.

It is possible to construct arbitrarily complex logical expressions.

Depending on the variables at hand and their meanings, you may have statements involving more than one numerical variable.

They could take the following form:if(x < 5 && y > 12)…if(x < 5 || y > 12)…

Page 6: Java has the logical operators and, or, and not, symbolized by &&, ||, and !, respectively.  Logical or means one or the other or both conditions hold

Number lines

You may also have comparisons involving a single numerical variable that bring up properties of number lines from algebra. Here are four such examples:

if(x < 5 && x > 12) Never true: A value can never be less than 5 and

more than 12 at the same time. The statement after this if statement will never be executed in any case.

Page 7: Java has the logical operators and, or, and not, symbolized by &&, ||, and !, respectively.  Logical or means one or the other or both conditions hold

Number lines, cont.

if(x < 5 || x > 12) True for values outside of the range from 5 to

12:

if(x > 5 && x < 12) True for values inside the range from 5 to 12

Page 8: Java has the logical operators and, or, and not, symbolized by &&, ||, and !, respectively.  Logical or means one or the other or both conditions hold

Number lines, cont.

if(x > 5 || x < 12) Always true:

Page 9: Java has the logical operators and, or, and not, symbolized by &&, ||, and !, respectively.  Logical or means one or the other or both conditions hold

Equality and Inequality

When using if statements, execution of certain blocks of code depends on the truth value of conditions such as numeric comparisons of equality and inequality.

Depending on the values of variables, each condition has a value of true or false.

Page 10: Java has the logical operators and, or, and not, symbolized by &&, ||, and !, respectively.  Logical or means one or the other or both conditions hold

The boolean simple type

Java has a simple type called boolean that can contain truth values.

true and false are keywords designating the two values a boolean variable can contain.

Here is an example of declaration and assignment:

boolean someResult;someResult = true;someResult = false;

Page 11: Java has the logical operators and, or, and not, symbolized by &&, ||, and !, respectively.  Logical or means one or the other or both conditions hold

The boolean return value

The next question to consider is that of notifying the user when one of several possible actions is taken by a method.

The way to do this is by giving the method a type and a return value that signifies which action was taken.

For a method where there are essentially two alternatives, either some action or no action, a boolean return value is a reasonable choice.

Page 12: Java has the logical operators and, or, and not, symbolized by &&, ||, and !, respectively.  Logical or means one or the other or both conditions hold

Example method

The increaseSeedCount() method is rewritten below to return a value indicating whether the seedCount was increased or not.

The method is now typed boolean and one of the two boolean values is returned at the end of the two different execution paths through the method.

public boolean increaseSeedCount(int addedNumber) { if(addedNumber > 0) { seedCount = seedCount + addedNumber; return true; } else return false; }

Page 13: Java has the logical operators and, or, and not, symbolized by &&, ||, and !, respectively.  Logical or means one or the other or both conditions hold

Use of this method

In code that makes use of the method, a call would take the form illustrated in the following fragment:

boolean callResult;int moreSeeds = 7;Cup4 myCup = new Cup4(5);callResult = myCup.increaseSeedCount(moreSeeds);

Page 14: Java has the logical operators and, or, and not, symbolized by &&, ||, and !, respectively.  Logical or means one or the other or both conditions hold

After the method

After the call it would be possible to take certain actions in the program depending on whether the return value was true or false.

The structure of such actions might be further if statements.

Note that when using boolean values in if conditions, it is not necessary to use a comparison symbol to test equality.

Page 15: Java has the logical operators and, or, and not, symbolized by &&, ||, and !, respectively.  Logical or means one or the other or both conditions hold

After the method, cont.

Note that when using boolean values in if conditions, it is not necessary to use a comparison symbol to test equality.

It is sufficient to use a variable containing the truth value by itself.

For example, a line of code like that shown below could follow the code shown above.

The variable callResult alone could serve as the contents of an if condition:

if(callResult) …

Page 16: Java has the logical operators and, or, and not, symbolized by &&, ||, and !, respectively.  Logical or means one or the other or both conditions hold

A different way to do the same thing

It is also possible to write code in the form shown below.

This is not recommended for beginning programmers because it can be difficult to read.

The call to increase the seedCount is embedded in the parentheses of the if statement.

When the call is made, a truth value is returned. The action of the if statement depends on this

return value, which is unnamed and “invisible”. if(myCup.increaseSeedCount(moreSeeds)) …

Page 17: Java has the logical operators and, or, and not, symbolized by &&, ||, and !, respectively.  Logical or means one or the other or both conditions hold

Disregarding the return value

It is true in general that it is not necessary to capture the return value.

There may be cases where you aren’t interested in the return value.

If so, it can be ignored and this is not a syntactical error.

For example, this is a valid line of code: myCup.increaseSeedCount(moreSeeds);

Page 18: Java has the logical operators and, or, and not, symbolized by &&, ||, and !, respectively.  Logical or means one or the other or both conditions hold

Variable for the return value

Notice that in the increaseSeedCount() method code there were two return statements.

They were in the two mutually exclusive alternatives of an if statement.

Whenever a return statement is encountered this ends the execution of the method.

It is possible to consolidate such a situation by assigning a return value to a variable and returning the variable at the bottom of the method.

Page 19: Java has the logical operators and, or, and not, symbolized by &&, ||, and !, respectively.  Logical or means one or the other or both conditions hold

Variable for the return value, cont.

Here is the method modified in that way:

public boolean increaseSeedCount(int addedNumber) { boolean returnValue;   if(addedNumber > 0) { seedCount = seedCount + addedNumber; returnValue = true; } else returnValue = false;   return returnValue; }

Page 20: Java has the logical operators and, or, and not, symbolized by &&, ||, and !, respectively.  Logical or means one or the other or both conditions hold

Variable for the return value, cont.

This code is neither better nor worse than the previous version in an absolute sense.

Conceptually, it does illustrate the fact that you can ultimately only return once out of any method.

Also, this might be useful in the case where you don’t want to return right away.

For instance, you may want to decide to return true or false, then do some other functionality before returning.