Java Control Statement
Control statements in Java are essential for controlling the flow of execution in a program. They allow you to make decisions, execute certain blocks of code conditionally, or repeat blocks of code based on specific conditions. Java has three main categories of control statements:
- Conditional Statements
- Looping Statements
- Jump Statements
Let's explore each of these categories in detail.
1. Conditional Statements
Conditional statements allow you to execute a block of code based on whether a specified condition evaluates to true
or false
.
a. if
Statement
The if
statement executes a block of code if its condition is true.
Example:
int number = 10;
if (number > 0) {
System.out.println("The number is positive.");
}
b. if-else
Statement
The if-else
statement provides an alternative block of code to execute when the condition is false.
Example:
int number = -5;
if (number > 0) {
System.out.println("The number is positive.");
} else {
System.out.println("The number is not positive.");
}
c. if-else if-else
Statement
This structure allows you to check multiple conditions.
Example:
int number = 0;
if (number > 0) {
System.out.println("The number is positive.");
} else if (number < 0) {
System.out.println("The number is negative.");
} else {
System.out.println("The number is zero.");
}
d. switch
Statement
The switch
statement allows you to execute one block of code among many based on the value of a variable.
Example:
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;
default:
System.out.println("Invalid day");
}
2. Looping Statements
Looping statements allow you to execute a block of code repeatedly based on a condition.
a. for
Loop
The for
loop is used when the number of iterations is known.
Example:
for (int i = 1; i <= 5; i++) {
System.out.println("Iteration: " + i);
}
b. while
Loop
The while
loop continues executing as long as the specified condition is true. It is used when the number of iterations is not known in advance.
Example:
int i = 1;
while (i <= 5) {
System.out.println("Iteration: " + i);
i++;
}
c. do-while
Loop
The do-while
loop is similar to the while
loop, but it guarantees that the block of code will execute at least once.
Example:
int i = 1;
do {
System.out.println("Iteration: " + i);
i++;
} while (i <= 5);
3. Jump Statements
Jump statements allow you to alter the flow of control in your program.
a. break
Statement
The break
statement is used to terminate a loop or a switch statement immediately.
Example:
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break; // Exit the loop when i is 3
}
System.out.println("Iteration: " + i);
}
// Output: Iteration: 1
// Iteration: 2
b. continue
Statement
The continue
statement skips the current iteration of a loop and moves to the next iteration.
Example:
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // Skip the iteration when i is 3
}
System.out.println("Iteration: " + i);
}
// Output: Iteration: 1
// Iteration: 2
// Iteration: 4
// Iteration: 5
c. return
Statement
The return
statement is used to exit a method and optionally return a value.
Example:
public class ReturnExample {
public static int add(int a, int b) {
return a + b; // Exit the method and return the sum
}
public static void main(String[] args) {
int sum = add(5, 10);
System.out.println("Sum: " + sum); // Output: Sum: 15
}
}
Summary
Control statements in Java play a crucial role in defining the flow of execution in a program. Understanding how to use conditional statements, looping statements, and jump statements effectively allows you to write more flexible and powerful Java applications. Mastery of these control statements is essential for any Java programmer.