Download presentation
Presentation is loading. Please wait.
Published byWesley Heath Modified over 9 years ago
1
Boolean expressions Conditional statements and expressions
2
Penny guessing game I weighed a pound of pennies and found there was exactly 160. I weighed a pound of pennies and found there was exactly 160. 160 is an integer. 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. It is also a constant that I never want to change. final int exact = 160; So let’s write a guessing game. 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 and exact are equal, then let them know. // Otherwise, let them know that their guess was incorrect. (We currently don’t have the language tools to do this!) in.close();}}
4
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!” ); System.out.println( “You are correct!” );else System.out.println( “Nope.” ); System.out.println( “Nope.” );in.close();}}
5
Penny guessing game … //check the answer if (guess==exact) System.out.println( “You are correct!” ); System.out.println( “You are correct!” );else System.out.println( “Nope.” ); System.out.println( “Nope.” );… == is a boolean (true or false) operator == is a boolean (true or false) operator It tests for equality. It tests for equality. A==B is a boolean expression A==B is a boolean expression The result of this expression is either true or false. The result of this expression is either true or false.
6
George Boole 2 November 1815 – 8 December 1864 2 November 1815 – 8 December 1864 an English mathematician and philosopher an English mathematician and philosopher As the inventor of Boolean logic—the basis of modern digital computer logic—Boole is regarded in hindsight as a founder of the field of computer science. As the inventor of Boolean logic—the basis of modern digital computer logic—Boole is regarded in hindsight as a founder of the field of computer science.
7
Penny guessing game Let’s accept a guess that’s 160 ± 10%. Let’s accept a guess that’s 160 ± 10%.
8
Penny guessing game Let’s accept a guess that’s 160 ± 10%. 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. So let’s define two more constants – the minimum and the maximum that we’ll accept.
9
Penny guessing game Let’s accept a guess that’s 160 ± 10%. 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. 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;
10
Penny guessing game Now we must change the check to include the acceptable range. Now we must change the check to include the acceptable range.… //check the answer if (guess==exact) System.out.println( “You are correct!” ); System.out.println( “You are correct!” );else System.out.println( “Nope.” ); System.out.println( “Nope.” );…
11
Penny guessing game Now we must change the check to include the acceptable range. Now we must change the check to include the acceptable range.… //check the answer if (guess==exact) System.out.println( “You are correct!” ); System.out.println( “You are correct!” ); else if (guess<min) System.out.println( “Nope.” ); System.out.println( “Nope.” ); else if (guess>max) System.out.println( “Nope.” ); System.out.println( “Nope.” );else System.out.println( “Close enough.” ); System.out.println( “Close enough.” );…
12
Penny guessing game … //check the answer if (guess==exact) System.out.println( “You are correct!” ); System.out.println( “You are correct!” ); else if (guess<min) System.out.println( “Nope.” ); System.out.println( “Nope.” ); else if (guess>max) System.out.println( “Nope.” ); System.out.println( “Nope.” );else System.out.println( “Close enough.” ); 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?
13
Penny guessing game … //check the answer if (guess==exact) System.out.println( “You are correct!” ); System.out.println( “You are correct!” ); else if (guess max) System.out.println( “Nope.” ); System.out.println( “Nope.” );else System.out.println( “Close enough.” ); System.out.println( “Close enough.” );… boolean operator for or
14
Penny guessing game … //check the answer if (guess==exact) System.out.println( “You are correct!” ); System.out.println( “You are correct!” ); else if (guess>=min & guess =min & guess<=max) System.out.println( “Close enough.” ); System.out.println( “Close enough.” );else System.out.println( “Nope.” ); System.out.println( “Nope.” );… boolean operator for and
15
Operators used in boolean expressions Unary (i.e., op A): Unary (i.e., op A): !not !not Binary (i.e., A op B): Binary (i.e., A op B):, =lt, gt, le, ge, =lt, gt, le, ge ==, !=equals, not equal ==, !=equals, not equal &and &and |or |or &&and &&and ||or ||or This is also the precedence from highest to lowest. This is also the precedence from highest to lowest.
16
Truth table for AND
17
Truth table for OR
18
Truth table for NOT
19
COMPARING STRINGS
20
Comparing strings == doesn’t work (as you expect) so please don’t use it for comparing strings. (But use == for comparing ints or doubles.) == doesn’t work (as you expect) so please don’t use it for comparing strings. (But use == for comparing ints or doubles.) Example: Example: String city1 = “Philadelphia”; String city2 = “Phoenix”; I want to say: I want to say: if (city1==city2)but I can’t!
21
Comparing strings Use.equals() instead: Use.equals() instead: if (city1.equals(city2)) ….equals() is case sensitive (i.e., NYC is not the same as NyC).equals() is case sensitive (i.e., NYC is not the same as NyC).equalsIgnoreCase() is not case sensitive.equalsIgnoreCase() is not case sensitive if (city1.equalsIgnoreCase(city2)) …
22
if’s and blocks Sometimes we wish to execute more than one statement for an if statement. 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.
23
if’s and blocks Common mistake: Are the following all the same (or are they different)? Common mistake: Are the following all the same (or are they different)? if (x==y) System.out.println( "hello" ); System.out.println( "there" ); if (x==y) System.out.println( "hello" ); System.out.println( "there" );
24
if’s and blocks Common mistake: Are the following all the same (or are they different)? Common mistake: Are the following all the same (or are they different)? if (x==y) System.out.println( "hello" ); System.out.println( "there" ); if (x==y) { System.out.println( "hello" ); System.out.println( "there" ); }
25
if’s and blocks Common mistake: Are the following all the same (or are they different)? Common mistake: Are the following all the same (or are they different)? if (x==y) { System.out.println( "hello" ); System.out.println( "there" ); } if (x==y) { System.out.println( "hello" ); System.out.println( "there" ); }
26
if’s and blocks Common mistake: Are the following all the same (or are they different)? Common mistake: Are the following all the same (or are they different)? if (x==y) { System.out.println( "hello" ); } System.out.println( "there" ); if (x==y) { System.out.println( "hello" ); } System.out.println( "there" );
27
if‘s and blocks Rule(s): Rule(s): Don’t be confused/misled by indentation! Don’t be confused/misled by indentation! The purpose of indentation is to make code more humanly readable. The purpose of indentation is to make code more humanly readable. Indentation does not matter to the compiler at all. Indentation does not matter to the compiler at all. Blocks (not indentation) group lines of code together. Blocks (not indentation) group lines of code together.
28
if examples if (x == 2 & y < 10) x = 10; if (x == 2) { x = 10; } if (x == y) { x = 0; y = 1; }
29
if examples if (x==y) { x = 0; y = 1; } else { x = 1; y = 0; }
30
if examples if (x==1) { x = 2; y = 3; } else if (x==2) { x = 3; y = 4; } else { x = 1; y = 0; }
31
if examples if (x==1) { x = 2; y = 3; } else if (x==2) { x = 3; y = 4; } else { //we don’t need to restate else if (x!=1 && x!=2) x = 1; y = 0; }
32
if-else form in general if (conds1) one-statement; or { block-of-statements } else if (conds2) one-statement; or { block-of-statements } … else if (condsn) one-statement; or { block-of-statements } else
33
if-else form Same or different? Same or different? int x = 5; if (x >= 4) System.out.println( "A" ); if (x >=5) System.out.println( "B" ); int x = 5; if (x >= 4) System.out.println( "A" ); else if (x >=5) System.out.println( "B" );
34
if-else form Same or different? Same or different? int x = 5; if (x >= 4) { System.out.println( "A" ); } else if (x >=5) { System.out.println( "B" ); } int x = 5; if (x >= 4) System.out.println( "A" ); else if (x >=5) System.out.println( "B" );
35
if-else form Same or different? Same or different? int x = 5; if (x >= 4) { System.out.println( "A" ); } else if (x >=5) { System.out.println( "B" ); } int x = 5; if (x >= 4) { System.out.println( "A" ); } else if (x >=5) { System.out.println( "B" ); }
36
Boolean type, and boolean variables boolean b; b = true; b = false; int x = 10; int y = 9; b = (x==y); b = (x>y);
37
Boolean I/O boolean answer = in.nextBoolean(); boolean answer = in.nextBoolean(); You must type in either true or false and hit return (enter). You must type in either true or false and hit return (enter). System.out.println( answer ); //works as too System.out.println( answer ); //works as too
38
Conditional operator ?: Many programs contain code like the following: if (n > 0) average = sum / n; else average = 0;
39
Conditional operator ?: example 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 ); int apples = 6; if (apples != 1) System.out.println( "I have " + apples + " apples." ); else System.out.println( "I have " + apples + " apple." ); (examples use printf and println)
40
Conditional operator ?: (boolean-expr) ? expr if-true : expr if-false evaluated if-and-only-if (iff) boolean-expr is true evaluated iff boolean-expr is false
41
Conditional operator ?: if (n > 0) average = sum / n; else average = 0; average = (n > 0) ? sum / n : 0;
42
Conditional operator ?: (example using printf) 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" : "" );
43
Conditional operator ?: (example using println) int apples = 6; if (apples != 1) System.out.println( "I have " + apples + " apples." ); else System.out.println( "I have " + apples + " apple." ); System.out.printf( "I have " + apples + " apple" + (apples != 1) ? "s" : "" + "." );
44
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. 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 if-else? How can we do this with ?:? How can we do this with ?:?
45
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. 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 if-else? if (A>B) C = A; else C = B; How can we do this with ?:? How can we do this with ?:?
46
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. 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 if-else? if (A>B) C = A; else C = B; How can we do this with ?:? How can we do this with ?:? C = (A>B) ? A : B;
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.