140 likes | 318 Views
Chapter 5. Java Methods. Conditionals Booleans Relational and Logical Operators Switch statements. Topics Covered. If-else statements Use logical expressions Else statement is optional Booleans Primitive data type – only true or false Relational operators >,<,>=,<=, ==, !=
E N D
Chapter 5 Java Methods
Conditionals Booleans Relational and Logical Operators Switch statements Topics Covered
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 == Ideas already covered in previous sections
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? Relational operators
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? Relational operators
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! Relational operators
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”);} Relational operators
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 Logical Operators
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 Short circuit evaluation
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 If-else-if
Given a situation where one of several outcomes depend on a value: IntAPscore; If(APscore = 1) {…} Else if (APscore = 2) {…} Else if (APscore = 3) {…} Else if (APscore = 4) {…} Else {…} Switch statement
If-else-if statements can be a little messy • Use switch statement instead: • IntAPscore; • Switch(APscore) • { case 1: • …. //do action • break; • case 2: • … // do action • break; } Switch statements
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 Switch statements
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 HW