Translate

Search This Blog

Total Pageviews

Friday, November 30, 2018

Java beginner5: Conditionals and Logic

The programs we’ve seen in previous lessons do pretty much the same thing every time, regardless of the input. For more complex computations, programs usually react to the inputs, check for certain conditions, and generate appropriate results.
This lesson presents the features you need for programs to make decisions:
  • a new data type called boolean
  • operators for expressing logic, and 
  • if statements.

                              Relational Operators

Relational operators are used to check conditions like
  1. whether two values are equal, or 
  2. whether one is greater than the other.
The following expressions show how they are used:

x == y // x is equal to y
x != y // x is not equal to 
x > y // x is greater than y
x < y // x is less than y
x >= y // x is greater than or equal to y
x <= y // x is less than or equal to y

The result of a relational operator is one of two special values,
  1. true or 
  2. false.
These values belong to the data type boolean; in fact, they are the only boolean values.
You are probably familiar with these operations, but notice that the Java operators are different from the mathematical symbols like =, ≠, and ≤.
  1. A common error is to use a single = instead of a double ==. Remember that = is the assignment operator, and == is a comparison operator. 
  2. Also, there is no such thing as the =< or => operators.
  3. The two sides of a relational operator have to be compatible. For example, the expression 5 < "6" is invalid because 5 is an int and "6" is a String. When comparing values of different numeric types, Java applies the same conversion rules we saw previously with the assignment operator. For example, when evaluating the expression 5< 6.0, Java automatically converts the 5 to 5.0.
  4. Most relational operators don’t work with strings. But confusingly, == and != do work with strings—they just don’t do what you expect. We’ll explain what they do later; in the meantime, don’t use them with strings. Instead, you should use the equals method:
    String fruit1 = "Apple";
    String fruit2 = "Orange";
    System.out.println(fruit1.equals(fruit2));

    The result of fruit1.equals(fruit2) is the boolean value false.

                              Logical Operators

Java has three logical operators: &&, ||, and !, which respectively stand for and, or, and not. The results of these operators are similar to their meanings in English.
For example, x > 0 && x < 10 is true when x is both greater than zero and less than 10. The expression evenFlag || n % 3 == 0 is true if either condition is true, that is, if evenFlag is true or the number n is divisible by 3.

Finally, the ! operator inverts a boolean expression. So !evenFlag is true if evenFlag is not true.
Logical operators evaluate the second expression only when necessary. Ignoring the second operand, when possible, is called short circuit evaluation, by analogy with an electrical circuit.Short circuit evaluation can save time, especially if anything takes a long time to evaluate. It can also avoid unnecessary errors, if anything might fail.
For example, true || anything is always true, so Java does not need to evaluate the expression anything.
Likewise, false && anything is always false.

If you ever have to negate an expression that contains logical operators, and you
probably will, De Morgan’s laws can help:
  • !(A && B) is the same as !A || !B
  • !(A || B) is the same as !A && !B
Negating a logical expression is the same as negating each term and changing the operator.
The ! operator takes precedence over && and ||, so you don’t have to put parentheses around the individual terms !A and !B.
De Morgan’s laws also apply to the relational operators. In this case, negating each term means using the “opposite” relational operator


[to be continued...]

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.