Core JAVAWrite Once, Run Anywhere |
There are two types of decision making statements in Java. They are:
The syntax of enhanced if statement is:
Example
Output
if...else Statement
An if statement can be followed by an optional else statement, which executes when the Boolean expression is false.
Syntax
The syntax of a if...else is:
Example
Output
if...else if...else Statement
An if statement can be followed by an optional else if...else statement, which is very usefull to test various conditions using single if...else if statement.
When using if, else if , else statements there are few points to keep in mind.
Syntax
The syntax of a if...else if is:
Example
Output
Nested if...else Statement
It is always legal to nest if-else statements, which means you can use one if or else if statement inside another if or else if statement.
Syntax
The syntax of a nested if...else if is:
Example
Output
switch Statement
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.
Syntax
The syntax of a switch statement if is:
The following rules apply to a switch statement:
Example