if statements chapter 3. selection want to be able to do a statement sometimes, but not others if it...
Post on 22-Dec-2015
215 views
TRANSCRIPT
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;
any Java statement you know about
we don’t know about this
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 Values• <, >, >=, <=• !=• ==
• 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 ExpressionsA true, B false, C error
int x=3; int y = 4; int z=5;int x=3; int y = 4; int z=5;
x < yx < y
x < y < zx < y < z
x = yx = y
y == 4y == 4
z >= xz >= x
x != 3x != 3
(x + 4) < (y - 1)(x + 4) < (y - 1)
truetrue
error: error: < cannot be applied to boolean,int< cannot be applied to boolean,int
error: incompatible types - found int; expected boolean error: incompatible types - found int; expected boolean
truetrue
truetrue
falsefalse
7 < 3; false7 < 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 = 44 < 4 false.keep goingy <= 10 truefalse || true true! true is false4 < 4 false. DONE with
&&. Do not look at y <=10.
!false true
if statements• if statement form:
– if (boolean expression)
java statement;
if (x < y)
System.out.println(“x < y”);
you know both parts
now
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
Write 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 ifIf 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
Computer Workpublic class MyTwoInts{ public static void main(String[] args) { java.util.Scanner kbd = new java.util.Scanner (System.in); System.out.println(“Enter two numbers: “); int x = kbd.nextInt( ); int y = kbd.nextInt( ); // Write CODE IN HERE TO PRINT THE 2 // numbers IN ORDER // THE SMALLEST FIRST
}
Watch Outif (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 3B. the value of x is 0C. 3 < 4 is falseD. the value of x is 7E. 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 tableif (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 ifif (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
• Embedded ifs can be implemented using switch statements. We will not go over these or test you on them. But switch statements are in the book.
1. Write a program to read a day (1 for Monday, 2 for Tuesday, etc. to 5 for Friday), then print “yipee! Today is CPSC150.” for days 1, 3 and 5; or “Only one more day until CPSC150!” for days 2 and 4.
2. Write a program to read an integer, and print “yes” if it is even and “no” otherwise.
3. Write a program that prints a message about academic status according to this table. GPA and attempted hours will be read from the keyboard.
Credit Hours Attempted *
Minimum Good Standing
Academic Probation
Academic Suspension
1-30 2.00 1.99 - 1.61 1.60 Or Less
31-60 2.00 1.99 - 1.70 1.69 Or Less
61-75 2.00 1.99 - 1.80 1.79 Or Less
76-90 2.00 1.99 - 1.90 1.89 Or Less
91 Or More 2.00 1.99 - 1.98 1.97 Or Less