For programmers, Java is one of the most preferred programming languages. Java is an advanced programming language that has many tools and benefits that allow programmers to efficiently compile codes.

Edureify, the best AI Learning App has mentioned Java and its various tools and features in its previous informative articles. Even the coding courses that are offered by Edureify teach all the important tools and frameworks of Java.

In this article, Edureify will provide information on Java Control Statements, another important tool of Java. Read on to know more.

What is Java Control Statement?

Java control statement or Java control flow statement is a feature of Java that helps in the efficient flow and progress of a program. The control statements in Java determine the consecutive actions of other statements or commands. The Java control statements are the commands that generate further actions.

Types of Java Control Statements

Primarily, there are three types of Java control statements that has further branches. The different types of Java control statements are-

  • Decision Making Statements-
    • if statements
    • switch statements
  • Loop Statements-
    • do while loop
    • while loop
    • for loop
    • for-each loop
  • Jump Statements-
    • break statement
    • continue statement

Decision Making Statements

In programming, certain situations require a certain block of code that can be executed when some criteria are fulfilled. The control statements help control the flow of the execution of a program after considering certain conditions.

If Statement-

The if statement helps evaluate a condition. The statement diverts the control of the program by depending on the specific condition. It helps determine whether a statement is true or false.

Example-

public class Student {    
public static void main(String[] args) {    
int a = 7;    
int b = 14;    
if(a+b > 15) {    
System.out.println("a + b is greater than 15");    
}    
}      
}

Output:

a + b is greater than 15

Switch Statement-

The switch statement contains multiple blocks of code called cases. Depending on the variable that is being switched, a single case can be executed. Some features of the switch statement are-

  • There cannot be duplicate cases
  • int, short, byte, char, or enumeration be the case variables
  • The case expression must be of the same type as the variable

Example-

public class Student implements Cloneable {  
public static void main(String[] args) {  
int num = 5;  
switch (num){  
case 2:  
System.out.println("number is 2");  
break;  
case 4:  
System.out.println("number is 4");  
break;  
default:  
System.out.println(num);  
}  
}  
}

Output-

5

Loop Statement

The loop statements help execute instructions in a repeated manner.

For loop

The for loop initializes the loop variable. One must remember that the for loop can be used when one knows the exact number of times one needs to execute the block of code.

Example-

for (int num = 6; num <= 11; num ++)
{
              System.out.println(num);
}

Output-

6
7
8
9
10
11

For Each Loop

This statement is an enhancement for each loop. The purpose of the for each loop is to iterate over elements of collections and arrays.

Example-

int[] numList = { 50, 60, 70, 80};
for (int num : numList)
{
              System.out.println(num);
}

Output-

50
60
70
80
90

While Loop

The while loop is used when one is not aware of the number of repetitions in advance. The statements initialization and other actions do not take place inside the loop statement in this case which is unlike for loop.

Example-

public class JavaExample
{
              public static void main(String[] args)
              {
                  int count = 6;
                  while (count < 11)
                  {
                             System.out.println(“Count is: “ + count) ;
                             count++:
                 }
              }
}

Output-

6
7
8
9
10
11

Do While Loop

After the execution of the loop statements, the do while loop statement checks the condition at the end of the loop.

Example-

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 4 even numbers \n");    
do {    
System.out.println(i);    
i = i + 2;    
}while(i<=4);    
}    
}

Output-

0
2
4
6

Jump Statements

To transfer the control of the program to a particular statement, the jump statement is used. The control is transferred to some other part of the program with this statement.

Java Break Statement

To break the existing flow of a program and shift the control to the consecutive statement outside a loop or a switch statement the break statement is used.

Example-

public class BreakExample {  
public static void main(String[] args) {  
// TODO Auto-generated method stub  
for(int i = 7; i<= 10; i++) {  
System.out.println(i);  
if(i==14) {  
break;  
}  
}  
}  
}

Output-

7
8
9
10
11
12
13
14

Continue Statement

To skip to a specific part of the loop without breaking the loop and jumping to the next loop, the continue statement is used.

public class ContinueExample {  
public static void main(String[] args) { 
// TODO Auto-generated method stub  
for(int i = 10; i<= 12; i++) {  
for (int j = i; j<=15; j++) {  
if(j == 14) {  
continue;  
}  
System.out.println(j);  
}  
}  
}  
}

Output-

10
11
12
13
15
11
12
13
15
12
13
15

Here was the discussion on Java control statements.

Edureify provides more information on Java with its best coding courses. Some of the important topics covered in the coding courses of Edureify on Java are-

With Edureify’s certified coding courses, students will also benefit from the following-

  • 200+ learning hours
  • Live lectures
  • Learn from the industry experts
  • Get professional career guidance

The certified coding courses of Edureify provides the best learning experience to its students.

Some FAQs on Java Control Statements-

1. What is the Java control statement?

Java control statement or Java control flow statement is a feature of Java that helps in the efficient flow and progress of a program. The control statements in Java determine the consecutive actions of other statements or commands. The Java control statements are the commands that generate further actions.

2. What are the main types of Java control statement?

Decision making statement, loop statement, and jump statement are the three main types of Java control statement.

3. What are the different decision making statements?

The different decision making statements are-

  • if statements
  • switch statements

4. What are the different jump statements?

The different jump statements are-

  • break statement
  • continue statement

5. From where can I learn more about Java?

Study with Edureify and get the best coding courses to learn Java.

Facebook Comments