Compound Boolean Expressions and Comparing ... - GitHub Pages
Unit 3: Boolean Expressions, if Statements
Compound Boolean Expressions and Comparing Objects
Adapted from:
1) Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp 2) Runestone CSAwesome Curriculum
Evaluating logic expressions
Sometimes it is useful to use nested if conditions: if statements within if statements.
// if x is odd if(x % 2 != 0){
// if x is positive if(x > 0){ ... } }
We can combine the above nested if conditions using logical operators.
2
Logical operators
? Tests can be combined using logical operators:
Operator
&&
Description
and
Example
(2 == 3) && (-1 < 5)
Result
false
||
or
(2 == 3) || (-1 < 5) true
!
not
!(2 == 3)
true
? "Truth tables" for each, used with logical values p and q:
p
q
p && q p || q
true true true true
true false false true
false true false true
false false false false
p
!p
true false
false true
3
Combining Tests
The following code
// if x is odd if(x % 2 != 0){
// if x is positive if(x > 0){ ... } }
is equivalent to:
// if x is odd and positive if(x % 2 != 0 && x > 0){
... }
4
Using boolean
? Why is type boolean useful?
? Can capture a complex logical test result and use it later ? Can write a method that does a complex test and returns it ? Makes code more readable ? Can pass around the result of a logical test (as param/return)
int age = 21, height = 88; double salary = 100000;
boolean goodAge = age >= 12 && age < 29; //true
boolean goodHeight = height >= 78 && height < 84; //false
boolean rich
= salary >= 100000.0; //true
5
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- logic gates and truth tables the diverse compendium of
- compound boolean expressions and comparing github pages
- truth table of a boolean function penn state engineering
- truth tables for negation conjunction and disjunction
- truth tables for compound logical propositions worksheet
- truth table example
- 7 4 abbreviated truth tables texas a m university
- automatic table ground truth generation and a background
- logic and truth tables metropolitan community college
- random table and its ground truth automatic generation a
Related searches
- common english expressions and idioms
- popular expressions and sayings
- meanings of expressions and sayings
- dictionary of expressions and idioms
- calculator for expressions and equations
- expressions and equations practice
- algebraic expressions and equations practice
- identify equations expressions and inequalities
- expressions and equations practice pdf
- expressions and equations answer key
- expressions and equations answers
- writing expressions and equations pdf