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.
if(condition1) { if(condition2) { statement1; } else { statement2; } else { if(condition3) { statement3; } else { statement4; } 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:
The code given below, using the logical operators, is equivalent to the code given previously, using nested if s: 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.
It is the programmer’s choice whether to use nested if s 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 12)…
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 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.
if(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
if(x > 5 || x < 12) Always true:
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.
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;
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.
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; }
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);
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.
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) …
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)) …
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);
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.
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; }
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.