Selection in Java

Explains how to use selection (if and switch statements) in a Java program.

Prerequisites

Objectives

Relational Operators

The relational operators are used in a program to compare two values. There are six relational operators in Java.

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.

Boolean Expressions

if Statements

if … else Statements

if … else if … else Statements

Nested if Statements

switch Statements

Ternary Operator

Operator Precedence

The precedence of operators is as follows, with the bottom of the list having the lowest precedence, and the top the highest: