Chapter 2 Introduction to Java Applications 79 The (Space web hosting)

Chapter 2 Introduction to Java Applications 79 The circled numbers under the statement indicate the order in which Java applies the operators. The multiplication, modulus and division operations are evaluated first in left-toright order (i.e., they associate from left to right), because they have higher precedence than that of addition and subtraction. The addition and subtraction operations are evaluated next. These operations are also applied from left to right. Not all expressions with several pairs of parentheses contain nested parentheses. For example, the expression a * ( b + c ) + c * ( d + e ) does not contain nested parentheses. Rather, these parentheses are on the same level. To develop a better understanding of the rules of operator precedence, consider the evaluation of a second-degree polynomial (y = ax2 + bx + c): y = a * x * x + b * x + c; 16 2 4 3 5 The circled numbers under the preceding statement indicate the order in which Java applies the operators. There is no arithmetic operator for exponentiation in Java; x2 is represented as x*x. Suppose that a, b, c and x are initialized as follows: a=2, b=3, c=7 and x=5. Figure 2.18 illustrates the order in which the operators are applied in the preceding second- degree polynomial. As in algebra, it is acceptable to place unnecessary parentheses in an expression to make the expression clearer. Such unnecessary parentheses are also called redundant parentheses. For example, the preceding assignment statement might be parenthesized as follows: y = ( a * x * x ) + ( b * x ) + c; Good Programming Practice 2.16 Using parentheses for complex arithmetic expressions, even when the parentheses are not necessary, can make the arithmetic expressions easier to read. 2.8 Decision Making: Equality and Relational Operators This section introduces a simple version of Java s if structure that allows a program to make a decision based on the truth or falsity of some condition. If the condition is met (i.e., the condition is true), the statement in the body of the if structure is executed. If the condition is not met (i.e., the condition is false), the body statement does not execute. We will see an example shortly. Conditions in if structures can be formed by using the equality operators and relational operators summarized in Fig. 2.19. The relational operators all have the same level of precedence and associate from left to right. The equality operators both have the same level of precedence, which is lower than the precedence of the relational operators. The equality operators also associate from left to right. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

Leave a Reply