CS1150 Principles of Computer Science Boolean, Selection Statements - - PowerPoint PPT Presentation

cs1150 principles of computer science
SMART_READER_LITE
LIVE PREVIEW

CS1150 Principles of Computer Science Boolean, Selection Statements - - PowerPoint PPT Presentation

CS1150 Principles of Computer Science Boolean, Selection Statements Yanyan Zhuang Department of Computer Science http://www.cs.uccs.edu/~yzhuang CS1150 UC. Colorado Springs Review What happens when we create a Scanner? o Scanner input =


slide-1
SLIDE 1
  • UC. Colorado Springs

CS1150

CS1150 Principles of Computer Science Boolean, Selection Statements

Yanyan Zhuang

Department of Computer Science http://www.cs.uccs.edu/~yzhuang

slide-2
SLIDE 2

Review

  • UC. Colorado Springs

CS1150

  • What happens when we create a Scanner?
  • Scanner input = new Scanner(System.in);
  • int value = input.nextInt();
  • // close the Scanner
slide-3
SLIDE 3

Overview

  • UC. Colorado Springs

CS1150

  • Boolean data type
  • If statement
  • Switch statement
slide-4
SLIDE 4

The boolean Type and Operators

  • UC. Colorado Springs

CS1150

Often a program needs to compare two values, such as whether i is greater than j. Java provides six comparison operators (aka relational

  • perators) to compare two values. See next slide.

Result of the comparison is a boolean value: true or false.

boolean b = (1 > 2);

Similar to int, float, double, etc., boolean is just another data type.

slide-5
SLIDE 5

Relational Operators

  • UC. Colorado Springs

CS4500/5500

boolean b = (1 > 2); true and false are boolean literals

Java Mathematics Name Example Result

Operator Symbol (radius is 5) < < less than radius < 0 false <= ≤ less than or equal to radius <= 0 false > > greater than radius > 0 true >= ≥ greater than or equal to radius >= 0 true == = equal to radius == 0 false != ≠ not equal to radius != 0 true

slide-6
SLIDE 6

6

Logical Operators

Operator Name Description ! not logical negation && and logical conjunction ||

  • r

logical disjunction ^ exclusive or logical exclusion

slide-7
SLIDE 7

7

Truth Table for Operator ! (not)

p !p Example (assume age = 24, weight = 140) true false !(age > 18) is false, because (age > 18) is true. false true !(weight == 150) is true, because (weight == 150) is false.

slide-8
SLIDE 8

8

Truth Table for Operator && (and)

p1 p2 p1 && p2 Example (assume age = 24, weight = 140) false false false (age <= 18) && (weight < 140) is false, because both conditions are false. false true false true false false (age > 18) && (weight > 140) is false, because (weight > 140) is false. true true true (age > 18) && (weight >= 140) is true, because both (age > 18) and (weight >= 140) are true.

slide-9
SLIDE 9

9

Truth Table for Operator || (or)

p1 p2 p1 || p2 Example (assume age = 24, weihgt = 140) false false false false true true (age > 34) || (weight <= 140) is true, because (age > 34) is false, but (weight <= 140) is true. true false true (age > 14) || (weight >= 150) is true, because (age > 14) is true. true true true

slide-10
SLIDE 10

10

Truth Table for Operator ^ (exclusive or)

p1 p2 p1 ^ p2 Example (assume age = 24, weight = 140) false false false (age > 34) ^ (weight > 140) is false, because (age > 34) is false and (weight > 140) is false. false true true (age > 34) ^ (weight >= 140) is true, because (age > 34) is false but (weight >= 140) is true. true false true (age > 14) ^ (weight > 140) is true, because (age > 14) is true and (weight > 140) is false. true true false

slide-11
SLIDE 11

11

Short Circuit Evaluation

  • We stop evaluating a boolean expression as soon as

its value can be determined

  • This happens when have a compound expressions

involving && and ||

  • && - stop once an expression that evaluated to

false is found

  • || - stops once an expression that evaluates to

true is found

  • Example: Boolean1.java
slide-12
SLIDE 12

Order of Operators (Section 3.15)

  • UC. Colorado Springs

CS1150

  • Anything in parentheses
  • expr++ expr--

(postfix)

  • + - ++expr --expr

(unary plus/minus, prefix)

  • (type)

(Casting)

  • !

(not)

  • * / %

(multiplication, division, remainder)

  • + -

(binary addition, subtraction)

  • < <= > >=

(relational operators)

  • == !=

(equality)

  • ^

(exclusive or)

  • &&

(and)

  • ||

(or)

  • = += -= *= /= %=

(assignment, augmented assignment) Let’s see Boolean1.java as an example

slide-13
SLIDE 13

Selection Statements

  • UC. Colorado Springs

CS1150

  • Homework 1, Question 4 we assumed r>0
  • Calculate surface area of a sphere with radius r (user input)
  • Everything is fine as long as r is a positive number
  • What if the user input is a negative number?
  • Write code that can handle user input that is not correct
  • Types of selection statements
  • if statement
  • switch statement
slide-14
SLIDE 14

If Statement

  • UC. Colorado Springs

CS1150

  • Select a path of execution based on a

condition

  • A condition is evaluated (i.e. boolean expression)
  • If the condition is true

} The statements in the true branch are executed

  • If the condition is false

} The statements in false branch are executed (if there is an "else")

slide-15
SLIDE 15

One-way if Statements

  • UC. Colorado Springs

CS4500/5500

if (boolean-expression) { statement(s); }

slide-16
SLIDE 16

One-way if Statements

  • UC. Colorado Springs

CS4500/5500

  • But only one statement can omit {}
  • Let’s see code Boolean2.java, Even1.java

i f i > 0 {

System .out.pr intln(" i is po sitive "); }

(a) W rong (b) Correct

if (i > 0) { Sys tem.out .printl n("i is positi ve"); }

i f (i > 0) {

System .out.pr intln(" i is po sitive" ); }

(a)

Equivalent

(b)

if (i > 0) Syste m.out. println ("i is positiv e");

slide-17
SLIDE 17

Two-way if Statement

  • UC. Colorado Springs

CS4500/5500

if (boolean-expression) { statement(s)-for-the-true-case; } else { statement(s)-for-the-false-case; } Example: Even2.java

slide-18
SLIDE 18

Multiple Alternative if Statements

  • UC. Colorado Springs

CS4500/5500

if (score >= 90.0)

System.out.print("A"); else if (score >= 80.0) System.out.print("B"); else if (score >= 70.0) System.out.print("C"); else if (score >= 60.0) System.out.print("D"); else System.out.print("F");

(a) Equivalent

if (score >= 90.0) System.out.print("A"); else if (score >= 80.0) System.out.print("B"); else if (score >= 70.0) System.out.print("C"); else if (score >= 60.0) System.out.print("D"); else System.out.print("F");

(b) This is better

The else clause matches the most recent if clause Example: ComputeAndInterpretBMI.java

slide-19
SLIDE 19

19

Problem: Body Mass Index

Body Mass Index (BMI) is a measure of health on

  • weight. It can be calculated by taking your weight in

kilograms and dividing by the square of your height in

  • meters. The interpretation of BMI for people 16 years
  • r older is as follows:

BMI Interpretation BMI < 18.5 Underweight 18.5 <= BMI < 25.0 Normal 25.0 <= BMI < 30.0 Overweight 30.0 <= BMI Obese

slide-20
SLIDE 20

If statement notes

  • UC. Colorado Springs

CS1150

  • The else clause matches the most recent if

clause

  • What’s the output?
slide-21
SLIDE 21

If statement notes

  • UC. Colorado Springs

CS1150

To force the else clause to match the first if clause, must add a pair of braces:

int i = 1, j = 2, k = 3; if (i > j) { if (i > k) System.out.println("A"); } else System.out.println("B");

slide-22
SLIDE 22

If statement summary

  • UC. Colorado Springs

CS1150

  • Block statements {} can be omitted if there is only
  • ne statement
  • Recommendation
  • Use curly braces: it helps make the code easier to modify and less error

prone

  • Avoid deeply nested if statements
  • An ”else" clause always belongs with the most recent

if clause

slide-23
SLIDE 23

Summary

  • UC. Colorado Springs

CS1150

  • Numeric literals
  • Data casting
  • Boolean data type
  • If statement