1
Unit-4
Java Control Statements:
Java provides statements that can be used to control the flow of Java code. Such statements are called control
flow statements. It is one of the fundamental features of Java, which provides a smooth flow of program.
Java provides three types of control flow statements.
1. Decision Making statements
a) if statements
b) switch statement
2. Loop statements
a) do while loop
b) while loop
c) for loop
3. Jump statements
a) break statement
b) continue statement
Decision-Making statements:
Decision-making statements evaluate the Boolean expression and control the program flow depending upon the
result of the condition provided. There are two types of decision-making statements in Java i.e. If statement and
switch statement.
1) If Statement:
In Java, the "if" statement is used to evaluate a condition. The control of the program is diverted depending
upon the specific condition. The condition of the If statement gives a Boolean value, either true or false. In Java,
there are four types of if-statements given below.
1. Simple if statement
2. if-else statement
3. if-else-if ladder
4. Nested if-statement
1) Simple if statement:
It is the most basic statement among all control flow statements in Java. It evaluates a Boolean expression and
enables the program to enter a block of code if the expression evaluates to true.
Syntax:
if(condition) {
statement 1;
}
Example:
public class Student {
public static void main(String[] args) {
int x = 10;
int y = 12;
if(x+y > 20) {
[Link]("x + y is greater than 20");
}
}
}
Output:
x + y is greater than 20
2) if-else statement
The if-else statement is an extension to the if-statement, which uses another block of code i.e., else block. The
else block is executed if the condition of the if-block is evaluated as false.
Syntax:
if(condition) {
2
statement 1;
}
else {
statement 2;
}
Example
public class Student {
public static void main(String[] args) {
int x = 10;
int y = 12;
if(x+y < 10) {
[Link]("x + y is less than 10");
}
else {
[Link]("x + y is greater than 10");
}
}
}
Output:
x + y is greater than 20
3) if-else-if ladder:
The if-else-if statement contains the if-statement followed by multiple else-if statements. In other words, we can
say that it is the chain of if-else statements that create a decision tree where the program may enter in the block
of code where the condition is true. We can also define an else statement at the end of the chain.
Syntax:
if(condition 1) {
statement 1;
}
else if(condition 2) {
statement 2;
}
else {
statement 3;
}
Example:
public class Student {
public static void main(String[] args) {
int x = 11;
int y = 2;
if(x+y < 10) {
[Link]("x + y is less than 10");
}
else if(x+y < 20) {
[Link]("x + y is less than 20");
}
else {
[Link]("x + y is greater than 20");
}
}
}
Output:
x + y is less than 20
4. Nested if-statement
In nested if-statements, the if statement can contain a if or if-else statement inside another if or else-if statement.
Syntax: if(condition 1) {
3
statement 1;
if(condition 2) {
statement 2;
}
else{
statement 2;
}
}
Example:
public class Student {
public static void main(String[] args) {
int x = 11;
int y = 99;
if(x+y < 100) {
if(x+y < 50) {
[Link]("x + y is less than 50");
}
else {
[Link]("x + y is greater than 50 and less than 100");
}
}
else {
[Link]("x + y is greater than 100");
}
}
} Output:
x + y is greater than 100
Switch Statement:
In Java, Switch statements are similar to if-else-if statements. The switch statement contains multiple blocks of
code called cases and a single case is executed based on the variable which is being switched.
Points to be noted about switch statement:
• The case variables can be int, short, byte, char, or enumeration. String type is also supported since
version 7 of Java
• Cases cannot be duplicate
• Default statement is executed when any of the case doesn't match the value of expression. It is optional.
• Break statement terminates the switch block when the condition is satisfied.
It is optional, if not used, next case is executed.
• While using switch statements, we must notice that the case expression will be of the same type as the
variable. However, it will also be a constant value.
Syntax:
switch (expression){
case value1:
statement1;
break;
.
.
.
case valueN:
statementN;
break;
default:
default statement;
}
4
Example:
public class Student {
public static void main(String[] args) {
int num = 2;
switch (num){
case 0:
[Link]("number is 0");
break;
case 1:
[Link]("number is 1");
break;
case 2:
[Link]("number is 2");
break;
case 3:
[Link]("number is 3");
break;
default:
[Link]("number is greater than 3");
}
}
}
Output:
number is 2
Loop Statements
In programming, sometimes we need to execute the block of code repeatedly while some condition evaluates to
true. However, loop statements are used to execute the set of instructions in a repeated order. The execution of
the set of instructions depends upon a particular condition.
In Java, we have three types of loops that execute similarly. However, there are differences in their syntax and
condition checking time.
1. for loop
2. while loop
3. do-while loop
Java for loop
It enables us to initialize the loop variable, check the condition, and increment/decrement in a single line of
code. We use the for loop only when we exactly know the number of times, we want to execute the block of
code.
Syntax:
for (initialization; condition; increment/decrement)
{
Statements;
}
Example:
public class Calculation {
public static void main(String[] args) {
int j,sum = 0;
for( j = 1; j<=10; j++) {
sum = sum + j;
}
[Link]("The sum of first 10 natural numbers is " + sum);
}
}
Output:
The sum of first 10 natural numbers is 55
5
Java while loop
The while loop is also used to iterate over the number of statements multiple times. However, if we don't know
the number of iterations in advance, it is recommended to use a while loop. It is also known as the entry-
controlled loop since the condition is checked at the start of the loop. If the condition is true, then the loop body
will be executed; otherwise, the statements after the loop will be executed.
Syntax:
while(condition) {
statements;
}
Example:
public class Calculation {
public static void main(String[] args) {
int i = 2;
[Link]("List of Even numbers less than 10");
while(i<20) {
[Link](i);
i = i + 2;
}
}
}
Output:
List of Even numbers less than 10
2
4
6
8
Java do-while loop:
The do-while loop checks the condition at the end of the loop after executing the loop statements. When the
number of iterations is not known and we have to execute the loop at least once, we can use do-while loop.
It is also known as the exit-controlled loop since the condition is not checked in advance.
Syntax:
do {
statements;
} while(condition);
Example:
public class Calculation {
public static void main(String[] args) {
int i = 0;
[Link]("List of Even numbers less than 10");
do {
[Link](i);
i = i + 2;
}while(i<10);
}
} Output:
List of Even numbers less than 10
0
2
4
6
8
Jump Statements:
Jump statements are used to transfer the control of the program to the specific statements. In other words, jump
statements transfer the execution control to the other part of the program. There are two types of jump
statements in Java, i.e., break and continue.
6
Java break statement:
As the name suggests, the break statement is used to break the current flow of the program and transfer the
control to the next statement outside a loop or switch statement. However, it breaks only the inner loop in the
case of the nested loop. The break statement cannot be used independently in the Java program, i.e., it can only
be written inside the loop or switch statement.
Example with for loop:
public class Example {
public static void main(String[] args) {
for(int i =1 ; i<= 10; i++) {
[Link](i);
if(i==4) {
break;
}
}
}
}
Output:
1
2
3
4
Java continue statement
Continue statement skips the specific part of the loop and jumps to the next iteration of the loop immediately.
Example:
public class Calculation {
public static void main(String[] args) {
for(int i =1 ; i<= 10; i++) {
if(i==4)
{
continue;
}
[Link](i);
}
}
}
Output:
1
2
3
5
6
7
8
9
10