Conditionals boolean operators Chapter 3 Condition Statement A
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 boolean expression use relational operators to make a decision
Logical Comparison Operators Six different comparison operators are used in mathematics and computer programming. Condition In Math In Programming A equals B A=B A = B or A == B A is not equal to B A != B A is less than B A<B A is greater than B A>B A is less than or equal to B A <= B A is greater than or equal to B A >= B
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
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. 100 th Anniversary Edition
boolean Logic Operators Sometimes 2 conditions need to be checked. Compound boolean expression use logic operators in boolean expressions. answer = (x > 7 && x == 9) Both conditions must be true && AND || OR answer = (x > 7 || x == 9) Only one condition needs to be true ! Not answer = !(x > 7 || x == 9) Not reverses the result (opposite)
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
Writing boolean statements with && AND • And operator will be true only if both expressions evaluate to true. a b a && b true false true false
Writing boolean statements with && AND int x = 2 int y = 90 (x < 10 && y < 97) T T (x > 10 && y < 97) False Condition would produce True Condition would produce False True • (If one were false the whole thing would be false. )
Writing an or || boolean statement: The outcome will be true as long as one of the expressions evaluates to true. a true false b true false a || b true false
Boolean Operators • int x = 2 int y = 90 • Writing an or || boolean statement: • (x < 10 || y < 97) True • (x > 10 || y < 97) False Condition would produce True
Boolean Operators Not ! • It reverses the value of a boolean expression a outcome True False True
Boolean Operators Not ! int x = 2 int y = 90 Writing an && with ! boolean statement: (!(2 < 10) && (90 < 97)) !True Condition would produce False True !((2 < 10) && (90 < 97)) Reverses the whole things after evaluated. Condition would produce false True !True (!(2 > 10) && (90 < 97)) !False True Condition would produce True
Writing Boolean Statements You 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 (x>y && y > z); (x<0 && y<0); !(x<0 && y<0); (!(x<0) && (!y<0)); 4. x is equal to y but not equal to z ((x==y) && (!x==z));
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
if, if-else-if-else Statements The if-else class of statements should have the following form if (condition) { statements; } else { statements; } if (condition) { statements; } else if (condition) { statements; }
Conditional Statements Programming 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; } else { statement; Curly brackets optional statement; } Curly brackets required
Conditional Statements Nested if statements If statements will execute every if that is true public void grade(int test. Score) { if (test. Score >= 90) System. out. println("Your grade is A"); test. Score = 90 if (test. Score >= 80) System. out. println("Your grade is B"); if (test. Score >= 70) System. out. println("Your grade is C"); else System. out. println("Your grade is F"); } Your grade is A Your grade is B Your grade is C
Conditional Statements Nested if statements When you use the if else structure, it will stop and execute the first if else that is true. public void grade 2(int test. Score) { if (test. Score >= 90){ System. out. println("Your grade is A"); } else if (test. Score >= 80 ){ System. out. println("Your grade is B"); } else if (test. Score >= 70 ){ System. out. println("Your grade is C"); } else{ System. out. println("Your grade is F"); } } test. Score = 90 Your grade is A
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.
Need braces public void grade 2(int test. Score) { if (test. Score >= 90) System. out. println("Your grade is A"); System. out. println("First if statement"); if (test. Score >= 80) System. out. println("Your grade is B"); System. out. println("Second if statement"); if (test. Score >= 70) System. out. println("Your grade is C"); System. out. println("Third if statement"); if(test. Score < 70) System. out. println("Your grade is F"); System. out. println("Last if statement"); } test. Score = 90; Your grade is A First if statement Your grade is B Second if statement Your grade is C Third if statement Last if statement
What prints int i=3; if(i<10) System. out. println("hello"); System. out. println("goodbye");
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");
If(total = 25); { } Do not capitalize if if(total == 10) { } Use the == to show equality
- Slides: 25