File 3 (Java Control Statements)
File 3 (Java Control Statements)
Java compiler executes the code from top to bottom. The statements in the code are executed
according to the order in which they appear. However, 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
o if statements
o switch statement
2. Loop statements
o do while loop
o while loop
o for loop
o for-each loop
3. Jump statements
o break statement
o continue statement
Decision-Making statements:
As the name suggests, decision-making statements decide which statement to execute and
when. 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.
a) Simple if statement
b) if-else statement
c) if-else-if ladder
d) Nested if-statement
Let's understand the if-statements one by one.
a) 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.
1. Syntax: 1. Example:
2. if(condition) { 2. public class Student {
3. statement 1; true 3. public static void main(String[] args) {
4. int x = 10;
}
5. int y = 12;
6. if(x+y > 20) {
7. System.out.println("x + y is greater than 20");
8. }}}
b) 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: Example:
if(condition) { public class Student {
statement 1; // when condition is true public static void main(String[] args) {
} int x = 10;
else{ int y = 12;
statement 2; // when condition is false if(x+y < 10) {
} System.out.println("x + y is less than 10");
} else {
System.out.println("x + y is greater than 20");
}}}
c) 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: Example:
if(condition 1) { public class Student {
statement 1; public static void main(String[] args) {
} String city = "Dhaka";
else if(condition 2) { if(city == " Khulna ") {
statement 2; System.out.println("city is Khulna ");
} }else if (city == "Barisal") {
else { System.out.println("city is Barisal ");
statement 2; }else if(city == "Jhenaidah") {
} System.out.println("city is Jhenaidah ");
}else {
System.out.println(city);
}}}
d) 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.
if(condition 1) { public class Student {
statement 1; public static void main(String[] args) {
if(condition 2) { String address = "Delhi, India";
statement 2;
} if(address.endsWith("India")) {
else{ if(address.contains("Meerut")) {
statement 2; System.out.println("Your city is Meerut");
} }else if(address.contains("Noida")) {
} System.out.println("Your city is Noida");
}else {
System.out.println(address.split(",")[0]);
}
}else {
System.out.println("You are not living in India");
}
}
}
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. The switch statement is easier to use instead of if-else-if statements. It also
enhances the readability of the program.
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.
switch (expression){ public class Student implements Cloneable {
case value1: public static void main(String[] args) {
statement1; int num = 2;
break; switch (num){
. case 0:
. System.out.println("number is 0");
. break;
case valueN: case 1:
statementN; System.out.println("number is 1");
break; break;
default: default:
default statement; System.out.println(num);
} }
}
}
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. The switch permits only int,
string, and Enum type variables to be used.
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.
for loop
while loop
do-while loop
Let's understand the loop statements one by one.
public class Calculation {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 0;
System.out.println("Printing the list of first 10 even numbers \
n");
while(i<=10) {
System.out.println(i);
i = i + 2;
} } }
public class Calculation {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 0;
System.out.println("Printing the list of first 10 even numbers \
n");
do {
System.out.println(i);
i = i + 2;
}while(i<=10);
}
}
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.
public static void main(String[] args) { public static void main(String[] args) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
for(int i = 0; i<= 10; i++) { a:
System.out.println(i); for(int i = 0; i<= 10; i++) {
if(i==6) { b:
break; for(int j = 0; j<=15;j++) {
} c:
} for (int k = 0; k<=20; k++) {
} System.out.println(k);
} if(k==5) {
break a;
} } } } } }
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i = 0; i<= 2; i++) {
for (int j = i; j<=5; j++) {
if(j == 4) {
continue;
}
System.out.println(j);
} } } }