Control Statements in Java

In Java, all the statements we write will execute one after another in the order they appear by default. But when we want to change the flow of execution based on certain conditions or requirements, we need to use control statements.
In java we have 3 types of control statements
Conditional statements
if..Else
switch-case
iterative statements
for
while
do..while
foreach
transferring statements
break
continue
return
Conditional statements are the control statements used to execute a group of statements based on a condition or value. In Java, we have 2 types of conditional statements:
if..else: This conditional statement is used to execute another group of statements based on a given condition.
syntax:
if(condition){
statements1;
}
else{
statements2;
}
Here, if the condition is true, it will execute statements1; otherwise, if the condition is false, it will execute statements2.
switch-case is another conditional statement that is used to execute statements based on a value.
Syntax:
switch(variable){
case label1: statements1;
break;
case label2: statements2;
break;
........
default: statements;
}
Here if the value of the variable id equals to any one of the case labels then the corresponding case statements will be executed, otherwise if all the cases are failed then it will execute default statements.
iterative statements or loops are the control statements which are used execute group of statements for n number of times.
for is used when we want to repeat a set of statements for a fixed number of times. We can control the starting point, ending point, and how the loop moves (increment or decrement) all in one line.
Syntax:
for(initialization; condition;increment/decrement){
statements;
}
Here, the loop will start by initializing the value, then it checks the condition. If the condition is true, it will execute the statements, then perform increment or decrement, and again check the condition. This process will continue until the condition becomes false, then the loop will stop.
while loop is used when we want to repeat a set of statements as long as the given condition is true. The loop will keep running until the condition becomes false.
Syntax:
initialization;
while(condition){
statements;
increment/decrement;
}
Here, the loop first checks the condition. If the condition is true, then it will execute the statements and continue the process by updating the value. The loop will keep running until the condition becomes false, after that the loop will stop.
do while loop is used when we want to execute the statements at least once, no matter what, and then keep repeating them as long as the given condition is true.
Syntax:
initialization;
do {
statements;
increment/decrement;
} while(condition);
Here, the loop will first execute the statements without checking the condition. After that, it will check the condition, and if it is true, it will repeat the loop. This process continues until the condition becomes false, then the loop will stop.
transferring statements are the control statements which are used to transfer the control position from 1 location to another location.
break is used to stop the loop or switch statement immediately and bring the control out of it when a certain condition is met.
continue used to skip the current iteration of the loop and move to the next iteration without stopping the entire loop.
return is used to end the execution of a method and send a value back to the place where the method was called. If the method has no return value, we simply write return;.
Differences between if..else and switch-case
if-else is used when we need to check conditions based on boolean expressions like <, >, <=, >=, or logical operators like &&, ||, !.
switch-case is used when we need to check one variable against multiple fixed constant values using only equality (==).
Use if-else for complex or range-based conditions and use switch for simple multi-option value matching.
Differences between for and while
For loop is used when we know exactly how many times we want to repeat the statements. We can set start, end, and update (increment/decrement) all in one line, so it is short and clear.
While loop is used when we don’t know how many times the loop will run. It keeps running as long as the condition is true and is better when the number of repetitions is unknown.
We use a for loop for fixed repetition and a while loop for conditional repetition when the end is not clear.
Differences between while and do while
While loop checks the condition first, then runs the statements. If the condition is false at the start, the loop won’t run even once.
do-while loop runs the statements first at least once, then checks the condition. So, the loop guarantees one execution even if the condition is false.
We use while when you want to run only if the condition is true and do-while when you want to run at least once no matter what.
Nested Loops in Java
In Java, when we write a loop inside another loop, it is called a nested loop. This means one loop will run completely for each single execution of the outer loop.
We use nested loops when we need to repeat some tasks inside another repeated task, like printing patterns, working with tables, or handling multi-level data.
Control statements in Java help us decide how and when certain parts of the code should run. They give us the power to control the flow of execution based on conditions or to repeat tasks multiple times. By using conditional statements, loops, and transfer statements, we can write programs that are flexible, dynamic, and efficient to handle any kind of real-world situation.
Subscribe to my newsletter
Read articles from PEMMANA VISWESHWAR REDDY directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
