chapter 3 selections
play

Chapter 3 Selections false A logical value, or a truth value, is a - PDF document

boolean Type boolean type is used to represent one of the 2 truth values - true or Chapter 3 Selections false A logical value, or a truth value, is a value indicating the extent to which a proposition is true. The result of a


  1. boolean Type boolean type is used to represent  one of the 2 truth values - true or Chapter 3 Selections false A logical value, or a truth value, is a  value indicating the extent to which a proposition is true. The result of a comparison is a  boolean type 6/3 > 2  radius > 0  Boolean algebra (or Boolean logic) is a  logical calculus of truth values, CS170 developed by George Boole Introduction to Computer Science 1 Motivations The Boolean Expressions  Use a boolean variable Scanner scanner = new Scanner(System.in); boolean var; // declare a boolean variable // read in a radius var = expression; // assign a boolean value double radius = scanner.nextDouble(); boolean var = expression; // declare and assign // Compute area  A Boolean expression is an expression that evaluates to a double area = radius * radius * 3.14159; Boolean value // Display results System.out.println("The area for the circle of radius " +  Comparison operators: compare a pair of values radius + " is " + area); (numbers, characters, boolean values) boolean validInput = radius > 0;  What would happen if a negative value for radius was  Boolean operators: perform logic operations (boolean entered above? values) boolean validInput = (radius > 0) && (radius<10); 2 5 Objectives Comparison Operators  Declare and use boolean types Operator Name  Compare values using relational operators < less than  Write Boolean expressions using logic operators  Implement selection control using if statements <= less than or equal to  Implement selection control using switch statements > greater than >= greater than or equal to == equal to != not equal to 6

  2. Boolean Operators Truth Table for Operator || Operator Name ! not && and || or ^ exclusive or 7 10 Truth Table for Operator ! Truth Table for Operator ^ 8 11 Truth Table for Operator && Example – TestBoolean Scanner input = new Scanner(System.in); int number = input.nextInt(); System.out.println("Is " + number + " divisible by 2 and 3? " + ((number % 2 == 0) && (number % 3 == 0))); System.out.println("Is " + number + " divisible by 2 or 3? " + ((number % 2 == 0) || (number % 3 == 0))); System.out.println("Is " + number + " divisible by 2 or 3, but not both? " + ((number % 2 == 0) ^ (number % 3 == 0))); TestBoolean.java 9 12

  3. Boolean Expressions Evaluation Selection Statements  if statement  Java uses shortcut evaluation - evaluation of boolean expression stops as soon as the result is known, which makes  switch statement code execute faster than if all boolean operands were  Conditional operators evaluated. int x = 0; if ( x > 0 && x++ < 5) x = x - 1; 16 Problem: Determining Leap Year? Simple if Statements  Write a program that determines if given year is a leap if (booleanExpression) { if (radius >= 0) { statement(s); area = radius * radius * PI; year } System.out.println("The area" + " for the circle of radius " + radius + " is " + area); 1. Read input year } 2. Compute whether input year is a leap year - a year is a leap year if it is divisible by 4 but not by 100 or if it is false false Boolean divisible by 400 (radius >= 0) Expression 3. Display the result true true Statement(s) Compupte area and print results LeapYear.java (A) (B) 17 14 Problem: A Simple Math Learning Tool Flow Chart  A flowchart is a schematic representation of an algorithm  Write a program to let a first grader practice or a process. additions  Start and end symbols, represented as rounded rectangles  The program should randomly generate two single-  usually containing the word "Start" or "End“ digit integers number1 and number2 and display a  Arrows, showing "flow of control".  Processing steps, represented as rectangles. question such as “What is 7 + 9?” to the student  Input/Output, represented as a parallelogram.  After the student types the answer, the program  Conditional (or decision), represented as a diamond. displays a message to indicate whether the answer is  Typically contain a Yes/No question or True/False test. true or false.  Has two arrows coming out of it, usually from the bottom point and right point, one corresponding to Yes or True, and one corresponding to No or False. 15

  4. Flow Chart Example Note – Block statement if ((i > 0) && (i < 10)) { System.out.println(“i is between 0 and 10”); } if ((i > 0) && (i < 10)) System.out.println(“i is between 0 and 10”); 22 Caution – Block statement Example if ((i > 0) && (i < 10)){  Testing whether a number is even or odd int a = 10; a = a / 3; if (number % 2 == 0) { System.out.println(“i is between 0 and 10”); System.out.println(number + “ is even”); } } ? if (number % 2 != 0) { System.out.println(number + “ is odd”); if ((i > 0) && (i < 10)) } int a = 10; a = a / 3; System.out.println(“i is between 0 and 10”); if ((i > 0) && (i < 10)) int a = 10; a = a / 3; System.out.println(“i is between 0 and 10”); 23 Caution – if condition Caution – Extra semicolon  Adding a semicolon at the end of an if clause is a mistake. if (i > 0) && (i < 10){ int a = 10; if (radius >= 0); a = a / 3; Wrong System.out.println(“i is between 0 and 10”); { } area = radius*radius*PI; System.out.println("The area for the circle of radius " + radius + " is " + area); } if ((i > 0) && (i < 10)){ int a = 10; if (radius >= 0){}; a = a / 3; { System.out.println(“i is between 0 and 10”); area = radius*radius*PI; } System.out.println("The area for the circle of radius " + radius + " is " + area); } 21 24

  5. Trace if-else statement The if...else Statement if (booleanExpression) { Suppose score is 70.0 statement(s)-for-the-true-case; The condition is false } if (score >= 90.0) else { grade = 'A'; statement(s)-for-the-false-case; else if (score >= 80.0) } grade = 'B'; else if (score >= 70.0) grade = 'C'; else if (score >= 60.0) grade = 'D'; else grade = 'F'; 25 28 Trace if-else statement if...else Example Suppose score is 70.0 The condition is false if (radius >= 0) { if (score >= 90.0) area = radius * radius * 3.14159; grade = 'A'; System.out.println("The area for the circle “ + else if (score >= 80.0) “of radius ” + radius + " is " + area); grade = 'B'; } else if (score >= 70.0) else { grade = 'C'; System.out.println("Negative input"); } else if (score >= 60.0) grade = 'D'; else grade = 'F'; 26 29 Trace if-else statement Multiple Alternative if Statements Suppose score is 70.0 The condition is true if (score >= 90.0) if (score >= 90.0) grade = 'A'; grade = 'A'; if (score >= 90.0) else if (score >= 80.0) else grade = 'A'; if (score >= 80.0) Equivalent grade = 'B'; else if (score >= 80.0) else if (score >= 70.0) grade = 'B'; else grade = 'C'; grade = 'B'; else if (score >= 60.0) if (score >= 70.0) else if (score >= 70.0) grade = 'D'; grade = 'C'; grade = 'C'; else else else if (score >= 60.0) grade = 'F'; if (score >= 60.0) grade = 'D'; grade = 'D'; else else grade = 'F'; grade = 'F'; 27 30

  6. Trace if-else statement Note, cont.  To force the else clause to match the first if clause, you must add a pair of braces Suppose score is 70.0 grade is C int i = 1; if (score >= 90.0) int j = 2; grade = 'A'; int k = 3; else if (score >= 80.0) if (i > j) { grade = 'B'; if (i > k) else if (score >= 70.0) grade = 'C'; System.out.println("A"); else if (score >= 60.0) } grade = 'D'; else else System.out.println("B"); grade = 'F';  This statement prints B. 31 34 Trace if-else statement TIP – Assigning value to a boolean variable Suppose score is 70.0 Exit the if statement boolean even; if (number % 2 == 0) if (score >= 90.0) even = true; grade = 'A'; else else if (score >= 80.0) even = false; grade = 'B'; else if (score >= 70.0) grade = 'C'; else if (score >= 60.0) grade = 'D'; else boolean even = number % 2 == 0; grade = 'F'; 32 35 Note TIP – testing a boolean variable  The else clause matches the most recent if clause in the same block.  What does it print? if (even == true) int i = 1; int i = 1; Equivalent if (even) int j = 2; int j = 2; System.out.println( System.out.println( int k = 3; int k = 3; "It is even."); "It is even."); Equivalent if (i > j) if (i > j) (b) (a) if (i > k) if (i > k) System.out.println("A"); System.out.println("A"); else else System.out.println("B"); System.out.println("B"); (a) ( b ) 33 36

Recommend


More recommend