{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# Chapter3PPTb - Chapter 3 DECISION STRUCTURES CONT'D 1...

This preview shows pages 1–13. Sign up to view the full content.

1 Chapter 3 DECISION STRUCTURES CONT’D

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 LOGICAL OPERATORS We can combine two boolean expressions into a single expression using the logical operators && (AND) or | | (OR).
3 LOGICAL OPERATORS The && Operator The && is the logical AND operator. It is a binary operator that takes two boolean expressions as its operands and combines them into a boolean expression that has the value true , only when both of the operands have the value true . If either or both of the operands of the && operator are false , the expression formed using the && operator has the value false . The && operator has lower precedence than the arithmetic and relational operators, but higher precedence than the assignment operators. The && operator has left-to-right associativity. *** See Appendix C of the text on the CD-ROM that came with the text

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 LOGICAL OPERATORS The && Operator Example: In the statement below, the conditional expression, ( gameOver == true && player1Score > player2Score ), is true and the message Player 1 is the winner! is displayed, only if both gameOver is equal to true and player1Score is greater than player2Score . if(gameOver == true && player1Score > player2Score) { System.out.println("Player 1 is the winner!"); }
5 LOGICAL OPERATORS The && Operator The if statement on the previous slide could have equivalently been written as: if(gameOver && player1Score > player2Score) { System.out.println("Player 1 is the winner!"); }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
6 LOGICAL OPERATORS The && Operator If left operand of a logical && operator is false , the expression on the right will not even be evaluated. This is called short-circuit evaluation . It is a waste of processor time to evaluate the expression that is the right operand.
7 LOGICAL OPERATORS The && Operator Example: The statement below specifies that the value in the variable z is increased by 1 and the value in the variable w is decreased by 2, when both ch1 contains the Unicode value of the letter A and y has some value greater than 3.0. if (ch1 == ‘A’ && y > 3.0) { z += 1; w - = 2; }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
8 LOGICAL OPERATORS The && Operator The statement on the previous slide is logically equivalent to the following: if (ch1 == 'A') { if (y > 3.0) { z += 1; w -= 2; } }
9 LOGICAL OPERATORS The && Operator The && operator is useful for checking that a value is inside a numeric range.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
10 LOGICAL OPERATORS The && Operator Example: The conditional expression in the segment below is true when month has a value in the range 1 through 12 inclusive (1, 2, 3, …, 10, 11, 12). int month = 0; System.out.print("Enter the number of the month [JAN = 1, FEB = 2, etc.]: "); month = keyboard.nextInt( ); if(month >= 1 && month <= 12) { // The statements here are executed when month is in the range 1 through 12 }
11 LOGICAL OPERATORS The && Operator Java does not allow us to specify the condition as: (1 <= month <= 12) What if month has the value 7? 1 <= 7 is true , but true and 12 cannot be compared using the <= operator.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
12 LOGICAL OPERATORS The && Operator To test whether a value is inside a range, determine if it is both >= (or >) to the lower end of the range AND (&&) <= (or <) to the upper end of the range.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}