Selection in Java
Explains how to use selection (if and
switch statements) in a Java program.
Prerequisites
- Understand Java primitive types and variables
- Understand Java arithmetic operators
Objectives
- Know the relational and Boolean logic operators
- Understand Boolean expressions and data types
- Use basic if statements
- Use if … else statements
- Use nested if statements
- Use switch statements
- Use ternary operators
- Know the precendence of operators
Relational Operators
The relational operators are used in a program to compare two values. There are six relational operators in Java.
<(less than)>(greater than)<=(less than or equal to)>=(greater than or equal to)==(equals)!=(not equal)
Boolean Operators
The Boolean logic operators are used to combine multiple conditions into a condition that evaluates to a single Boolean result (true or false). There are four Boolean operators available in Java.
!(not)^(exclusive or)&&(and)||(or)
Boolean Expressions
- A
booleanvariable can hold eithertrueorfalse - A Boolean expression also evaluates to
trueorfalse - Examples:
double bobHeight = 1.92; double sueHeight = 1.65; double tall = 1.8; boolean bobIsTall = (bobHeight >= tall); // bobIsTall will store true boolean sueIsTall = (sueHeight >= tall); // sueIsTall will store false
- Boolean expressions can be simple like the above examples, or complex
- Example:
boolean b = ((x < y && z != v) || (x + 4 == a));
if Statements
- An
ifstatement has two parts: the keywordifand a boolean expression - Syntax:
if (boolean_expression) { statements; } - If the Boolean expression is true, the statements in the body of the
ifstatement are executed - If the Boolean expression is false, the statements in the body of the
ifstatement are skipped - Examples:
if (sueHeight >= tall) { System.out.println("Sue is tall!"); } if (x == y) { System.out.println("x and y are the same"); }
if … else Statements
- In the above examples, if an
ifstatement is false, the statements in the body are skipped - In an
if … elsestatement, if the condition inifstatement is false, then the statements in the body of theelsestatement are executed - Syntax:
if (boolean_expression) { statements; } else { statements; } - Example:
if (sueHeight >= tall) { System.out.println("Sue is tall!"); } else { System.out.println("Sue is short."); }
if … else if … else Statements
- There can be many conditions in an
if … else if … elsestatement. - Syntax:
if (expression) { statements; } else if (expression) { statements; } else { statements; } - If the first
ifstatement is false, the nextelse ifstatement is checked - If no
else ifstatement is evaluated true, the statements in theelsestatement are executed. - Example:
if (sueHeight >= tall) { System.out.println("Sue is tall!"); } else if (sueHeight == 5.9) { System.out.println("Sue is of medium stature."); } else { System.out.println("Sue is short."); }
Nested if Statements
ifstatements can be nested within otherifstatements- Example:
if (x > y) { if (z > y) { System.out.println("x and z are greater than y"); } } else { System.out.println("y is not greater than x and z"); } - What does the computer do when executing the above code when x = 5, y = 3, and z = 1?
switch Statements
ifstatements make selection based on a condition being true or false, whileswitchstatements can have many conditions- Syntax:
switch (variable) { case 0: statements; break; case 1: statements; break; default: statements; break; } - If the value is found in one of the cases, the expression is evaluated
- The keyword
breakends further evaluation of the other cases - If the value is not found in any of the cases, the
defaultcase is evaluated - Example:
char grade = 'C'; double percent; switch (grade){ case 'A': percent = 90.0; break; case 'B': percent = 80.0; break; case 'C': percent = 70.0; break; case 'D': percent = 60.0; break; case 'F': percent = 50.0; break; default: percent = 0; break; }
Ternary Operator
- A ternary operator is a conditional statement like an
ifstatement with a different syntax - Syntax:
boolean_expression ? expression_if_true : expression_if_false; - If the boolean expression is true, expression_if_true is executed
- If the boolean expression is false, expression2_if_false is executed
- Example:
int z = -54; String isPositive = (z > 0) ? "z is positive" : "z is zero or negative";
Operator Precedence
The precedence of operators is as follows, with the bottom of the list having the lowest precedence, and the top the highest:
var++ var--(postfix)+ -(unary plus and minus),++var --var(prefix)(int) (double)(type cast)!(not)* / %(multiplication, division, modulus)+ -(binary addition and subtraction)< <= > >=(comparison)== !=(equality)^(exclusive or)&&(and)||(or)= += -= *= /= %=(assignment operators)