Boolean expressions Conditional statements and expressions
Penny guessing game I weighed a pound of pennies and found there was exactly 160. 160 is an integer. int exact = 160; But nothing stops me from changing it. It is also a constant that I never want to change. final int exact = 160; So let’s write a guessing game.
Penny guessing game import java.util.Scanner; public class GuessingGame { public static void main ( String s[] ) { final int exact = 160; Scanner in = new Scanner( System.in ); //prompt for a guess System.out.print( “Guess the # of pennies in a pound. “ ); int guess = in.nextInt(); //check the answer if (guess==exact) System.out.println( “You are correct!” ); else System.out.println( “Nope.” ); in.close(); }
Penny guessing game … //check the answer if (guess==exact) System.out.println( “You are correct!” ); else System.out.println( “Nope.” ); == is a boolean (true or false) operator It tests for equality. A==B is a boolean expression The result of this expression is either true or false.
Penny guessing game Let’s accept a guess that’s 160 ± 10%.
Penny guessing game Let’s accept a guess that’s 160 ± 10%. So let’s define two more constants – the minimum and the maximum that we’ll accept.
Penny guessing game Let’s accept a guess that’s 160 ± 10%. So let’s define two more constants – the minimum and the maximum that we’ll accept. final int exact = 160; final int max = exact + exact/10; final int min = exact - exact/10;
Penny guessing game Now we must change the check to include the acceptable range. … //check the answer if (guess==exact) System.out.println( “You are correct!” ); else System.out.println( “Nope.” );
Penny guessing game Now we must change the check to include the acceptable range. … //check the answer if (guess==exact) System.out.println( “You are correct!” ); else if (guess<min) System.out.println( “Nope.” ); else if (guess>max) else System.out.println( “Close enough.” );
Penny guessing game … //check the answer if (guess==exact) System.out.println( “You are correct!” ); else if (guess<min) System.out.println( “Nope.” ); else if (guess>max) else System.out.println( “Close enough.” ); Note that we write “Nope.” if the first boolean expression or the second boolean expression is true. Wouldn’t it be nice if we had a boolean operator for or?
Penny guessing game … //check the answer if (guess==exact) System.out.println( “You are correct!” ); else if (guess<min | guess>max) System.out.println( “Nope.” ); else System.out.println( “Close enough.” ); boolean operator for or
Penny guessing game … //check the answer if (guess==exact) System.out.println( “You are correct!” ); else if (guess>=min & guess<=max) System.out.println( “Close enough.” ); else System.out.println( “Nope.” ); boolean operator for and
Operators used in boolean expressions Unary (i.e., op A): ! not Binary (i.e., A op B): <, >, <=, >= lt, gt, le, ge ==, != equals, not equal & and | or && and || or This is also the precedence from highest to lowest.
Truth table for AND
Truth table for OR
Truth table for NOT
Comparing strings == doesn’t work (as you expect) so please don’t use it. Example: String city1 = “Philadelphia”; String city2 = “Phoenix”; I want to say: if (city1==city2) but I can’t!
Comparing strings Use .equals() instead: if (city1.equals(city2)) … .equals() is case sensitive (i.e., NYC is not the same as NyC) .equalsIgnoreCase() is not case sensitive if (city1.equalsIgnoreCase(city2)) …
if’s and blocks Sometimes we wish to execute more than one statement for an if statement. if (x==y) singleStatement; if (x==y) { statement1; statement2; … statementn; } { s1; s2; … sn; } This is called a block.
if examples if (x == 2 & y < 10) x = 10; if (x == 2) { }
if examples if (x==y) { x = 0; y = 1; } else { x = 1; y = 0; }
if examples if (x==1) { x = 2; y = 3; } else if (x==2) { x = 3; y = 4;
if-else form in general if (conds1) one-statement; or { block-of-statements } else if (conds2) … else if (condsn) else
Boolean type and variables boolean b; b = true; b = false; int x = 10; int y = 9; b = (x==y); b = (x>y);
Boolean I/O boolean answer = in.nextBoolean(); You must type in either true or false and hit return (enter). System.out.println( answer ); //works as too
Conditional operator ?: Many programs contain code like the following: if (n > 0) average = sum / n; else average = 0;
Conditional operator ?: Many programs contain code like the following: int apples = 6; if (apples != 1) System.out.printf( "I have %d apples \n", apples ); else System.out.printf( "I have %d apple \n", apples );
Conditional operator ?: (boolean-expr) ? exprif-true : exprif-false evaluated if-and-only-if (iff) boolean-expr is true evaluated iff boolean-expr is false
Conditional operator ?: if (n > 0) average = sum / n; else average = 0; average = (n > 0) ? sum / n : 0;
Conditional operator ?: int apples = 6; if (apples != 1) System.out.printf( "I have %d apples \n", apples ); else System.out.printf( "I have %d apple \n", apples ); System.out.printf( "I have %d apple%s \n", apples, (apples != 1) ? "s" : "" );
Conditional operator ?: Problem: We have 2 int’s, A and B. We want another int, C, to be set to the maximum of A and B. How can we do this with if-else? How can we do this with ?:?
Conditional operator ?: Problem: We have 2 int’s, A and B. We want another int, C, to be set to the maximum of A and B. How can we do this with if-else? if (A>B) C = A; else C = B; How can we do this with ?:?
Conditional operator ?: Problem: We have 2 int’s, A and B. We want another int, C, to be set to the maximum of A and B. How can we do this with if-else? if (A>B) C = A; else C = B; How can we do this with ?:? C = (A>B) ? A : B;