This preview shows pages 1–12. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 1 Chapter 3 DECISION STRUCTURES CONT’D 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 lefttoright associativity. *** See Appendix C of the text on the CDROM that came with the text 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!"); } 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 shortcircuit 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; } 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. 10 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 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?...
View
Full
Document
This note was uploaded on 04/14/2008 for the course CS 1336 taught by Professor Thompson during the Spring '08 term at University of Texas at Dallas, Richardson.
 Spring '08
 Thompson

Click to edit the document details