In Java compiler will execute the program in sequential order from top to bottom. The code statements are executed according to the order in which they appear. Statements that can be used to control the flow of the java program. Such statements are called control statements.
Java Provides three types of control statements
Decision making statements are used to perform some decision based on the conditions, decision statements will be executed while execution of a java program. While execution of a program these statements will 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
Decision making statements are devided into 5 types:
if statement is the most simple decision-making statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not.
Syntax:
if(condition)
{
// Statements to execute if
// condition is true
}
Example Program:
public class Student {
public static void main(String[] args) {
int x = 30;
if(x > 20) {
System.out.println("x value is greater than 20");
}
}
}
if the condition is true then the code in if block is executed otherwise the else block is executed.
Let's see the execution flow of the if-else statement in a flow diagram:
Syntax:
if (condition) {
// If block executed when the condition is true
}
else {
// Else block executed when the condition is false
}
Example Program:
public class Voter {
public static void main(String[] args) {
int x = 17;
if(x >= 18) {
System.out.println("x is eligible for voting");
} else {
System.out.println("x is not eligible for voting");
}
}
}
Java allows us to nest if statements within if statements. Nested-if statements mean an if or if-else statement inside another if or else-if statement. It is similar to an if-else statement but they are defined inside another if-else statement.
Let's see the execution flow of the nested-if-else statement in a flow diagram:
As you can see the execution of nested-if-else, the execution block is determined by the series of nested conditions. If the condition of the first if is true, the inner if or else block is executed and so on, otherwise the outermost else block is executed.
Syntax:
if (condition1) {
// Executed when the condition1 is true
if (condition2) {
// Executed when the condition2 is true
}
else{
// Executed when the condition2 is false
}
}
else {
// Executed when the condition1 is false
}
Here, we have specified another if and else block inside the first if block. In the syntax, you can see the series of the blocks executed according to the evaluation of condition1 and condition2. In this, we can nest the control flow statements in Java to evaluate multiple related conditions.
Example Program:
class NestedIfDemo {
public static void main(String args[])
{
int age = 20;
String gender = "male";
if(age > 18) {
// person is an adult
if(gender.equals("male")) {
// person is a male
System.out.println("You can shop in the men's section on the 3rd Floor");
}
else {
// person is a female
System.out.println("You can shop in the women's section on 2nd Floor");
}
}
else {
// person is not an adult
System.out.println("You can shop in the kid's section on 1st Floor");
}
}
}
If the condition is true, then it will execute the If block. Otherwise, it will execute the Else-If block. Again, if the condition is not met, then it will move to the else block.
Let's see the execution flow of the if-else ladder in a flow diagram:
As you can see in the above flow diagram of the if-else ladder, we execute the if block if the condition is true, otherwise if the condition is false, instead of executing the else block, we check other multiple conditions to determine which block of code to execute. If none of the conditions are true, the last else block without any if condition, if present, is executed.
Syntax:
if(Boolean_expression 1) {
// Executes when the Boolean expression 1 is true
}else if(Boolean_expression 2) {
// Executes when the Boolean expression 2 is true
}else if(Boolean_expression 3) {
// Executes when the Boolean expression 3 is true
}else {
// Executes when the none of the above condition is true.
}
Example Program:
public class Test {
public static void main(String args[]) {
int x = 30;
if( x == 10 ) {
System.out.print("Value of X is 10");
}else if( x == 20 ) {
System.out.print("Value of X is 20");
}else if( x == 30 ) {
System.out.print("Value of X is 30");
}else {
System.out.print("This is else statement");
}
}
}
Switch statement allows program to select one action among multiple actions during the program execution.
Switch statements are almost similar to the if-else-if ladder control statements in Java. It is a multi-branch statement. It is a bit easier than the if-else-if ladder and also more user-friendly and readable. The switch statements have an expression and based on the output of the expression, one or more blocks of codes are executed. These blocks are called cases. We may also provide a default block of code that can be executed when none of the cases are matched
There are certain points that one needs to be remembered while using switch statements:
Let's see the execution flow of the switch statement in a flow diagram:
In the above flow diagram, we have a switch expression and we match the output of the expression through a series of case blocks. Whichever case matches the output, its block is executed and execution skips to the end of the switch; otherwise, if none of the cases matches, the default block is executed.
Syntax:
switch (expression) {
case value1:
//code block of case with value1
break;
case value2:
//code block of case with value2
break;
case valueN:
//code block of case with valueN
break;
default:
//code block of default value
}
Here, we have multiple case statements and each case code block is followed by a break statement to stop the execution to that case only.
Example Program:
class NestedIfDemo {
public static void main(String args[])
{
String browser = "chrome";
switch (browser)
{
case "safari":
System.out.println("The browser is Safari");
break;
case "edge":
System.out.println("The browser is Edge");
break;
case "chrome":
System.out.println("The browser is Chrome");
break;
default:
System.out.println("The browser is not supported");
}
}
}
Looping statements are the statements which executes a block of code repeatedly until some condition meet to the criteria. Loops can be considered as repeating if statements.
Java provides 4 types of looping statements:
While loop executes till the condition becomes false.
Let's see the execution flow of the while loop statement in a flow diagram:
In the above flow diagram of a while loop, we initialize a loop counter variable. After that, we check the loop condition and if it's true, then the body of the loop is executed followed by the updation of the counter variable. The control then again switches back to the loop condition and the cycle continues till the condition is false and we execute the statements outside the loop body.
Syntax:
while (boolean condition)
{
// code block to be executed
}
Let's say we want to print the numbers from 10 to 1 in decreasing order. Let's implement this through a while loop.
public class WhileLoopDemo {
public static void main(String args[]) {
int num = 10;
while( num > 0) {
System.out.println("The value of the number is: " + num);
num--;
}
}
}
When you are using for or while, then it will execute the loop body only if the condition is true. In do-while loop, it will execute the loop first, then it checks the condition. So, it will execute the loop atleast once.
Let's see the execution flow of the do-while loop statement in a flow diagram:
In the above flow diagram of a do-while loop, we also initialize a counter variable, but instead of checking the loop condition at the start, the body of the loop is executed at least once along with updation of the counter variable. After the completion of the loop body, we check the loop condition and continue to execute the loop body till the condition is false when we come out of the loop and execute the rest of the code.
Syntax:
do
{
// code block to be executed
} while (boolean condition)
Let's try to use the same example of printing the number in decreasing order through a do-while loop:
public class DoWhileLoopDemo {
public static void main(String args[]) {
int num = 10;
do {
System.out.println("The value of the number is: " + num);
i--;
}while( num > 0)
}
}
It executes the code until condition is false. It is used when number of iterations are known.
In a for loop statement, execution begins with the initialization of the looping variable, then it executes the condition, and then it increments or decrements the looping variable. If the condition results in true then the loop body is executed otherwise the for loop statement is terminated.
Let's see the execution flow of the for loop statement in a flow diagram:
As you can see in the above flow diagram, we have a for loop expression. In this expression, the loop condition is checked, and if the condition is true, the for loop body is executed till the condition is false and we continue with the normal flow of execution.
Syntax:
for (initialization; termination condition; increment/decrement)
{
// code block to be executed
}
for loop example:
public class ForLoopDemo {
public static void main(String args[]) {
for(int num = 10; num >0; num++)
System.out.println("The value of the number is: " + num);
}
}
The for-each loop statement provides an approach to traverse through elements of an array or a collection in Java. It executes the body of the loop for each element of the given array or collection. It is also known as the Enhanced for loop statement because it is easier to use than the for loop statement as you don’t have to increment the value. The major difference between the for and for-each loop is that whereas the for loop is a general-purpose loop that we can use for any use case, the for-each loop can only be used with collections or arrays.
In for-each loop statement, you cannot skip any element of the given array or collection. Also, you cannot traverse the elements in reverse order using the for-each loop control statement in Java.
Let's see the execution flow of the for-each loop statement in a flow diagram:
As you can see in the above flow diagram of a for each loop, we have a collection. Firstly, we check if the collection has any elements or not. If it has the elements, then the first element is assigned to the local variable mentioned in the for each expression, and the for each loop body is executed. After this, we again check if the collection has any remaining elements and this cycle continues till we have traversed all the elements and we come out of the loop body.
Syntax:
for(dataType variableName : array | collection)
{
// code block to be executed
}
for-each loop example:
public class ForEachLoopDemo {
public static void main(String args[]) {
int[] array = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}
System.out.println("Elements of the array are: ")
for(int elem : array)
System.out.println(elem);
}
}
Jump statments in java are used to jump from a statement to another statement, thereby the transferring the flow of execution.
Jump statements are two types:
The break statement as we can deduce from the name is used to break the current flow of the program. The break statement is commonly used in the following three situations:
The break statement cannot be used as a standalone statement in Java. It must be either inside a switch or a loop. If we try to use it outside a loop or a switch, JVM will give an error.
Let's see the execution flow of the break statement in a flow diagram:
In the above flow diagram of a break statement, whenever the loop body encounters a break statement, it stops the current flow of execution and jumps to the first statement out of the loop body.
Syntax:
for(condition) {
//body of the loop
break;
}
while(loop) {
//body of the loop
break;
}
break statement example:
public class BreakStatementExample {
public static void main(String args[]) {
for(int index = 0; index < 10; index++) {
System.out.println("The value of the index is: " + index);
if(index==3) {
break;
}
}
}
}
Sometimes there are situations where we just want to ignore the rest of the code in the loop body and continue from the next iteration. The continue statement in Java allows us to do just that. This is similar to the break statement in the sense that it bypasses every line in the loop body after itself, but instead of exiting the loop, it goes to the next iteration.
Let's see the execution flow of the continue statement in a flow diagram:
In the above flow diagram of a continue statement, whenever the continue statement has encountered the rest of the loop body is skipped and the next iteration is executed if the loop condition is true.
Syntax:
for(condition) {
//body of the loop
continue;
//the statements after this won't be executed
}
while(loop) {
//body of the loop
continue;
//the statements after this won't be executed
}
flow-diagram-of-continue-statement statement example:
public class ContinueStatementExample {
public static void main(String args[]) {
System.out.println("The odd numbers between 1 to 10 are: ");
for(int number = 1; number <= 10; number++) {
if(number %2 == 0)
continue;
System.out.println(number);
}
}
}