Control Statements in Java
Control Statements in Java is one of the fundamentals required for Java Programming. It
allows the smooth flow of a program. Following pointers will be covered in this article:
Simple if statement
if-else statement
Nested if statement
Switch statement
Looping statements
While
Do-while
For
For-Each
Branching statements
Break
Continue
Control Statements can be divided into three categories, namely
Selection statements
Iteration statements
Jump statements
Simple if statement
The if statement determines whether a code should be executed based on the specified
condition.
Syntax:
if (condition) {
Statement 1; //executed if condition is true
}
Statement 2; //executed irrespective of the condition
Example:
public class Main {
public static void main(String[] args) {
int x = 20;
Int y=18;
if (x > y) {
System.out.println("Good day.");
}
}
}
Output:
Good day.
If..else statement
In this statement, if the condition specified is true, the if block is executed. Otherwise, the
else block is executed.
Syntax:
if (condition1) {
Statement 1; //executed if condition is true
else {
Statement 2; //executed if condition is false
}
}
Example:
public class Main {
public static void main(String[] args) {
int time = 20;
if (time < 18) {
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
}
}
}
Output:
Good evening.
Nested if statement
An if present inside an if block is known as a nested if block. It is similar to an if..else
statement, except they are defined inside another if..else statement.
Syntax:
if (condition1) {
Statement 1; //executed if first condition is true
if (condition2) {
Statement 2; //executed if second condition is true
}
else {
Statement 3; //executed if second condition is false
}
}
Example:
public class Main {
public static void main(String[] args) {
int time = 22;
if (time < 10) {
System.out.println("Good morning.");
} else if (time < 20) {
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
}
}
}
Output:
Good evening.
While
Known as the most common loop, the while loop evaluates a certain condition. If the
condition is true, the code is executed. This process is continued until the specified
condition turns out to be false.
The condition to be specified in the while loop must be a Boolean expression. An error
will be generated if the type used is int or a string.
Syntax:
while (condition)
{
statementOne;
}
Example:
public class Main {
public static void main(String[] args) {
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
}
}
Output:
0
1
2
3
4
Do..while
The do-while loop is similar to the while loop, the only difference being that the condition
in the do-while loop is evaluated after the execution of the loop body. This guarantees that
the loop is executed at least once.
Syntax do{
: //code to be executed
}while(condition);
Example:
public class Main {
public static void main(String[] args) {
int i = 0;
do {
System.out.println(i);
i++;
}
while (i < 6);
}
}
Output:
0
1
2
3
4
5
For
The for loop in java is used to iterate and evaluate a code multiple times. When the number
of iterations is known by the user, it is recommended to use the for loop.
Syntax:
for (initialization; condition; increment/decrement)
{
statement;
}
Example:
public class Main {
public static void main(String[] args) {
for (int i = 0; i <= 10; i = i + 2) {
System.out.println(i);
}
}
}
Output:
0
2
4
6
8
10
For-Each
The traversal of elements in an array can be done by the for-each loop. The elements
present in the array are returned one by one. It must be noted that the user does not
have to increment the value in the for-each loop.
Syntax:
for (type var : array)
{
statements using var;
}
Example:
public class Main {
public static void main(String[] args) {
String[] sub = {"Oops", "DPD", "DS", "CO"};
for (String i : cars) {
System.out.println(i);
}
}
}
Output:
Oops
DPD
DS
CO
Switch Statement
The switch statement is a multi-way branch statement. It provides an easy way to
dispatch execution to different parts of code based on the value of the expression.
Basically, the expression can be byte, short, char, and int primitive data types.
Syntax:
/ switch statement
switch(expression)
{
// case statements
// values must be of same type of expression
case value1 :
// Statements
break; // break is optional
case value2 :
// Statements
break; // break is optional
// We can have any number of case statements
// below is default statement, used when none of the cases is true.
// No break is needed in the default case.
default :
// Statements
}
Example:
public class Main {
public static void main(String[] args) {
int day = 4;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
}
}
}
Output:
Thursday
Simple if statement:
The if statement determines whether a code should be executed based on the specified
condition.
Syntax:
if (condition) {
Statement 1; //executed if condition is true
}
Statement 2; //executed irrespective of the condition