Control statements in Java allow a program to make decisions, repeat tasks, and control the flow of execution based on conditions. Without control statements, a Java program would execute line by line with no flexibility. By using conditional statements and loops, developers can build intelligent, dynamic, and efficient programs.
The if statement is used to execute a block of code only when a specific condition is true. If the condition evaluates to false, the code inside the if block is skipped.
if (condition) {
// code to execute
}
int age = 20;
if (age >= 18) {
System.out.println("You are eligible to vote.");
}
If the value of age is 18 or above, the message is printed. Otherwise, nothing happens.
The if-else statement provides two execution paths. One block runs if the condition is true, and the other runs if the condition is false.
if (condition) {
// true block
} else {
// false block
}
int marks = 45;
if (marks >= 50) {
System.out.println("Pass");
} else {
System.out.println("Fail");
}
If marks are 50 or above, the student passes; otherwise, the student fails.
A nested if statement is an if statement inside another if. It is used when multiple conditions must be checked in a specific order.
if (condition1) {
if (condition2) {
// code
}
}
int age = 22;
boolean hasID = true;
if (age >= 18) {
if (hasID) {
System.out.println("Entry allowed.");
}
}
The message is printed only if both conditions are satisfied.
The switch-case statement is used to execute one block of code from multiple choices based on the value of a variable. It is often used as an alternative to long if-else-if chains.
switch (expression) {
case value1:
// code
break;
case value2:
// code
break;
default:
// code
}
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");
}
The program prints the day corresponding to the given number. The break statement prevents fall-through.
The ternary operator is a short-hand alternative to if-else. It evaluates a condition and returns one of two values.
condition ? value_if_true : value_if_false;
int number = 10;
String result = (number % 2 == 0) ? "Even" : "Odd";
System.out.println(result);
If the number is divisible by 2, it prints “Even”; otherwise, it prints “Odd”.
The for loop is used when the number of iterations is known in advance. It consists of initialization, condition, and increment/decrement.
for (initialization; condition; increment/decrement) {
// code
}
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
The loop prints numbers from 1 to 5.
The while loop executes a block of code as long as the condition is true. The condition is checked before each iteration.
while (condition) {
// code
}
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}
The loop prints numbers from 1 to 5.
The do-while loop executes the code at least once, even if the condition is false, because the condition is checked after execution.
do {
// code
} while (condition);
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 5);
The loop prints numbers from 1 to 5, guaranteeing at least one execution.
The break statement is used to terminate a loop or switch statement immediately.
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
System.out.println(i);
}
The loop stops when i becomes 3.
The continue statement skips the current iteration of the loop and moves to the next iteration.
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}
The number 3 is skipped, and the loop continues with the next value.