Selection/Conditional Statements
What are some decisions we make as human beings?
How do we get the computer to make a decision?
if and if-else statements
What is the format of an if statement?
int grade = 75;
final int PASSING_GRADE = 70;
if (grade >= PASSING_GRADE)
System.out.println("You passed!");
What is the format of an if-else statement?
int grade = 75;
final int PASSING_GRADE = 70;
if (grade >= PASSING_GRADE)
System.out.println("You passed!");
else
System.out.println("You did not pass:(");
What if we want to do more than one thing if a condition is true/not true?
int grade = 75;
final int PASSING_GRADE = 70;
if (grade >= PASSING_GRADE) {
System.out.println("You passed!");
System.out.println("Let's go out and celebrate!");
}
else {
System.out.println("You did not pass:(");
System.out.println("Please try again");
}
What happens when we try the in-class exercise?
How can we fix it?
How could we use "nested" if-else statements to figure out the price of a movie ticket?
Before 6:00 pm 6:00 pm or later
Under 12 $3.00 $5.50
12 and over $4.50 $7.00
Assume we have defined and given a value to:
a boolean variable named beforeSix
an int variable named age
Switch Statements
What is the format of a switch statement?
//Print the word for the value where value is 1, 2, or 3
int value = 2;
switch (value) {
case 1: System.out.println("one");
break;
case 2: System.out.println("two");
break;
case 3: System.out.println("three");
break;
default: System.out.println("Invalid value: " + value);
}
//Print the word for the gender
//when abbreviated as 'M', 'm', 'F', or 'f'
char gender = 'm';
switch (gender) {
case 'M':
case 'm': System.out.println("male");
break;
case 'F':
case 'f': System.out.println("female");
break;
default: System.out.println("Invalid gender: " + gender);
}
//Illustrate dropping through to the next case
//with "Twelve Days of Christmas"
//but only do 5 days, since I'm not about to
//type in all twelve days
int day = 4;
switch (day) {
case 5: System.out.println("Five golden rings");
case 4: System.out.println("Four calling birds");
case 3: System.out.println("Three French hens");
case 2: System.out.println("Two turtle doves");
case 1: System.out.println("And a partridge in a pear tree");
break;
default: System.out.println("Invalid day: " + day);
}
Conditional Operator(ADVANCED)
final int PASSING_SCORE = 60;
//Use conditional operator to compare score to passing score and
//print "Passed" or "Failed"
int score = 65;
System.out.println( score >= PASSING_SCORE ? "Passed" : "Failed");
//Use conditional operator to set grade to 'S' or 'U', based on score
char grade;
grade = (score >= 60) ? 'S' : 'U';
System.out.println("Grade: " + grade);
int x = 5, y = 10, z = 10;
Short Circuit Operators
What do mean when we say that && and || are "short-circuit" operators?
//Ilustrate "short-circuit operator" &&
int x = 6;
int y = 9;
int z = 7;
System.out.println( x == y && (y = 8) < z );
System.out.println("y = " + y);
//Illustrate "short-circuit operator" ||
x = 3;
System.out.println( x < y || (y = 5) < z);
System.out.println("y = " + y);
What is the "dangling-else problem"? Which if statement does the else belong with in the code below?
boolean isHot = true;
boolean isRaining = false;
if (isHot)
if (!isRaining)
System.out.println("Go swimming!!");
else
System.out.println("It's hot, but too rainy to swim");
How can we fix the code below?
isHot = false;
if (isHot)
if (isRaining)
System.out.println("Too rainy to swim");
else
System.out.println("It's cold today");