Presentation is loading. Please wait.

Presentation is loading. Please wait.

Boolean expressions Conditional statements and expressions

Similar presentations


Presentation on theme: "Boolean expressions Conditional statements and expressions"— Presentation transcript:

1 Boolean expressions Conditional statements and expressions

2 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.

3 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(); }

4 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.

5 Penny guessing game Let’s accept a guess that’s 160 ± 10%.

6 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.

7 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;

8 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.” );

9 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.” );

10 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?

11 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

12 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

13 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.

14 Truth table for AND

15 Truth table for OR

16 Truth table for NOT

17 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!

18 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)) …

19 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.

20 if examples if (x == 2 & y < 10) x = 10; if (x == 2) { }

21 if examples if (x==y) { x = 0; y = 1; } else { x = 1; y = 0; }

22 if examples if (x==1) { x = 2; y = 3; } else if (x==2) { x = 3; y = 4;

23 if-else form in general
if (conds1) one-statement; or { block-of-statements } else if (conds2) else if (condsn) else

24 Boolean type and variables
boolean b; b = true; b = false; int x = 10; int y = 9; b = (x==y); b = (x>y);

25 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

26 Conditional operator ?:
Many programs contain code like the following: if (n > 0) average = sum / n; else average = 0;

27 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 );

28 Conditional operator ?:
(boolean-expr) ? exprif-true : exprif-false evaluated if-and-only-if (iff) boolean-expr is true evaluated iff boolean-expr is false

29 Conditional operator ?:
if (n > 0) average = sum / n; else average = 0; average = (n > 0) ? sum / n : 0;

30 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" : "" );

31 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 ?:?

32 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 ?:?

33 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;


Download ppt "Boolean expressions Conditional statements and expressions"

Similar presentations


Ads by Google