Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5.  Conditionals  Booleans  Relational and Logical Operators  Switch statements.

Similar presentations


Presentation on theme: "Chapter 5.  Conditionals  Booleans  Relational and Logical Operators  Switch statements."— Presentation transcript:

1 Chapter 5

2  Conditionals  Booleans  Relational and Logical Operators  Switch statements

3  If-else statements  Use logical expressions  Else statement is optional  Booleans  Primitive data type – only true or false  Relational operators  >, =,<=, ==, !=  Most mistakes made confusing = and ==

4  Program that evaluates even or odd:  int x = 15;  if(x%2 == 1) {System.out.println(“odd”);}  else {System.out.println(“even”);}  --no need to evaluate with an expression since the only other possibility is even  What would be returned above?

5  Program that evaluates gender  String gender = “male”;  if(gender == “male”) {System.out.println(“guy”);}  Else {System.out.println(“lady”);}  What is displayed on your screen?

6  Try this:  String gender = “ma”;  Gender += “le”; //gender now is “male”;  if(gender == “male”) {System.out.println(“guy”);}  Else {System.out.println(“lady”);}  Anything change? Try it!

7  When using == and != for objects (like Strings), you compare their references, not their values, so it won’t always work  Instead of == and !=, objects typically have methods for comparison:  if(gender.equals(“male”)) {System.out.println(“guy”);}  Else {System.out.println(“lady”);}

8  We have already covered &&, ||, and !  Results of these operators have boolean data types  Match the following to it’s equivalent (2 pair):  1. !(p&& q)  2. !(p || q)  3. !p && !q  4. !p || !q

9  Logical operators are read left-right  If(rightIsClear() && frontIsClear() && hasBeepers())  If rightIsClear() is FALSE, the other operators ARE NOT evaluated – this is short circuit evaluation

10  We have used if-else statements which evaluate one of 2 possible conditions  Given multiple conditons:  If(condition1)  {…}  Else if (condition2) {…}  Else if (condition3) {…}  Else // optional given the situation

11  Given a situation where one of several outcomes depend on a value:  Int APscore;  If(APscore = 1) {…}  Else if (APscore = 2) {…}  Else if (APscore = 3) {…}  Else if (APscore = 4) {…}  Else {…}

12  If-else-if statements can be a little messy  Use switch statement instead:  Int APscore;  Switch(APscore)  { case 1:  …. //do action  break;  case 2:  … // do action  break; }

13  Practice  Create a short program using switch statements that does the following:  Create a scanner object that asks a user to input a number from 1-5 (if it is a different number, have the user re-enter the input)  Have a different output phrase given the number

14  Case study – rolling dice  Read carefully, as you will need to understand the game of craps to program  Exercise set - 1, 2, 3, 4, 5, 6, 9, 11, 13, 15, 19


Download ppt "Chapter 5.  Conditionals  Booleans  Relational and Logical Operators  Switch statements."

Similar presentations


Ads by Google