• Articles
  • Tutorials
  • Interview Questions

Control Statements in Java - A Comprehensive Guide

Table of content

Show More

Java Loop Iteration Overview

In this section, we are going to discuss about the control statements in Java.
Different situations may demand different execution flow of the program. There can be many different conditions which you want your program to follow.
Some of these situations can be:

  • You want your program to execute the same statement number of times.
  • You want your program after executing a certain number of times

Let’s say you want to print 9 for a number of times and you don’t want to write print statement that number of times. What will you do? To solve this type of problem control statements are used in java.

Watch this Java video by Intellipaat:

Video Thumbnail

Control Statements in Java:
Control Statements in Java are used to control the execution flow of the program.
Java1
There are three types of control statements:

  • Conditional Control Statements in Java
  • Looping Control Statements in Java
  • Unconditional Control Statements/Jump Statements in Java

Conditional Control Statements in Java

Conditional Control Statements allows the program to select between the alternatives during the program execution.
They are also called as decision-making statements or selection statements.

1. If statement

It will go inside the block only if the condition is true otherwise, it will not execute the block.

if(condition){
// statements (if Block)
}
//other statements
• Below flow chart shows the condition of if block execution.
Execution Flow Chart of If Statement
Java2

Get 100% Hike!

Master Most in Demand Skills Now!

2. If-Else Statement

If the condition is true then, it will execute the If block. Otherwise, it will execute the Else block.

if(condition){
// statements (if Block)
}
else{
// statements (Else block)
}
//other statements
• Below flow chart shows the condition of if block execution.

Execution flow chart of If-Else Statement
java

Check out the list of Hibernate Interview Questions to prepare for upcoming interviews.

3. If Else-If statement

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.

if(condition){
// statements (if Block)
}
else if{
// statements (Else-If block)
}
else{
//statements(Else Block)
}//other statements

Execution flow chart of If Else-If Statement
java
Program to understand If statements

class WriteExample{
public static void main(String []args){
int a=2; int b=3; int c=4;
if(a>b){
System.out.print(“Institute1”);
}
else if(a>c){
System.out.print(“Institute2”);
}
else{
System.out.print(“Intellipaat”);
}
}
}

Output:
Intellipaat

4. Switch Statement

Switch statement allows program to select one action among multiple actions during the program execution.
Syntax:

Switch(variable/value/expression){
Case :
// statements [];
Case :
// statements [];
…
default:
// statements [];
}
  • Based on the argument in the switch statement suitable case value will be selected and executed.
  • If no matching case found, then the default will be executed.
  • It is optional to write a break statement at the end of each case statement.

Program to understand switch statement:

class WriteExample{
public static void main(String []args){
int choice=2; int a=1;
switch(choice){
case a:
System.out.print(“For Right Study”); break;
case choice:
System.out.print(“Intellipaat Trainings”); break;
default:
System.out.print(“No choice found”); break;
}
} }
}

Output: Intellipaat Trainings

Want to ace your next Java interview? Check out our recent blog post about the most common Java interview questions and answers!

Looping Control Statements in Java

These are used to execute a block of statements multiple times. It means it executes the same code multiple times so it saves code. These are also called Iteration statements.

There are three types of looping control statements:

  • For loop
  • While loop
  • Do-while loop

1. For loop

  • It executes the code until condition is false.
  • It is used when number of iterations are known.

Syntax:

for(initialization; condition; increment/decrement){
//statements (For Body)
}

Program to understand for loop

class WriteExample{
public static void main(String []args){
int a=1;
for(int i=0; i<2; i++){
System.out.println(“Value of a is:”+ a); // string concatenation
a++; // increases value by 1
}
}
}

Output: Value of a is:1
Value of a is:2

2. While loop

  • While loop executes till the condition becomes false.

Syntax:

while(condition){
// statements
}

Program to understand while loop

class Intellipaat {public static void main(String args[]) {
int i = 2;
while( i <=2 ) {
System.out.println("Intellipaat” );
i++;
}
}
}

Output:  Intellipaat

Learn Java from scratch with this free Java Tutorial!

3. Do-while loop

  • When you are using for or while, then it will execute the loop body only if the condition if true.
  • In do-while loop, it will execute the loop first, then it checks the condition. So, it will execute the loop atleast once.
  • It is called exit controlled loop while for & while loop are called entry controlled loop.

Syntax:

do{
// statements
}while(condition);

Program to understand do-while loop

class Intellipaat {public static void main(String args[]){
int a = 2;
do{
System.out.println("Intellipaat”);
i++;
}while( a< =2 );
}
}

Output: Intellipaat

Go for the most professional Java Training Course for a stellar career now!

Unconditional Control Statements/Jump Statements in Java

1. break Statement

  • break is a keyword. It is used within any control statements. It is used to terminate the execution of the current loop or switch statements.
  • Syntax: break; or break <label>;
class Intellipaat{ // break without label
public static void main(String args[]){
for(int i=1;i<=4;i++){
if(i==3) break;
System.out.println(“Intellipaat.com”);}
} }

Output:
Intellipaat.com
Intellipaat.com

/* break with label is used to terminate the number of loops below the label */
class Intellipaat{
public static void main(String args[]){
Termi:
for(int i=1;i<=4;i++){
for(int k=1;i<=4;k++){
if(i==3) break Termi;
System.out.println(“Intellipaat.com”);
}
}
} }

Output:
Intellipaat.com
Intellipaat.com

Certification in Bigdata Analytics

2. continue Statement

  • continue is a keyword. It is used to continue the execution of the current loop with the next iteration.
  • Syntax: continue; or continue<label>;
class Intellipaat{ //continue without label
public static void main(String args[]){
for(int i=1;i<=4;i++){
System.out.print(“ “+i+” “);
if(i==3) continue;
System.out.println(“Intellipaat.com”);}
} }

Output:
1 Intellipaat.com
2 Intellipaat.com
3 4 Intellipaat.com

/* continue with label is used to continue the number of loops below the label */
class Intellipaat{
public static void main(String args[]){
Termi:
for(int i=1;i<=4;i++){
for(int k=1;i<=4;k++){
System.out.print(“ ”+i+” ”);
if(i==3) continue Termi;
System.out.println(“Intellipaat.com”);
}
}
} }

Output:
1 Intellipaat.com
2 Intellipaat.com
3 1 Intellipaat.com
2 Intellipaat.com
3 1 Intellipaat.com
This statement will keep on printing the same output till the memory gets full.

Read on:- Polymorphism in Java!

Course Schedule

Name Date Details
Python Course 23 Nov 2024(Sat-Sun) Weekend Batch View Details
30 Nov 2024(Sat-Sun) Weekend Batch
07 Dec 2024(Sat-Sun) Weekend Batch

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration.