What are Decision Making statements?
Decision-making statements allow a program to execute certain blocks of code based on specific conditions.
1. if Statement: Executes a block of code if the condition is true.
Example : if Statement #
public class IfExample {
public static void main(String[] args) {
int age = 20;
if (age >= 18) {
System.out.println("You are eligible to vote.");
}
}
}
Output:
You are eligible to vote.
In this example, the condition age >= 18 is true, so the message is printed.
2. if-else Statement: Executes one block of code if the condition is true, and another block if the condition is false.
Example : if-else Statement #
public class IfElseExample {
public static void main(String[] args) {
int age = 16;
if (age >= 18) {
System.out.println("You are eligible to vote.");
} else {
System.out.println("You are not eligible to vote.");
}
}
}
Output:
You are not eligible to vote.
Here, since age >= 18 is false, the code in the else block is executed.
3. else-if Ladder: Checks multiple conditions sequentially and executes the corresponding block of code for the first condition that is true.
Example : else-if Ladder #
public class ElseIfExample {
public static void main(String[] args) {
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");
}
}
}
Output:
Grade B
The program checks conditions sequentially until it finds a true condition. Since marks >= 80 is true, “Grade B” is printed.
4. Nested if statements in Java are if statements placed inside another if statement. This structure is used to check multiple conditions, where each condition is dependent on the previous one.
Example : Nested-if #
This program determines the largest of three numbers using nested if statements.
public class LargestNumberExample {
public static void main(String[] args) {
int num1 = 10;
int num2 = 20;
int num3 = 15;
if (num1 >= num2) {
if (num1 >= num3) {
System.out.println("The largest number is: " + num1);
} else {
System.out.println("The largest number is: " + num3);
}
} else {
if (num2 >= num3) {
System.out.println("The largest number is: " + num2);
} else {
System.out.println("The largest number is: " + num3);
}
}
}
}
Output:
The largest number is: 20
This program demonstrates how nested if statements can be used to evaluate multiple dependent conditions and make decisions based on those conditions.
5. switch Statement: Evaluates a variable and executes a block of code based on the matching case.
Example : switch Statement #
public class SwitchExample {
public static void main(String[] args) {
int day = 3;
String dayName;
switch (day) {
case 1:
dayName = "Sunday";
break;
case 2:
dayName = "Monday";
break;
case 3:
dayName = "Tuesday";
break;
case 4:
dayName = "Wednesday";
break;
case 5:
dayName = "Thursday";
break;
case 6:
dayName = "Friday";
break;
case 7:
dayName = "Saturday";
break;
default:
dayName = "Invalid day";
break;
}
System.out.println("Day: " + dayName);
}
}
Output:
Day: Tuesday
The switch statement evaluates the day variable and executes the matching case.