There are three types of control statements in Java:
1) Conditional Statements / Decision Making Statements:
These statements allow you to execute different blocks of code based on the results of a condition. The most common conditional statements in Java are the if-else statement and the switch statement.
2) Loop statements:
Loop statements allow you to repeat a block of code until a certain condition is met. The most common loop statements in Java are the for loop, while loop, and do-while loop.
3) Transfer statements / Jump Statements:
Transfer statements allow you to transfer control from one part of the program to another. The most common transfer statements in Java are the break, continue, and return statements.
In this article, we'll take a closer look at each of these control statements and understand how they can be used to control the flow of a program.
1) Conditional Statements / Decision Making Statements :
Conditional statements in Java allow you to execute different blocks of code based on the results of a condition.
Simple If Statement:
The simple if statement is the most basic form of an if statement. It is used to test a single condition and execute a block of code if the condition is true. The syntax for a simple if statement is as follows:if (condition) { // code to be executed if the condition is true }
int x = 5; if (x > 0) { System.out.println("Hello World"); }
if-else statement:
The if-else statement is an extension of the simple if statement. It allows you to specify a block of code to be executed if the condition is true, and another block of code to be executed if the condition is false. The syntax for an if-else statement is as follows:if (condition) { // code to be executed if the condition is true } else { // code to be executed if the condition is false }
int x = -5; if (x > 0) { System.out.println("x is positive"); } else { System.out.println("x is negative"); }
if-else-if ladder:
The if-else-if ladder is a series of if-else statements, where each if statement is followed by an optional else if statement, and finally an optional else statement. The syntax for an if-else-if ladder is as follows:if (condition1) { // code to be executed if condition1 is true } else if (condition2) { // code to be executed if condition1 is false and condition2 is true } else if (condition3) { // code to be executed if condition1 and condition2 are false and condition3 is true } ... else { // code to be executed if all conditions are false }
For example, consider the following code that prints "x is positive" if the value of x is greater than 0, "x is negative" if the value of x is less than 0, and "x is zero" if the value of x is equal to 0:
int x = -5; if (x > 0) { System.out.println("x is positive"); } else if (x < 0) { System.out.println("x is negative"); } else { System.out.println("x is zero"); }
Nested If-Else Statement:
The nested if-else statement allows you to test multiple conditions. It allows you to specify a series of if-else statements inside another if-else statement. The syntax for a nested if-else statement is as follows:if (condition1) { // code to be executed if condition1 is true if (condition2) { // code to be executed if condition2 is true } else { // code to be executed if condition2 is false } } else { // code to be executed if condition1 is false }
int x = 0; if (x > 0) { System.out.println("x is positive"); } else { if (x < 0) { System.out.println("x is negative"); } else { System.out.println("x is zero"); } }
Switch statement:
In Java programming, the switch statement provides an alternative way to make conditional decisions based on the value of a single expression. Unlike the if-else statement, the switch statement is designed to handle multiple cases, making it a more concise and efficient way to handle multiple conditions. we'll take a look at what the switch statement is, how it works, and how you can use it in your Java programs.
The switch statement evaluates an expression and then executes one of several statements based on the value of that expression. The syntax for a switch statement is as follows:
switch (expression) { case value1: // code to be executed if expression is equal to value1 break; case value2: // code to be executed if expression is equal to value2 break; ... default: // code to be executed if expression is not equal to any of the values break; }
Flowchart of Switch Statement
For example, consider the following code that prints "x is positive" if the value of x is greater than 0, "x is negative" if the value of x is less than 0, and "x is zero" if the value of x is equal to 0:
int x = -5; switch (x > 0) { case true: System.out.println("x is positive"); break; case false: switch (x < 0) { case true: System.out.println("x is negative"); break; case false: System.out.println("x is zero"); break; } break; }
2) Loop Statements:
Loop statements in Java allow you to repeat a block of code until a certain condition is met.for Statement:
The for loop is used to repeat a block of code a specific number of times.
The flow chart for the for-loop is given below.
Consider the following example to understand the proper functioning of the for loop in java.
For example:
for (int i = 0; i < 10; i++) { System.out.println(i); }
while loop:
The while loop is used to repeat a block of code as long as a certain condition is true.The flow chart for the while loop is given in the following image.
Consider the following example.
For example:
int i = 0; while (i < 10) { System.out.println(i); i++; }
do-while loop:
The do-while loop is similar to the while loop, but it guarantees that the block of code will be executed at least once. The flow chart of the do-while loop is given in the following image.
Consider the following example to understand the functioning of the do-while loop in Java.
For example:
int i = 0; do { System.out.println(i); i++; } while (i < 10);
3) Transfer Statements / Jump Statements:
Transfer statements in Java allow you to transfer control from one part of the program to another.break statements:
The break statement is used to exit a loop or switch statement early.
For example:
for (int i = 0; i < 10; i++) { if (i == 5) { break; } System.out.println(i); }
Continue Statements:
The continue statement is used to skip the current iteration of a loop and move on to the next iteration. For example:
for (int i = 0; i < 10; i++) { if (i % 2 == 0) { continue; } System.out.println(i); }
return Statements:
The return statement is used to exit a method and return a value. For example:
public int sum(int a, int b) { return a + b; }