Control statements in Java are used to control the flow of execution in a program based on certain conditions or loops. They allow you to make decisions, repeat tasks, and manage the flow of your program. The main types of control statements in Java are:
- Decision-Making Statements:
if
,if-else
,if-else-if
, andswitch
.
- Looping Statements:
for
,while
, anddo-while
.
- Jump Statements:
break
,continue
, andreturn
.
Below is a detailed explanation of each type of control statement.
1. Decision-Making Statements
a. if
Statement
The if
statement is used to execute a block of code if a condition is true.
if (condition) {
// Code to execute if condition is true
}
Example:
int age = 18;
if (age >= 18) {
System.out.println("You are eligible to vote.");
}
b. if-else
Statement
The if-else
statement is used to execute one block of code if the condition is true and another block if the condition is false.
if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
Example:
int age = 16;
if (age >= 18) {
System.out.println("You are eligible to vote.");
} else {
System.out.println("You are not eligible to vote.");
}
c. if-else-if
Ladder
The if-else-if
ladder is used to test multiple conditions and execute different blocks of code based on the first true condition.
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else {
// Code to execute if all conditions are false
}
Example:
int marks = 85;
if (marks >= 90) {
System.out.println("Grade: A");
} else if (marks >= 80) {
System.out.println("Grade: B");
} else if (marks >= 70) {
System.out.println("Grade: C");
} else {
System.out.println("Grade: D");
}
d. switch
Statement
The switch
statement is used to execute one block of code among many based on the value of a variable or expression.
switch (expression) {
case value1:
// Code to execute if expression == value1
break;
case value2:
// Code to execute if expression == value2
break;
default:
// Code to execute if expression doesn't match any case
}
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
a. for
Loop
The for
loop is used to execute a block of code repeatedly for a fixed number of times.
for (initialization; condition; update) {
// Code to execute repeatedly
}
Example:
for (int i = 1; i <= 5; i++) {
System.out.println("Iteration: " + i);
}
b. while
Loop
The while
loop is used to execute a block of code repeatedly as long as a condition is true.
while (condition) {
// Code to execute repeatedly
}
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 is executed at least once, even if the condition is false.
do {
// Code to execute repeatedly
} while (condition);
Example:
int i = 1;
do {
System.out.println("Iteration: " + i);
i++;
} while (i <= 5);
3. Jump Statements
a. break
Statement
The break
statement is used to terminate a loop or switch
statement.
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break; // Exit the loop when i == 5
}
System.out.println("Iteration: " + i);
}
b. continue
Statement
The continue
statement is used to skip the current iteration of a loop and proceed to the next iteration.
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // Skip iteration when i == 3
}
System.out.println("Iteration: " + i);
}
c. return
Statement
The return
statement is used to exit a method and optionally return a value.
public int add(int a, int b) {
return a + b; // Exit the method and return the sum
}
Example Program
public class Main {
public static void main(String[] args) {
// if-else example
int age = 20;
if (age >= 18) {
System.out.println("You are eligible to vote.");
} else {
System.out.println("You are not eligible to vote.");
}
// switch 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");
}
// for loop example
for (int i = 1; i <= 5; i++) {
System.out.println("For Loop Iteration: " + i);
}
// while loop example
int j = 1;
while (j <= 5) {
System.out.println("While Loop Iteration: " + j);
j++;
}
// do-while loop example
int k = 1;
do {
System.out.println("Do-While Loop Iteration: " + k);
k++;
} while (k <= 5);
}
}
Summary
Control Statement | Description |
---|---|
if | Executes a block of code if a condition is true. |
if-else | Executes one block if the condition is true, another if false. |
if-else-if | Tests multiple conditions and executes the first true block. |
switch | Executes one block of code based on the value of an expression. |
for | Repeats a block of code for a fixed number of times. |
while | Repeats a block of code as long as a condition is true. |
do-while | Repeats a block of code at least once, then checks the condition. |
break | Terminates a loop or switch statement. |
continue | Skips the current iteration of a loop and proceeds to the next iteration. |
return | Exits a method and optionally returns a value. |
Understanding control statements is essential for writing efficient and structured Java programs.