Logical operators, String comparison

advertisement
Lec 6
Logical Operators
String comparison
Review – if statement syntax
• OR
Logical expressions
• <boolean expression> is logical expression
score == 100
x+y>10
ans!='y'
(ans is a char)
isValid
(isValid is a boolean )
3
The 6 Comparison Operators
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)
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”;
}
6
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”;
}
7
The 3 Logical Operators
logical
operator
meaning
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
Examples
1. T/F
2. T/F
3. T/F
( 3 < 2 ) && (5 > 7)
!(2>3)
(25 = = 25 ) || ( 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
9
Oil Change
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";
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;
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"){ ...
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");
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)
How to avoid: total = $5.66666668
• EITHER: truncate (chop) to two decimal places
double d = 33.2482;
int i = (int)(d*100);
System.out.println(i / 100.0);
• OR format (round) to two decimal places for output
d = 33.2482;
System.out.printf("%1.2f", d);
System.out.println(); //need this since printf doesn't
//produce a newline character
Calculating the sum or average
sum num
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);
k
k < 10 Console
Download