Java Control Flow Statements
Java Control statements control the order of execution in a java program, based on data values and conditional logic. There are three main
categories of control flow statements;
· Selection statements: if, if-else and switch.
· Loop statements: while, do-while and for.
· Transfer statements: break, continue, return, try-catch-finally and assert.
We use control statements when we want to change the default sequential order of execution
Selection Statements
The If Statement
The if statement executes a block of code only if the specified expression is true. If the value is false,
then the if block is skipped and execution continues with the rest of the program. You can either have a single statement or a block
of code within an if statement. Note that the conditional expression must be a Boolean expression.
The simple if statement has the following syntax:
if (<conditional expression>)
<statement action>
Below is an example that demonstrates conditional execution based on if statement condition.
public class IfStatementDemo {
public static void main(String[] args) {
int a = 10, b = 20;
if (a > b)
System.out.println("a > b");
if (a < b)
System.out.println("b > a");
}
} |
Output
b > a
Download
IfStatementDemo.java
The If-else Statement
The if/else statement is an extension of the if statement. If the statements in the if statement fails, the statements in the else block
are executed. You can either have a single statement or a block of code within if-else blocks. Note that the conditional expression
must be a Boolean expression.
The if-else statement has the following syntax:
if (<conditional expression>)
<statement action>
else
<statement action>
Below is an example that demonstrates conditional execution based on if else statement condition.
public class IfElseStatementDemo {
public static void main(String[] args) {
int a = 10, b = 20;
if (a > b) {
System.out.println("a > b");
} else {
System.out.println("b > a");
}
}
} |
Output
b > a
Download
IfElseStatementDemo.java
Switch Case Statement
The switch case statement,
also called a case statement is a multi-way branch
with several choices. A switch is easier to
implement than a series of if/else statements. The
switch statement begins with a keyword, followed by
an expression that equates to a no long integral
value. Following the controlling expression is a
code block that contains zero or more labeled cases.
Each label must equate to an integer constant and
each must be unique. When the switch statement
executes, it compares the value of the controlling
expression to the values of each case label. The
program will select the value of the case label that
equals the value of the controlling expression and
branch down that path to the end of the code block.
If none of the case label values match, then none of
the codes within the switch statement code block
will be executed. Java includes a default label to
use in cases where there are no matches. We can have
a nested switch within a case block of an outer
switch.
Its general form is as follows:
switch (<non-long integral expression>)
{
case label1: <statement1>
case label2: <statement2>
...
case labeln: <statementn>
default: <statement>
} // end switch
When executing a switch
statement, the program falls through to the next
case. Therefore, if you want to exit in the middle
of the switch statement code block, you must insert
a break statement, which causes the program to
continue executing after the current code block.
Below is a java example that demonstrates conditional execution based on nested if else statement condition to find the greatest of 3 numbers.
public class SwitchCaseStatementDemo {
public static void main(String[] args) {
int a = 10, b = 20, c = 30;
int status = -1;
if (a > b && a > c) {
status = 1;
} else if (b > c) {
status = 2;
} else {
status = 3;
}
switch (status) {
case 1:
System.out.println("a is the greatest");
break;
case 2:
System.out.println("b is the greatest");
break;
case 3:
System.out.println("c is the greatest");
break;
default:
System.out.println("Cannot be determined");
}
}
} |
Output
c is the greatest
Download
SwitchCaseStatementDemo.java
|