Selection Statement
Perform action only wPerform action only when condition hen condition isis
truetrue
Perform Perform different specifdifferent specified action when condition isied action when condition is
false
false
Conditional operator (Conditional operator (?:?:))
NestedNested
if…elseif…else
selection structuresselection structures
if…else
There are times when you want to do something if a condition is true and do something There are times when you want to do something if a condition is true and do something
else if the condition is false. You can do this by using the else statement in addition to the else if the condition is false. You can do this by using the else statement in addition to the if statement, as in the following example:
if statement, as in the following example: if (answer == correctAnswer) {
if (answer == correctAnswer) { score += 10;
score += 10;
System.out.println("That's right. You get 10 points."); } System.out.println("That's right. You get 10 points."); } else { score -= 5;
else { score -= 5;
System.out.println("Sorry, that's wrong. You lose 5 points."); System.out.println("Sorry, that's wrong. You lose 5 points."); }}
The else statement does not have a condition listed alongside it, unlike the if statement. The else statement does not have a condition listed alongside it, unlike the if statement. Generally, the else statement is matched with the if statement that immediately comes Generally, the else statement is matched with the if statement that immediately comes before it in a Java program. You also can use else to chain several i
before it in a Java program. You also can use else to chain several if staf statements together, astements together, as in the following example:
in the following example: if (grade == "A")
if (grade == "A")
System.out.println("You got an A. Great job!"); System.out.println("You got an A. Great job!"); else if (grade == "B")
else if (grade == "B")
System.out.println("You got a B. Good work!"); System.out.println("You got a B. Good work!"); else if (grade == "C")
else if (grade == "C")
System.out.println("You got a C. You'll never get into a good college!"); System.out.println("You got a C. You'll never get into a good college!"); else
else
System.out.pri
Fig 4.4
Fig 4.4 if…elseif…else double-selections statement activity diagram.double-selections statement activity diagram.
[grade >= 60] [grade >= 60] [grade < 60]
[grade < 60]
print “Failed”
switch Statementsswitch Statements
Another way to do this is to use the swi
Another way to do this is to use the switch statement. tch statement. YYou can use it ou can use it in a Javain a Java
program to test for a variety of different conditions and respond accordingly. In the program to test for a variety of different conditions and respond accordingly. In the following example, the grade example has been rewritten with the switch statement following example, the grade example has been rewritten with the switch statement to handle a complicated range of choices:
to handle a complicated range of choices: switch (grade) {
switch (grade) { case ‘A’:
case ‘A’:
System.out.println("You got an A. Great job!"); System.out.println("You got an A. Great job!");
break; break; case ‘B’: case ‘B’:
System.out.println("You got a B. Good work!"); System.out.println("You got a B. Good work!"); break;
break; case ‘C’: case ‘C’:
System.out.printl
System.out.println("Yn("You got a C. ou got a C. YYou'll ou'll never get into a goodnever get into a good college!"); college!"); break; break; default: default: System.out.print
System.out.println("Yln("You got an F. You got an F. You'll do well ou'll do well in Congress!in Congress!");"); }}
The Conditional OperatorThe Conditional Operator
The most complicated conditional statement is one that you might not find reasons to use in your programs, The most complicated conditional statement is one that you might not find reasons to use in your programs,
the ternary operator. If you find
the ternary operator. If you find it too confusing to implemit too confusing to implement in your own prent in your own programs, take heartograms, take heart: Y: You canou can use other conditionals to accomplish the same thing.
use other conditionals to accomplish the same thing. Y
You can use the ternaou can use the ternary operator when you want to assign a value or display a value based on a conditionalry operator when you want to assign a value or display a value based on a conditional test. For example, in a video game, you might need to set the numberOfEnemies variable based on whether test. For example, in a video game, you might need to set the numberOfEnemies variable based on whether the
the variable is variable is greater than greater than 5. One 5. One way way to to do this do this is with is with an if-else an if-else statement:statement: if (skillLevel > 5) if (skillLevel > 5) numberOfEnemies = 10; numberOfEnemies = 10; else else numberOfEnemies = 5; numberOfEnemies = 5;
A shorter way to do this is to use the ternary operator, which is ?. A
A shorter way to do this is to use the ternary operator, which is ?. A ternary operator has the follternary operator has the following parts:owing parts: The condition to test, surrounded by parentheses, as in (skillLevel > 5)
The condition to test, surrounded by parentheses, as in (skillLevel > 5)
A question mark (?) A question mark (?)
The value to use if the condition is trueThe value to use if the condition is true
A colon (:) A colon (:)
The value to use if the condition is falseThe value to use if the condition is false
T
To use the ternary operator to set the value of numbero use the ternary operator to set the value of numberOfEnemies based on skillOfEnemies based on skillLevel, you could use theLevel, you could use the following statement:
following statement:
numberOfEnemies = ( skillLevel > 5) ? 10 : 5; numberOfEnemies = ( skillLevel > 5) ? 10 : 5;
Exercises 1 (Nested if Statement)Exercises 1 (Nested if Statement)
Create a module nestedIf.java.Create a module nestedIf.java.