Control Statements in Java
Control statements are used to control the flow of execution of a program. Java has three types of control statements:
- Decision-Making Statements
- Looping Statements
- Jump Statements
1. Decision-Making Statements
These statements allow you to control the flow of execution based on certain conditions.
1.1. if Statement
The if statement allows the execution of a block of code only if a specific condition is true.
public class IfStatementExample {
public static void main(String[] args) {
int number = 10;
if (number > 0) {
System.out.println("Number is positive.");
}
}
}
Output: Number is positive.
1.2. if-else Statement
The if-else statement executes one block of code if the condition is true, and another block if the condition is false.
public class IfElseStatementExample {
public static void main(String[] args) {
int number = -5;
if (number > 0) {
System.out.println("Number is positive.");
} else {
System.out.println("Number is negative.");
}
}
}
Output: Number is negative.
1.3. if-else if Statement
The if-else if statement is used to check multiple conditions. It checks each condition sequentially.
public class IfElseIfStatementExample {
public static void main(String[] args) {
int number = 0;
if (number > 0) {
System.out.println("Number is positive.");
} else if (number < 0) {
System.out.println("Number is negative.");
} else {
System.out.println("Number is zero.");
}
}
}
Output: Number is zero.
1.4. switch Statement
The switch statement allows you to test a variable against a series of values (cases). It is an alternative to multiple if-else if statements.
public class SwitchStatementExample {
public static void main(String[] args) {
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
default:
System.out.println("Invalid day");
}
}
}
Output: Wednesday
2. Looping Statements
Looping statements are used to repeat a block of code as long as a condition is true.
2.1. for Loop
The for loop is used when you know beforehand how many times you want to execute a statement or a block of statements.
public class ForLoopExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println("Iteration number: " + i);
}
}
}
Output:
Iteration number: 1
Iteration number: 2
Iteration number: 3
Iteration number: 4
Iteration number: 5
2.2. while Loop
The while loop repeats a block of code as long as a given condition is true. The condition is evaluated before the execution of each iteration.
public class WhileLoopExample {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
System.out.println("Iteration number: " + i);
i++;
}
}
}
Output:
Iteration number: 1
Iteration number: 2
Iteration number: 3
Iteration number: 4
Iteration number: 5
2.3. do-while Loop
The do-while loop works similarly to the while loop, but the condition is evaluated after the execution of the loop, meaning the loop is guaranteed to run at least once.
public class DoWhileLoopExample {
public static void main(String[] args) {
int i = 1;
do {
System.out.println("Iteration number: " + i);
i++;
} while (i <= 5);
}
}
Output:
Iteration number: 1
Iteration number: 2
Iteration number: 3
Iteration number: 4
Iteration number: 5
3. Jump Statements
Jump statements are used to transfer control to another part of the program.
3.1. break Statement
The break statement is used to exit from a loop or switch statement prematurely.
public class BreakStatementExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break; // Exits the loop when i is 3
}
System.out.println("Iteration number: " + i);
}
}
}
Output:
Iteration number: 1
Iteration number: 2
3.2. continue Statement
The continue statement is used to skip the current iteration of a loop and continue with the next iteration.
public class ContinueStatementExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // Skips the iteration when i is 3
}
System.out.println("Iteration number: " + i);
}
}
}
Output:
Iteration number: 1
Iteration number: 2
Iteration number: 4
Iteration number: 5
3.3. return Statement
The return statement is used to exit from a method and optionally return a value.
public class ReturnStatementExample {
public static void main(String[] args) {
System.out.println("Sum: " + addNumbers(5, 3));
}
public static int addNumbers(int a, int b) {
return a + b; // Exits the method and returns the result
}
}
Output: Sum: 8
Summary of Control Statements in Java
Control statements in Java are used to alter the flow of program execution. They are categorized into three main types: Decision-Making Statements, Looping Statements, and Jump Statements. Below is a table summarizing the key control statements with brief descriptions and examples.
| Control Statement | Description | Example |
|---|---|---|
| if | Executes a block of code if a condition is true. | if (condition) { // block of code } |
| if-else | Executes one block of code if a condition is true, another if false. | if (condition) { // block of code } else { // other block of code } |
| if-else if | Checks multiple conditions and executes the first block of code with a true condition. | if (condition1) { // block1 } else if (condition2) { // block2 } else { // block3 } |
| switch | Evaluates an expression and matches it with multiple cases. | switch (expression) { case value1: // block1 break; case value2: // block2 break; default: // default block } |
| for | Executes a block of code a specific number of times. | for (initialization; condition; increment) { // block of code } |
| while | Repeats a block of code as long as a condition is true. | while (condition) { // block of code } |
| do-while | Executes a block of code at least once, and then repeats while a condition is true. | do { // block of code } while (condition); |
| break | Exits from a loop or switch statement. | break; |
| continue | Skips the current iteration of a loop and moves to the next iteration. | continue; |
| return | Exits from a method and optionally returns a value. | return value; |