chapter 8 conditionals. learning java through alice © daly and wrigley objectives list relational...

15
Chapter 8 Conditionals

Upload: violet-hall

Post on 01-Jan-2016

234 views

Category:

Documents


7 download

TRANSCRIPT

Page 1: Chapter 8 Conditionals. Learning Java through Alice © Daly and Wrigley Objectives List relational operators. List logical operators. Use the hierarchy

Chapter 8

Conditionals

Page 2: Chapter 8 Conditionals. Learning Java through Alice © Daly and Wrigley Objectives List relational operators. List logical operators. Use the hierarchy

Learning Java through Alice © Daly and Wrigley

Objectives• List relational operators. • List logical operators. • Use the hierarchy of operators chart to properly construct

if/else statements. • Construct switch statements. • Use nested if statements.

2

Page 3: Chapter 8 Conditionals. Learning Java through Alice © Daly and Wrigley Objectives List relational operators. List logical operators. Use the hierarchy

Learning Java through Alice © Daly and Wrigley

Decisions

True and false values are also known as Boolean values, named after the 19th century English mathematician George Boole.

3

Page 4: Chapter 8 Conditionals. Learning Java through Alice © Daly and Wrigley Objectives List relational operators. List logical operators. Use the hierarchy

Learning Java through Alice © Daly and Wrigley

Relational OperatorsA Boolean test compares primitives, constants, and variables and or objects using the following relational operators:

Operator Meaning Example

== Equal to x == 3

! = Not equal to x != 3

< Less than x < 3

> Greater than x > 3

<= Less than or equal to x <= 3

>= Greater than or equal to x >= 3

Note: These relational operators must be typed exactly as above. You can’t type a space between the two equal signs and you can’t put =< to mean less than or equal.

4

Page 5: Chapter 8 Conditionals. Learning Java through Alice © Daly and Wrigley Objectives List relational operators. List logical operators. Use the hierarchy

Learning Java through Alice © Daly and Wrigley

Alice Example of an if statement :

5

Page 6: Chapter 8 Conditionals. Learning Java through Alice © Daly and Wrigley Objectives List relational operators. List logical operators. Use the hierarchy

Learning Java through Alice © Daly and Wrigley

Logical OperatorsLogical Operator Explanation:

 &&

Logical AND - Both conditions on both sides of the && must be true for the result to be TRUE.  The first condition will be tested and if it is false, the computer will not bother to test the second condition since there is no way for both conditions to be true if the first condition is false..

  ||

Logical OR - If either condition is true on both sides of the || then the result is true.  The first condition will be tested and if it is true, the computer will not bother to test the second condition since it only needs one condition to be true and it already found one to be true.

  & Boolean AND -Same as logical AND but it tests both sides of & no matter what.

  | Boolean OR - Same as logical OR but it tests both side of | no matter what.

  !Logical NOT - Negates the condition (i.e. - if result was false, it will become true and vice versa.

 ^Boolean logical exclusive OR - Results in true value if and only if one of its operands is true and the other is false.

6

Page 7: Chapter 8 Conditionals. Learning Java through Alice © Daly and Wrigley Objectives List relational operators. List logical operators. Use the hierarchy

Learning Java through Alice © Daly and Wrigley

If StatementsThe syntax contains the keyword if, followed by a condition(boolean test) in parenthesis, followed by either a singlestatement or a block statement to execute if the test istrue.   An optional else keyword provides the alternativestatement to execute if the test is false. 

if  (condition){    statements to do when conditional is true   }

else {   statements to do when conditional is false   }

7

Page 8: Chapter 8 Conditionals. Learning Java through Alice © Daly and Wrigley Objectives List relational operators. List logical operators. Use the hierarchy

Learning Java through Alice © Daly and Wrigley

If Statements Examples

if   (x   <   y) { System.out.println ( "x is smaller than y" ); } 

if    (x   <   y) { System.out.println ("x is smaller than y" );  }

else  { System.out.println ("x is not smaller than y" ); }

8

Page 9: Chapter 8 Conditionals. Learning Java through Alice © Daly and Wrigley Objectives List relational operators. List logical operators. Use the hierarchy

Learning Java through Alice © Daly and Wrigley

If Statements Examples Continued…if  ( guess  ==  correctAnswer )  {       

score = score + 10;  System.out.println ( "You are right. You get 10

points.");  } else  { 

score  = score -  5;  System.out.println ( "Sorry, that is wrong. You lose 5

points."); }

 

9

Page 10: Chapter 8 Conditionals. Learning Java through Alice © Daly and Wrigley Objectives List relational operators. List logical operators. Use the hierarchy

Learning Java through Alice © Daly and Wrigley

Else If

if  (x  <  0) { System.out.println ( "X is negative." );  }

else if (x == 0) { System.out.println ( "X is equal to 0." );  }

else { System.out.println ( "X is positive." );  }

10

Page 11: Chapter 8 Conditionals. Learning Java through Alice © Daly and Wrigley Objectives List relational operators. List logical operators. Use the hierarchy

Learning Java through Alice © Daly and Wrigley

Using Logical Operators• The following only prints if x is less than y and x

is less than z.  The && means if x is NOT less than y it won't even bother to test to see if x is less than z.

if ( x < y  &&  x < z)               { System.out.println ("x is less than both y and z" ); }

• The following calculates grossPay if either condition is true.   The || means that if weeklyHours is less 40, it won't bother to test to see if employeeType is 'P'.

if (weeklyHours < 40  ||  employeeType == 'P'  )       { grossPay =  weeklyHours * hourlyRate; }

11

Page 12: Chapter 8 Conditionals. Learning Java through Alice © Daly and Wrigley Objectives List relational operators. List logical operators. Use the hierarchy

Learning Java through Alice © Daly and Wrigley

Switch StatementsDone with if statements: Done with switch statements:

if (grade == 'A') {     System.out.println("Great!");  } else if (grade == 'B') {      System.out.println("Good!");  } else if (grade == 'C') {    System.out.println("Nice.");  } else {     System.out.println("Not Good.");}        

switch (grade) {  case 'A':     System.out.println("Great!");     break; case 'B':     System.out.println("Good!");     break;  case 'C':      System.out.println("Nice.");      break; default:       System.out.println("Not Good.");  }

12

Page 13: Chapter 8 Conditionals. Learning Java through Alice © Daly and Wrigley Objectives List relational operators. List logical operators. Use the hierarchy

Learning Java through Alice © Daly and Wrigley

Complex If Statements vs. Switch

Complex If Statements: Switch Statements:

if  (month == 2) {       days =28; } else if ((month == 4) || (month == 6)  ||  (month == 9)  || (month == 11)) {        days=30; } else {      days=31; }       

switch (month) {    case 2:             days = 28;              break;    case 4:  case 6:  case 9:  case 11:             days = 30;              break;    default:             days = 31;   }

13

Page 14: Chapter 8 Conditionals. Learning Java through Alice © Daly and Wrigley Objectives List relational operators. List logical operators. Use the hierarchy

Learning Java through Alice © Daly and Wrigley

Comparing Strings using if/else

14

Page 15: Chapter 8 Conditionals. Learning Java through Alice © Daly and Wrigley Objectives List relational operators. List logical operators. Use the hierarchy

Learning Java through Alice © Daly and WrigleyComparing Strings using the

Switch

15