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
boolean
variable can hold eithertrue
orfalse
- A Boolean expression also evaluates to
true
orfalse
- 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
if
statement has two parts: the keywordif
and a boolean expression - Syntax:
if (boolean_expression) { statements; }
- If the Boolean expression is true, the statements in the body of the
if
statement are executed - If the Boolean expression is false, the statements in the body of the
if
statement 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
if
statement is false, the statements in the body are skipped - In an
if … else
statement, if the condition inif
statement is false, then the statements in the body of theelse
statement 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 … else
statement. - Syntax:
if (expression) { statements; } else if (expression) { statements; } else { statements; }
- If the first
if
statement is false, the nextelse if
statement is checked - If no
else if
statement is evaluated true, the statements in theelse
statement 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
if
statements can be nested within otherif
statements- 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
if
statements make selection based on a condition being true or false, whileswitch
statements 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
break
ends further evaluation of the other cases - If the value is not found in any of the cases, the
default
case 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
if
statement 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)