Java/J2EE and SOA (349 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (5 Blogs)
  • Java/J2EE and SOA (346 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

What are Control Statements in Java?

Last updated on Nov 29,2022 82.5K Views


Control Statements in Java is one of the fundamentals required for Java Programming. It allows the smooth flow of a program. Following pointers will be covered in this article:

Every programmer is familiar with the term statement, which can simply be defined as an instruction given to the computer to perform specific operations. A control statement in java is a statement that determines whether the other statements will be executed or not. It controls the flow of a program. An ‘if’ statement in java determines the sequence of execution between a set of two statements.

Control Statements in JavaControl Statements can be divided into three categories, namely

  • Selection statements
  • Iteration statements
  • Jump statements

Moving on with this article on Control Statements in Java

 

Decision-Making Statements

Statements that determine which statement to execute and when are known as decision-making statements. The flow of the execution of the program is controlled by the control flow statement.
There are four decision-making statements available in java.

Moving on with this article on Control Statements in Java

 

Simple if statement

The if statement determines whether a code should be executed based on the specified condition.
Syntax:

if (condition) { 
Statement 1; //executed if condition is true
}
Statement 2; //executed irrespective of the condition

Output:
If statement!
Hello World!

Moving on with this article on Control Statements in Java

 

If..else statement

In this statement, if the condition specified is true, the if block is executed. Otherwise, the else block is executed.
Example:


public class Main
{
public static void main(String args[])
{
int a = 15;
if (a > 20)
System.out.println("a is greater than 10");
else
System.out.println("a is less than 10");
System.out.println("Hello World!");
}
}
}

Output:
a is less than 10
Hello World!

Moving on with this article on Control Statements in Java

 

Nested if statement

An if present inside an if block is known as a nested if block. It is similar to an if..else statement, except they are defined inside another if..else statement.
Syntax:


if (condition1) {
Statement 1; //executed if first condition is true
if (condition2) {
Statement 2; //executed if second condition is true
}
else {
Statement 3; //executed if second condition is false
}
}

Example:

public class Main
{
public static void main(String args[])
{
int s = 18;
if (s > 10)
{
if (s%2==0)
System.out.println("s is an even number and greater than 10!");
else
System.out.println("s is a odd number and greater than 10!");
}
else
{
System.out.println("s is less than 10");
}
System.out.println("Hello World!");
}
}

Output:
s is an even number and greater than 10!
Hello World!

Moving on with this article on Control Statements in Java

 

Switch statement

A switch statement in java is used to execute a single statement from multiple conditions. The switch statement can be used with short, byte, int, long, enum types, etc.
Certain points must be noted while using the switch statement:
α One or N number of case values can be specified for a switch expression.
α Case values that are duplicate are not permissible. A compile-time error is generated by the compiler if unique values are not used.
α The case value must be literal or constant. Variables are not permissible.
α Usage of break statement is made to terminate the statement sequence. It is optional to use this statement. If this statement is not specified, the next case is executed.

Example:


public class Music {
public static void main(String[] args)
{
int instrument = 4;
String musicInstrument;
// switch statement with int data type
switch (instrument) {
case 1:
musicInstrument = "Guitar";
break;
case 2:
musicInstrument = "Piano";
break;
case 3:
musicInstrument = "Drums";
break;
case 4:
musicInstrument = "Flute";
break;
case 5:
musicInstrument = "Ukelele";
break;
case 6:
musicInstrument = "Violin";
break;
case 7:
musicInstrument = "Trumpet";
break;
default:
musicInstrument = "Invalid";
break;
}
System.out.println(musicInstrument);
}
}

Output:
Flute

Moving on with this article on Control Statements in Java

 

Looping Statements

Statements that execute a block of code repeatedly until a specified condition is met are known as looping statements. Java provides the user with three types of loops:

Moving on with this article on Control Statements in Java

 

While

Known as the most common loop, the while loop evaluates a certain condition. If the condition is true, the code is executed. This process is continued until the specified condition turns out to be false.
The condition to be specified in the while loop must be a Boolean expression. An error will be generated if the type used is int or a string.

Syntax:


while (condition)
{
statementOne;
}

Example:

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

Output:
5
7
9
11
13
15

Moving on with this article on Control Statements in Java

 

Do..while

The do-while loop is similar to the while loop, the only difference being that the condition in the do-while loop is evaluated after the execution of the loop body. This guarantees that the loop is executed at least once.

Syntax:


do{
//code to be executed
}while(condition);

Example:

public class Main 
{ 
public static void main(String args[]) 
{ 
int i = 20; 
do 
{ 
System.out.println(i); 
i = i+1; 
} while (i <= 20); 
} 
}
 &nbsp;

Output:
20

Moving on with this article on Control Statements in Java

 

For

The for loop in java is used to iterate and evaluate a code multiple times. When the number of iterations is known by the user, it is recommended to use the for loop.

Syntax:


for (initialization; condition; increment/decrement)
{
statement;
}

Example:

public class forLoop
{
public static void main(String args[])
{
for (int i = 1; i <= 10; i++)
System.out.println(i);
}
}

Output:
5
6
7
8
9
10

Moving on with this article on Control Statements in Java

 

For-Each

The traversal of elements in an array can be done by the for-each loop. The elements present in the array are returned one by one. It must be noted that the user does not have to increment the value in the for-each loop.

Example:


public class foreachLoop{
public static void main(String args[]){
int s[] = {18,25,28,29,30};
for (int i : s) {
System.out.println(i);
}
}
}

Output:
18
25
28
29
30

Moving on with this article on Control Statements in Java

 

Branching Statements

Branching statements in java are used to jump from a statement to another statement, thereby the transferring the flow of execution.

Moving on with this article on Control Statements in Java

 

Break

The break statement in java is used to terminate a loop and break the current flow of the program.

Example:


public class Test
{
public static void main(String args[])
{
for (int i = 5; i < 10; i++)
{
if (i == 8)
break;
System.out.println(i);
}
}
}

Output:
5
6
7

Moving on with this article on Control Statements in Java

 

Continue

To jump to the next iteration of the loop, we make use of the continue statement. This statement continues the current flow of the program and skips a part of the code at the specified condition.

Example:


public class Main
{
public static void main(String args[])
{
for (int k = 5; k < 15; k++)
{
// Odd numbers are skipped
if (k%2 != 0)
continue;
// Even numbers are printed
System.out.print(k + " ");
}
}
}

Output:
6 8 10 12 14

If you’re just beginning, then watch at this Java Loops Tutorial to Understand the Fundamental Concepts.

With this, we come to an end of this Control Statements in Java Article. The control statements in java must be used efficiently to make the program effective and user-friendly.

Edureka’s Java Certification Training is curated by professionals as per the industrial requirements and demands. This training encompasses comprehensive knowledge on basic and advanced concepts of core Java & J2EE along with popular frameworks like Hibernate, Spring, & SOA. In this course, you will gain expertise in concepts like Java Array, Java OOPs, Java Function, Java Loops, Java Collections, Java Thread, Java Servlet, and Web Services using industry use-cases. Join our Java Training in Mangalore today.

 

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 7th December,2024

7th December

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

What are Control Statements in Java?

edureka.co