Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lec 6 Logical Operators String comparison

Similar presentations


Presentation on theme: "Lec 6 Logical Operators String comparison"— Presentation transcript:

1 Lec 6 Logical Operators String comparison

2 Review – if statement syntax
OR

3 Logical expressions <boolean expression> is logical expression
score == 100 x+y>10 ans!='y' (ans is a char) isValid (isValid is a boolean )

4 The 6 Comparison Operators

5 Sometimes we need more complicated logic
example: age is between 18 and 55 months > 3 OR miles>5000 word is not "yes" (word is String)

6 Application: How to tell if n is in the correct range
...println("Enter a number from 1 to 10"); n = myInput.nextInt(); if (n>=1){ if (n<=10){ cout<<“OK, n is between 1 and 10!”; } else { cout<<“n is too big”; } } else{ cout<<“n is too small”;

7 Flowchart of previous slide

8 A better way using && ...println("Enter a number from 1 to 10");
n = myInput.nextInt(); if (n>=1 && n <=10){ cout<<“OK, n is between 1 and 10!”; } else{ cout<<“illegal value of n”;

9 The 3 Logical Operators logical operator meaning && and || or ! not
example in English && and a < 3 && b > 10 a is less than 3 and b is greater than 10 || or a < 3 || b > 10 a is less than 3 or b is greater than 10 ! not !(a < 3) it is not the case that a is less than 3

10 Examples 1. T/F ( 3 < 2 ) && (5 > 7) 2. T/F ! ( 2 > 3 )
4. the expression: ( number > 10 && number < 40 )  is TRUE, when a)      number is larger than 40 b)      number is smaller than 10 c)      number is between 10 and 40 d) Never

11 Oil Change

12 In groups, flowchart previous slide

13 Primitives vs Objects Recall the primitives:
int x = 3; double num = 3.42; char letter = 'Y'; boolean found = true; And objects (of Classes we've seen) Scanner myUserInput = new Scanner(System.in); String greeting = "hello";

14 Some differences between Primitives and Objects
Objects have methods: greeting.trim(); // remove blanks before/after greeting.toUpperCase(); // convert to CAPS myUserInput.nextInt(); // get an integer Primitives just store values: int x = 3; double num = 3.42; char letter = 'Y'; boolean found = true;

15 Comparing Strings Normally, Strings (and objects in general) should not use the comparison operators to check values: WRONG: if ( greeting == "hello" ){ .... Use .equals( ) instead RIGHT: if (greeting.equals( "hello") { ... For strings, this is even better: if ( greeting.equalsIgnoreCase("hello"){ ...

16 True or false? String s1 = "hello", s2 = "HeLLo", s3 = " hello ";
s1.equals("hello"); s2.equals("hello"); s3.equals("hello"); s2.equalsIgnoreCase("Hello"); s3.trim().equals("hello");

17 Lab 6 Department store Checkout.java Ask how many items to ring up
must be 1-10 use do loop to verify correct numbers logical operators to check for too high or too low While loop repeats for as many items get item cost add to total Ask if a bag is wanted, $ 0.05 extra if only 1-3 items Add 8.25% sales tax ( total = total*1.0825)

18 How to avoid: total = $ EITHER: truncate (chop) to two decimal places double d = ; int i = (int)(d*100); System.out.println(i / 100.0); OR format (round) to two decimal places for output d = ; System.out.printf("%1.2f\n", d); // means print d with two digits after decimal // %1.2f is format, \n means go to new line after

19 Calculating the sum or average
num k k < 10 Console int sum = 0; int k = 0, num; while ( k < 5) { num = scan.nextInt(); sum = sum + num; k = k + 1; } ...println("sum is " + sum); ...println("avg is " + sum/5.0);


Download ppt "Lec 6 Logical Operators String comparison"

Similar presentations


Ads by Google