conditionals & boolean operators
DESCRIPTION
Conditionals & boolean operators . Chapter 3 . Condition Statement . A conditional statement is sometimes called a selection statement. Each decision is based on a boolean expression called a condition. . Relational Operators . - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Conditionals & boolean operators](https://reader036.vdocuments.site/reader036/viewer/2022081512/568161d1550346895dd1ca31/html5/thumbnails/1.jpg)
Conditionals & boolean operators
Chapter 3
![Page 2: Conditionals & boolean operators](https://reader036.vdocuments.site/reader036/viewer/2022081512/568161d1550346895dd1ca31/html5/thumbnails/2.jpg)
Condition Statement
• A conditional statement is sometimes called a selection statement.
• Each decision is based on a boolean expression called a condition.
![Page 3: Conditionals & boolean operators](https://reader036.vdocuments.site/reader036/viewer/2022081512/568161d1550346895dd1ca31/html5/thumbnails/3.jpg)
Relational Operators
Relational Operators:
Operation equal to
not equal to
greater than
less than
less than or equal to
greater than or equal to
Algebra =
≠
>
<
≤
≥
Java ==
!=
>
<
<=
>=
Note 1: You can not have a space between !=, ==, > =, or < = Note 2: You can not have =!, =>, or =<
boolean expression use relational operators to make a decision
![Page 4: Conditionals & boolean operators](https://reader036.vdocuments.site/reader036/viewer/2022081512/568161d1550346895dd1ca31/html5/thumbnails/4.jpg)
Logical Comparison Operators
Condition In Math In ProgrammingA equals B A = B A = B or A == B
A is not equal to B A B A != B
A is less than B A < B A < B
A is greater than B A > B A > B
A is less than or equal to B A B A <= B
A is greater than or equal to B A B A >= B
Six different comparison operators are used in mathematics and computer programming.
![Page 5: Conditionals & boolean operators](https://reader036.vdocuments.site/reader036/viewer/2022081512/568161d1550346895dd1ca31/html5/thumbnails/5.jpg)
Logical Conditions
Logical comparisons that are either true or false are most often used as the basis for the true and false values in Boolean logic.
They are often used for simple conditions in branching and looping instructions.
if (hours > 40)
pay overtime
if (age < 12)
stay in the back seat
while (count 10)
print count increment count
![Page 6: Conditionals & boolean operators](https://reader036.vdocuments.site/reader036/viewer/2022081512/568161d1550346895dd1ca31/html5/thumbnails/6.jpg)
Boolean Logic
Boolean logic is a form of mathematics in which the only values used are true and false.
Boolean logic is the basis of all modern computing.
There are three basic operations in Boolean logic – AND, OR, and NOT.
100th Anniversary Edition
![Page 7: Conditionals & boolean operators](https://reader036.vdocuments.site/reader036/viewer/2022081512/568161d1550346895dd1ca31/html5/thumbnails/7.jpg)
boolean Logic Operators
&& AND
|| OR
! Not
Sometimes 2 conditions need to be checked.
Compound boolean expression use logic operators in boolean expressions.
answer = (x > 7 && x == 9)
answer = (x > 7 || x == 9)
Both conditions must be true
Only one condition needs to be true
answer = !(x > 7 || x == 9)
Not reverses the result (opposite)
![Page 8: Conditionals & boolean operators](https://reader036.vdocuments.site/reader036/viewer/2022081512/568161d1550346895dd1ca31/html5/thumbnails/8.jpg)
Compound Conditions
Compound Boolean conditions can be created using the Boolean AND, OR and NOT operations in branching and looping instructions.
if ( (hours > 40) && (type = hourly) ) pay overtime
if ( (age < 12) || (height < 42 in.) ) stay in the back seat
while ( (count <= 10) && ! (status = away) ) print name.count increment count
![Page 9: Conditionals & boolean operators](https://reader036.vdocuments.site/reader036/viewer/2022081512/568161d1550346895dd1ca31/html5/thumbnails/9.jpg)
Writing boolean statements with && AND
• And operator will be true only if both expressions evaluate to true.
a b a && b
true true true
true false false
false true false
false false false
![Page 10: Conditionals & boolean operators](https://reader036.vdocuments.site/reader036/viewer/2022081512/568161d1550346895dd1ca31/html5/thumbnails/10.jpg)
Writing boolean statements with && AND
int x = 2 int y = 90 (x < 10 && y < 97)
(x > 10 && y < 97)
• (If one were false the whole thing would be false.)
False True
Condition would produce True
Condition would produce False
T T
![Page 11: Conditionals & boolean operators](https://reader036.vdocuments.site/reader036/viewer/2022081512/568161d1550346895dd1ca31/html5/thumbnails/11.jpg)
Writing an or || boolean statement:
a b a || b true true true true false true false true true false false false
The outcome will be true as long as one of the expressions evaluates to true.
![Page 12: Conditionals & boolean operators](https://reader036.vdocuments.site/reader036/viewer/2022081512/568161d1550346895dd1ca31/html5/thumbnails/12.jpg)
Boolean Operators
• int x = 2 int y = 90
• Writing an or || boolean statement:
• (x < 10 || y < 97)
• (x > 10 || y < 97)
True
False
True
True
Condition would produce True
Condition would produce True
![Page 13: Conditionals & boolean operators](https://reader036.vdocuments.site/reader036/viewer/2022081512/568161d1550346895dd1ca31/html5/thumbnails/13.jpg)
Boolean Operators Not !
• It reverses the value of a boolean expression
a outcome
True False
False True
![Page 14: Conditionals & boolean operators](https://reader036.vdocuments.site/reader036/viewer/2022081512/568161d1550346895dd1ca31/html5/thumbnails/14.jpg)
Boolean Operators Not !int x = 2 int y = 90
Writing an && with ! boolean statement:
(!(2 < 10) && (90 < 97))
!((2 < 10) && (90 < 97)) Reverses the whole things after evaluated. Condition would produce false
(!(2 > 10) && (90 < 97))
!True
!False
True
True
Condition would produce False
Condition would produce True
True True
!True
![Page 15: Conditionals & boolean operators](https://reader036.vdocuments.site/reader036/viewer/2022081512/568161d1550346895dd1ca31/html5/thumbnails/15.jpg)
Writing Boolean StatementsYou must write the full condition when using logic operators ):
1. x > y > z
2. x and y are both less than 0
3. neither x nor y is less than 0
4. x is equal to y but not equal to z
(x>y && y > z);
(x<0 && y<0);
(!(x<0) && (!y<0));
!(x<0 && y<0);
((x==y) && (!x==z));
![Page 16: Conditionals & boolean operators](https://reader036.vdocuments.site/reader036/viewer/2022081512/568161d1550346895dd1ca31/html5/thumbnails/16.jpg)
Operator precedence
Operator Associativity
* / % left to right+ - left to right< <= > >= left to right== != left to right&& (and) left to right|| (or) left to right= += -= *= /= right to left
![Page 17: Conditionals & boolean operators](https://reader036.vdocuments.site/reader036/viewer/2022081512/568161d1550346895dd1ca31/html5/thumbnails/17.jpg)
if, if-else, if-else-if-else StatementsThe if-else class of statements should have the following form
if (condition) {statements;} else {statements;}
if (condition) {statements;} else if (condition) {statements;} else if (condition) {statements;}
if (condition) {statements;}
![Page 18: Conditionals & boolean operators](https://reader036.vdocuments.site/reader036/viewer/2022081512/568161d1550346895dd1ca31/html5/thumbnails/18.jpg)
Conditional StatementsProgramming style
Note that if there is only a single statement in the if or else block, curly brackets are not needed. If there is more than one statement in one of these blocks, the curly brackets are required.
if (boolean condition)
statement;
else
statement;
if (boolean condition) {
statement;
statement;
}
else {
statement;
statement;
}Curly brackets optional
Curly brackets required
![Page 19: Conditionals & boolean operators](https://reader036.vdocuments.site/reader036/viewer/2022081512/568161d1550346895dd1ca31/html5/thumbnails/19.jpg)
Conditional StatementsNested if statements
If statements will execute every if that is true
public void grade(int testScore) { if (testScore >= 90) System.out.println("Your grade is A"); if (testScore >= 80) System.out.println("Your grade is B"); if (testScore >= 70) System.out.println("Your grade is C"); else System.out.println("Your grade is F"); }
testScore = 90
Your grade is AYour grade is BYour grade is C
![Page 20: Conditionals & boolean operators](https://reader036.vdocuments.site/reader036/viewer/2022081512/568161d1550346895dd1ca31/html5/thumbnails/20.jpg)
Conditional StatementsNested if statements
When you use the if else structure, it will stop and execute the first if else that is true.
public void grade2(int testScore) { if (testScore >= 90){ System.out.println("Your grade is A"); } else if (testScore >= 80 ){ System.out.println("Your grade is B");} else if (testScore >= 70 ){ System.out.println("Your grade is C"); } else{ System.out.println("Your grade is F"); } }
testScore = 90
Your grade is A
![Page 21: Conditionals & boolean operators](https://reader036.vdocuments.site/reader036/viewer/2022081512/568161d1550346895dd1ca31/html5/thumbnails/21.jpg)
No braces?
if (condition) //AVOID! THIS OMITS THE BRACES {}!
statement;
If you do not use braces the first statement after the if condition is the only one that goes with it.
![Page 22: Conditionals & boolean operators](https://reader036.vdocuments.site/reader036/viewer/2022081512/568161d1550346895dd1ca31/html5/thumbnails/22.jpg)
Need braces public void grade2(int testScore) { if (testScore >= 90) System.out.println("Your grade is A"); System.out.println("First if statement"); if (testScore >= 80) System.out.println("Your grade is B"); System.out.println("Second if statement"); if (testScore >= 70) System.out.println("Your grade is C"); System.out.println("Third if statement"); if(testScore < 70) System.out.println("Your grade is F"); System.out.println("Last if statement"); }
testScore = 90;
Your grade is AFirst if statementYour grade is BSecond if statementYour grade is CThird if statementLast if statement
![Page 23: Conditionals & boolean operators](https://reader036.vdocuments.site/reader036/viewer/2022081512/568161d1550346895dd1ca31/html5/thumbnails/23.jpg)
What prints
int i=3; if(i<10) System.out.println("hello"); System.out.println("goodbye");
![Page 24: Conditionals & boolean operators](https://reader036.vdocuments.site/reader036/viewer/2022081512/568161d1550346895dd1ca31/html5/thumbnails/24.jpg)
What prints
int i=3, j=14; if(i<10) System.out.println("hello");if(j>10)System.out.println("howdy");System.out.println("goodbye");
![Page 25: Conditionals & boolean operators](https://reader036.vdocuments.site/reader036/viewer/2022081512/568161d1550346895dd1ca31/html5/thumbnails/25.jpg)
If(total = 25);{}
if(total == 10){}
Do not capitalize if
Use the == to show equality