dale roberts program control using java - boolean expressions dale roberts, lecturer computer...

13
Dale Roberts Program Control using Java Program Control using Java - Boolean Expressions - Boolean Expressions Dale Roberts, Lecturer Computer Science, IUPUI E-mail: [email protected] Department of Computer and Information Science, School of Science, IUPUI

Upload: emmeline-nicholson

Post on 01-Jan-2016

236 views

Category:

Documents


1 download

TRANSCRIPT

Dale Roberts

Program Control using JavaProgram Control using Java

- Boolean Expressions- Boolean Expressions

Dale Roberts, LecturerComputer Science, IUPUIE-mail: [email protected]

Department of Computer and Information Science,School of Science, IUPUI

Dale Roberts2

5.8 Logical Operators5.8 Logical Operators

Logical operatorsLogical operatorsAllows for forming more complex conditionsAllows for forming more complex conditions

Combines simple conditionsCombines simple conditions

Java logical operatorsJava logical operators&&&& (conditional AND) (conditional AND)

|||| (conditional OR) (conditional OR)

&& (boolean logical AND) (boolean logical AND)

|| (boolean logical inclusive OR) (boolean logical inclusive OR)

^̂ (boolean logical exclusive OR) (boolean logical exclusive OR)

!! (logical NOT) (logical NOT)

Dale Roberts3

5.8 Logical Operators (Cont.)5.8 Logical Operators (Cont.)

Conditional AND (Conditional AND (&&&&) Operator) OperatorConsider the following Consider the following ifif statement statementifif ( gender == ( gender == FEMALEFEMALE && age >= && age >= 6565 ) )

++seniorFemales;++seniorFemales;

Combined condition is Combined condition is truetrue if and only if both simple conditions are if and only if both simple conditions are truetrue

Combined condition is Combined condition is falsefalse if either or both of the simple conditions are if either or both of the simple conditions are falsefalse

Dale Roberts4

Fig. 5.14 Fig. 5.14 | | &&&& (conditional AND) operator truth table. (conditional AND) operator truth table.

expression1 expression2 expression1 && expression2 false false False false true False true false False true true True

Dale Roberts5

5.8 Logical Operators (Cont.)5.8 Logical Operators (Cont.)

Conditional OR (Conditional OR (||||) Operator) OperatorConsider the following Consider the following ifif statement statementifif ( ( semesterAverage >= ( ( semesterAverage >= 9090 ) || ( finalExam >= ) || ( finalExam >= 9090 ) )

System.out.println( “Student grade is A” );System.out.println( “Student grade is A” );

Combined condition is Combined condition is truetrue if either or both of the simple condition are if either or both of the simple condition are truetrue

Combined condition is Combined condition is falsefalse if both of the simple conditions are if both of the simple conditions are falsefalse

Dale Roberts6

Fig. 5.15 Fig. 5.15 | | |||| (conditional OR) operator truth table. (conditional OR) operator truth table.

expression1 expression2 expression1 || expression2 false false false false true true true false true true true true

Dale Roberts7

5.8 Logical Operators (Cont.)5.8 Logical Operators (Cont.)

Short-Circuit Evaluation of Complex ConditionsShort-Circuit Evaluation of Complex ConditionsParts of an expression containing Parts of an expression containing &&&& or or |||| operators are operators are evaluated only until it is known whether the condition is evaluated only until it is known whether the condition is true or falsetrue or false

E.g., E.g., ( gender == ( gender == FEMALEFEMALE ) && ( age >= ) && ( age >= 6565 ) )Stops immediately if gender is not equal to Stops immediately if gender is not equal to FEMALEFEMALE

Dale Roberts8

5.8 Logical Operators (Cont.)5.8 Logical Operators (Cont.)

Boolean Logical AND (Boolean Logical AND (&&) Operator) OperatorWorks identically to Works identically to &&&&

Except Except && always evaluate both operands always evaluate both operands

Boolean Logical OR (Boolean Logical OR (||) Operator ) Operator Works identidally to Works identidally to ||||

Except Except || always evaluate both operands always evaluate both operands

Dale Roberts9

5.8 Logical Operators (Cont.)5.8 Logical Operators (Cont.)

Boolean Logical Exclusive OR (Boolean Logical Exclusive OR (^̂))One of its operands is One of its operands is truetrue and the other is and the other is falsefalse

Evaluates to Evaluates to truetrue

Both operands are Both operands are truetrue or both are or both are falsefalseEvaluates to Evaluates to falsefalse

Logical Negation (Logical Negation (!!) Operator) OperatorUnary operatorUnary operator

Dale Roberts10

Fig. 5.16 Fig. 5.16 | | ^̂ (boolean logical exclusive OR) operator truth table. (boolean logical exclusive OR) operator truth table.

expression1 expression2 expression1 ^ expression2 false false false false true true true false true true true false

Dale Roberts11

Fig. 5.17 Fig. 5.17 ||! ! (logical negation, or logical NOT) operator truth (logical negation, or logical NOT) operator truth table.table.

expression !expression false true true false

Dale Roberts12

Fig. 5.19 Fig. 5.19 | Precedence/associativity of the operators discussed | Precedence/associativity of the operators discussed so far.so far.

Operators Associativity Type

++ -- right to left unary postfix ++ - + - ! (type) right to left unary prefix * / % left to right multiplicative + - left to right additive < <= > >= left to right relational == != left to right equality & left to right boolean logical AND ^ left to right boolean logical exclusive OR | left to right boolean logical inclusive OR && left to right conditional AND || left to right conditional OR ?: right to left conditional = += -= *= /= %= right to left assignment

Dale Roberts

AcknowledgementsAcknowledgementsDeitel, Java How to Program