if statements Chapter 3

Download Report

Transcript if statements Chapter 3

if statements
Chapter 3
Selection
• Want to be able to do a statement
sometimes, but not others
• if it is raining, wear a raincoat.
• Start first with how to make a condition
if statements
if (booleanexpression)
java statement;
we don’t know about this
any Java statement you
know about
Boolean Expressions
• Evaluate to be true or false
• boolean variables
– boolean isVisible = false;
• relational expressions (compares values)
• logical expressions (compares
expressions with and, or, not)
Comparing Primitives
• <, >, >=, <=
• !=
• ==
•
•
•
•
•
•
3 == 4 false
4 == 4 true
3 < 4 true
3 <= 4 true
3 >= 4 false
3 > 4 false
• These do not work for object types
Evaluating Boolean Expressions
A true, B false, C error
int x=3; int y = 4; int z=5;
x<y
true
x<y<z
error: < cannot be applied to boolean,int
x=y
error: incompatible types - found int; expected boolean
y == 4
true
z >= x
true
x != 3
false
(x + 4) < (y - 1)
7 < 3; false
Logical Operators
• and (&&, single & very different)
– both values must be true for the expression to be
true
– if it is cold and rainy, wear your winter raincoat
(both must be true)
• or (|| - on keyboard, called pipe symbol)
– either value can be true
– if it is cold or rainy, wear a coat (if either or both is
true, do)
• not (!)
– changes the truth value of the expression
– if it is not cold, do not wear a winter coat
Logical Operators
A true, B false, C error
int x=3; int y=10;
(x < y) && (y < 20)
(x == 3) || (y == 3)
x < y; 3 < 10; true
y < 20; 10 < 20; true
true && true is true
x == 3 true.
short circuit evaluation
(y==3 false
true || false is true)
More logical operators
A true, B false, C error
int x=3; int y=10;
!(y=10)
(x != 3) || (y != 3)
trick question
error
!(y==10)
y == 10 true
!true false
x != 3 false
Keep going. y != 3 true
false || true is true
Yet more logical operators
A true, B false, C error
int x=3; int y=10;
!((x+1 < 4) ||
(y <= 10))
!((x+1 < 4) &&
(y <= 10))
x+1 = 4
4 < 4 false.keep going
y <= 10 true
false || true true
! true is false
4 < 4 false. DONE with
&&. Do not look at y
<=10.
!false true
Strings and Classes
• == tests if objects are equal (point to the same
thing), NOT if they have the same content. May
return false when true should be returned
• use equals
• no corresponding <, lessthan,…
• use compareTo
• Difference between primitives (holds a value)
and Classes (holds a pointer) is important.
if statements
• if statement form:
– if (boolean expression)
java statement;
you know both
parts now
if (x < y)
System.out.println(“x < y”);
if statements cautions
• MUST have ( )s around boolean
expression
• no syntax error for non-boolean like
expressions
• only ONE statement in an if statement
• no ';' after if condition
• Make sure you account for values that are
equal
• use relational operators only with
primitives
• use equals, compareTo with String
Do on board
• Write an if statement that prints "Before
Middle" if int x is less than int middle
• Write an if statement that assigns 0 to
average (double) if numberCounted (an
int) is 0
if-else
• If you want to do one thing if a condition is true and
something else if not, use if-else.
– form: if (condition)
Java statement
else
Java statement
if (x < y)
System.out.println(x + " is less than the other number");
else
System.out.println(y + " is less than the other number");
What's wrong with this logic?
> one statement in an if
If you want to have more than one statement inside
an if or an else, use {}s:
if (x < y)
{
System.out.println(x + " is less than the other number”);
x = 0;
}
else
{
System.out.println(y + " is less than the other number”);
y = 0;
}
If-else cautions
• either if clause or else clause or both may
have {}s.
• After statements inside if and else clause
are executed, control passes back to next
sequential statement
• no ';' after else
• Make sure you account for values that are
equal
Board Work
public class MyTwoInts
{ private int x, y;
// write methods here
}
Write the method printSmaller that prints the
smaller of x and y.
(print either if they are equal).
Write the method getSmaller that returns the
smaller of x and y (return either if they are
equal).
Watch Out
if (3 < 4)
x = 3;
else
System.out.println(“3 < 4 is false”);
x = 7;
System.out.println("the value of x is " + x);
Prints what?
A. the value of x is 3
B. the value of x is 0
C. 3 < 4 is false
D. the value of x is 7
E. none of the above
Embedded ifs
• If statements and if-else statements may
be embedded (if within if). simply evaluate
them as the Java code is executed:
• if-else example is most common. sets up
a table of conditions
Embedded if-else for table
if (ave >= 90)
grade = 'A';
else if ((ave < 90) && (ave >= 80))
// note: need ()s around entire condition
grade = 'B';
else if ((ave < 80) && (ave >=70))
grade = 'C';
else if ((ave < 70) && (ave >=60))
grade = 'D';
else if ((ave < 70) && (ave < 60))
grade = 'F';
Tracing through the embedded if
Fixing the embedded if
if (ave >= 90)
grade = 'A';
else if (ave >= 80)
// We know (ave < 90) or we wouldn't be here
grade = 'B';
else if (ave >=70) // we know ave < 80
grade = 'C';
else if (ave >=60)
grade = 'D';
else
// if ((ave < 70) && (ave < 60))
grade = 'F';
Cautions for embedded ifs
• Don't use redundant comparisons
• Make sure you check for values that are
equal
• Account for out of range values