Updated March 20, 2023
ย
ย
Overview of Control Statement in Java
Control statements in Java are statements that help in determining the flow of control from one statement in Java to another. Control statements in Java are of many types. In this article, we are going to observe the different aspects of control statements along with some of their examples. The different kinds of control statements are:
Decision-Making Statements:
- if-else, Nested if-else statements
- Switch case statements
Repetition/Looping Statements:
- For loop
- For each loop
- While loop
- Do while loop
Jumping Statements:
- Break
- Continue
- Goto
- Return
These are the above statements that are used in Java as flow control statements, and they are classified into Decision-making statements, Repetition or Looping statements and Jumping statements.
Decision-Making Statements in Java
In the decision-making statements, we will see about the if-else and nested if-else statement and the Switch case statement. We are also going to see coding examples and sample output showing the validity and execution of such statements.
1. If Else Statement
The if-else statement works in a conditional fashion. The following is the syntax:
Syntax:
if(condition)
Statement 1
else
Statement 2
Example:
In the first if-else example, we are going to see whether a number entered by the user is greater than 100 or not. If the number is greater than 100, then the output will be shown accordingly.
Code:
import java.io.*;
public class Example1
{
public static void main(String args[])throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("ENTER A NUMBER");
int n= Integer.parseInt(br.readLine());
if (n>100)
System.out.println("NUMBER ENTERED IS GREATER THAN 100");
else
System.out.println("NUMBER ENTERED IS LESS THAN 100");
}
}
Output:
In the following program, we enter two numbers. Whenever we enter 250 as a number, the program shows that it is more than 100, and whenever we enter 65 as a number, the program shows that the number is less than 100.
2. Nested If Else Statement
In the nested, if-else statement, there are multiple if conditions, and finally, there is a print statement. The following is the syntax:
Syntax:
if (condition1)
if(condition2)
if (condition3)
Statement 1
Example:
In nested if statements, we check conditions using two or three if-else statements and then we finally arrive at a conclusion. We further check whether the number is greater than 200 or not; if it is greater, we print it as greater than 200.
Code:
import java.io.*;
public class Example2
{
public static void main(String args[])throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("ENTER A NUMBER");
int n= Integer.parseInt(br.readLine());
if (n>100)
{
if(n>200)
System.out.println("NUMBER ENTERED IS GREATER THAN 200");
}
}
}
Output:
We enter both the numbers greater than 200, and we find the correct output that both the numbers are greater than 200.
3. Switch Case Statement
In the switch cases, there are multiple cases from which one is selected. The following is the syntax:
Syntax:
switch(Variable)
case 1:
case 2:
case 3:
case n:
Example:
In this example, we will enter a number, and the program will return what number the user has returned. This is a simple example of a switch case statement that runs in the BlueJ programming interface.
Code:
import java.io.*;
public class Example3
{
public static void main(String args[])throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("ENTER A NUMBER BETWEEN 1 TO 5");
int n= Integer.parseInt(br.readLine());
switch(n)
{
case 1: System.out.println("NUMBER ENTERED IS 1");
break;
case 2: System.out.println("NUMBER ENTERED IS 2");
break;
case 3: System.out.println("NUMBER ENTERED IS 3");
break;
case 4: System.out.println("NUMBER ENTERED IS 4");
break;
case 5: System.out.println("NUMBER ENTERED IS 5");
break;
}
}
}
Output:
In the above code, we enter the number as 4, and the program returns that the number entered is 4.
Repetition/Looping Statements in Java
Below are the Repeat / Looping clauses in Java:
A. For Loop
In the for loop, the loop goes on for a number of times as initialized by the user. The following is the syntax.
Syntax:
for(initialization, condition, update)
Statement 1
Example:
In the for loop example, we are going to print the odd numbers starting from 3 to 10. We use a for loop for the respective program.
Code:
import java.io.*;
public class Example4
{
public static void main(String args[])throws IOException
{
System.out.println("Odd numbers from 3 to 10 are as follows");
for(intย i=3; i<10; i+=2)
{
System.out.println(i);
}
}
}
Output:
In the above program, we see the odd numbers starting from 3 to 10, and the numbers printed are 3,5,7 and 9.
B. While Loop
In the while loop, the statement is executed while the condition is true. The following is the syntax:
Syntax:
while(Condition)
Statement 1
Example:
Using a while loop, we are now going to find the reverse of a number. This program is robust and can find the reverse of any integer number.
Code:
import java.io.*;
public class Example5
{
public static void main(String args[])throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("ENTER A NUMBER");
int n= Integer.parseInt(br.readLine());
int digit, rev=0;
while(n>0)
{
digit= n%10;
rev= (rev*10) +digit;
n=n/10;
}
System.out.println("Reverse number is " +rev);
}
}
Output:
In the above program, we find the reverse of a particular number. The number entered is 635, and the reverse of the number is 536, as displayed on the output screen.
Jumping Statements in Java
The jumping statements in java are explained below.
Break Statement
There can be in the for loop in the break statement while loop or in switch case. Following is the syntax.
Syntax:
for(Statements)
break;
while(Statements)
break;
Example:
In this example, we will see a menu-driven program, and we see the break statement’s application.
Code:
import java.io.*;
public class Example6
{
public static void main(String args[])throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("ENTER A NUMBER BETWEEN 1 TO 5");
int n= Integer.parseInt(br.readLine());
switch(n)
{
case 1: System.out.println("NUMBER ENTERED IS 1");
break;
case 2: System.out.println("NUMBER ENTERED IS 2");
break;
case 3: System.out.println("NUMBER ENTERED IS 3");
break;
case 4: System.out.println("NUMBER ENTERED IS 4");
break;
case 5: System.out.println("NUMBER ENTERED IS 5");
break;
default: System.out.println("Number entered is not between 1 to 5");
break;
}
}
}
Output:
The above code is very similar to the code used in the switch case statements. The break statement is generally used in the switch case statement. The break statement is also used in the if-else condition where the if-else statements need to be terminated. The above program asks for the number entered between 1 to 5. If the number is not between 1 to 5, then there is a default print that the number entered is not between 1 to 5. In the above case, we enter the number as 65, and it prints accordingly that the number entered is not between 1 to 5.
Conclusion – Control Statement in Java
In this article, we come across the control statements in Java that are present. We take a look at the Looping statements, the Conditional statements, and others that are present. We also look at the programming aspects of the statements and how the statements are used inside the code. Control statements are quite used in Java, and they are present in every other programming language. They are used significantly throughout all programs for smooth execution.
Recommended Articles
This is a guide to Control Statement in Java. Here we discuss the basic concept, various control statements in java, i.e. Decision-making, Repetition/Looping, and Jumping statements, etc. You may also look at the following articles to learn more-