CSCI 144 - Introduction to Computer Science Instructor: John - - PowerPoint PPT Presentation

csci 144 introduction to
SMART_READER_LITE
LIVE PREVIEW

CSCI 144 - Introduction to Computer Science Instructor: John - - PowerPoint PPT Presentation

1 CSCI 144 - Introduction to Computer Science Instructor: John Goettsche Computer Science Department Pacific Lutheran University Spring 2018 More Boolean Expressions and If Statements Flags Flags boolean onGreen = false; Monitor some


slide-1
SLIDE 1

CSCI 144 - Introduction to Computer Science

Instructor: John Goettsche Computer Science Department Pacific Lutheran University Spring 2018

1

slide-2
SLIDE 2

More Boolean Expressions and If Statements

slide-3
SLIDE 3

Flags

slide-4
SLIDE 4

Flags

if (distanceToHole < 30)

  • nGreen = true;

boolean onGreen = false; if (onGreen) System.out.println("Get out your putter!");

Boolean variable Monitor some condition Can be set And later tested

slide-5
SLIDE 5

Comparing Characters

slide-6
SLIDE 6

Comparing Characters

< > <= >= == != Characters can be compared

using relational operators

0000000001000011 char ch = ‘C’;

Unicode is stored as a 16 bit number

slide-7
SLIDE 7

Comparing Characters

char c = ′A′; if(c < ′Z′) System.out.println("A is less than Z"); < > <= >= == != Characters can be compared

using relational operators

0000000001000011 char ch = ‘C’;

Unicode is stored as a 16 bit number ‘0’ … ‘9’ ‘?’ ‘A’ … ‘Z’ ‘a’ … ‘z’ Characters are ordinal -- they have an order

slide-8
SLIDE 8

Character Unicode 'A' 65 'B' 66 'C' 67 'D' 68 ... ... 'Z' 90 Character Unicode 'a' 97 'b' 98 'c' 99 'd' 100 ... ... 'z' 122

slide-9
SLIDE 9

Comparing Strings

slide-10
SLIDE 10

“Sally”

name1 String name1, name2;

“Sally”

name2 if (name1 == name2) System.out.println(“The names are equal”);

Can’t compare strings with ==

slide-11
SLIDE 11

“Sally”

name1 String name1, name2;

“Sally”

name2 if (name1.equals(name2)) System.out.println(“The names are equal”);

Must use equals method

slide-12
SLIDE 12

“Sally”

name1 String name1, name2;

“SALLY”

name2 if (name1.equalsIgnoreCase(name2)) System.out.println(“The names are equal”);

slide-13
SLIDE 13

Logical Operators

slide-14
SLIDE 14

Logical Operators

Operator Meaning Effect

&& AND Both expressions must be true for the overall expression to be true. || OR One or both expressions must be true for the overall expression to be true. ! NOT The ! operator reverses the truth of a boolean expression.

slide-15
SLIDE 15

AND &&

Expression 1 Expression 2 Expression1 && Expression2 true false false true false false true true

slide-16
SLIDE 16

OR ||

Expression 1 Expression 2 Expression1 || Expression2 true false false true false false true true

slide-17
SLIDE 17

NOT !

Expression 1 !Expression1 true false

slide-18
SLIDE 18

Precedence

slide-19
SLIDE 19

Order of Precedence Operators Description

1 (unary negation) ! Unary negation, logical NOT 2 * / % Multiplication, Division, Modulus 3 + - Addition, Subtraction 4 < > <= >= Less-than, Greater-than, Less-than or equal to, Greater-than or equal to 5 == != Is equal to, Is not equal to 6 && Logical AND 7 || Logical OR

slide-20
SLIDE 20

Practice

slide-21
SLIDE 21

Logical Operators

Evaluate the following expressions given these values for Boolean variables x, y, and z:

boolean x = true, y = false, z = true; Expression Result x && y || x && z __________ (x || !y) && (!x || z) __________ x || y && z __________ !(x || y) && z __________

slide-22
SLIDE 22

Boolean Expressions

Evaluate the following expressions given these variables:

int count = 0, limit = 10, x = 5, y = 7; char midtermGrade = ‘A’; Expression Result count == 0 && limit < 20 __________ limit > 20 || count < 5 __________ !(midtermGrade != ‘C’) __________ (count < 10) || (x < y) __________

slide-23
SLIDE 23

Boolean Expressions (cont.)

Evaluate the following expressions given these variables:

int count = 0, limit = 10, x = 5, y = 7; char midtermGrade = ‘A’; Expression Result !(((count < 10) || (x < y)) && (count >= 0)) __________ ((limit/count) > 7) || (limit < 20) __________ ((midtermGrade == ‘A’) || ((limit/count) < 20) __________ (limit < 0) && ((limit/count) > 7) __________

slide-24
SLIDE 24

Write an if statement to calculate tax (totalSales times TAXRATE) if the user input forProfit is ‘Y’ or ‘y’.

if (forProfit == ‘Y’ || forProfit == ‘y’) { tax = totalsales * TAXRATE; }

slide-25
SLIDE 25

Write an if statement to calculate tax (totalSales times TAXRATE) if the user input response is “yes” (be sure to ignore case).

if (response.equalsIgnoreCase(“yes”)) { tax = totalsales * TAXRATE; }

slide-26
SLIDE 26

Write an JAVA program, scan user’s input as temperature and mood, write if statement to print “Play golf!” if the temperature is between 65 and 80, and mood is “happy”. For all other cases, print ‘Programming at home’. (hint: consider ‘flags’)

slide-27
SLIDE 27

Reference: Tables and flowcharts adapted from Starting Out with Java by T. Gaddis Chapter 3 Slides.