Java Decision Marking and Branching
Java decision-making
Java decision-making refers to how Java programs make choices or determine the flow of execution based on conditions. The most common structures for decision-making in Java include:
1. if Statement
2. if-else Statement
3. else if` Ladder
4. switch Statement
1. if Statement
The `if` statement evaluates a condition and executes a block of code if the condition is `true`.
Syntax:
if (condition) {
// code to be executed if condition is true
}
Example:
int age = 18;
if (age >= 18) {
System.out.println("You are an adult.");
}
2. if-else Statement
The `if-else` statement allows for two outcomes: one if the condition is `true`, and another if it is `false`.
if (condition) {
// code to be executed if condition is true
} else {
// code to be executed if condition is false
}
Example:
int age = 16;
if (age >= 18) {
System.out.println("You are an adult.");
} else {
System.out.println("You are a minor.");
}
3. else if` Ladder
The `else if` statement is used when there are multiple conditions to evaluate.
if (condition1) {
// code to be executed if condition1 is true
} else if (condition2) {
// code to be executed if condition2 is true
} else {
// code to be executed if all conditions are false
}
Example:
int score = 85;
if (score >= 90) {
System.out.println("Grade: A");
} else if (score >= 80) {
System.out.println("Grade: B");
} else {
System.out.println("Grade: C");
}
4.Nested `if` statement
In Java, a nested `if` statement is when you place one `if` statement inside another `if` (or `else if` or `else`) statement. This allows for testing multiple conditions in a hierarchical way, meaning the inner `if` is only evaluated if the outer `if` condition is true.
Syntax:
if (condition1) {
// Executes when condition1 is true
if (condition2) {
// Executes when both condition1 and condition2 are true
} else {
// Executes when condition1 is true but condition2 is false
}
} else {
// Executes when condition1 is false
}
Example: Age and Gender Classification
Let's say we want to check a person’s age and gender to classify them into different categories.
class Main {
public static void main(String[] args) {
int age = 25;
String gender = "Male";
// Outer if statement to check age
if (age >= 18) {
System.out.println("Adult");
// Nested if to check gender
if (gender.equals("Male")) {
System.out.println("Adult Male");
} else {
System.out.println("Adult Female");
}
} else {
System.out.println("Minor");
}
}
}
5. switch Statement
The `switch` statement is an alternative to the `if-else` structure, often used when comparing a variable to multiple possible values.
switch (variable) {
case value1:
// code to be executed if variable == value1
break;
case value2:
// code to be executed if variable == value2
break;
default:
// code to be executed if variable doesn't match any case
}
Example:
int day = 3;
switch (day) {
case 1:
System.out.println("Sunday");
break;
case 2:
System.out.println("Monday");
break;
case 3:
System.out.println("Tuesday");
break;
default:
System.out.println("Invalid day");
}
5. Ternary Operator ( ? : )The ternary operator is a shorthand for `if-else` statements.
variable = (condition) ? expression1 : expression2;
Example:
int age = 20;
String status = (age >= 18) ? "Adult" : "Minor";
System.out.println(status);
These structures help control the program flow, allowing Java applications to react to different inputs or conditions efficiently.
------------------------xxxxxxxxxxxxxxxxxxxx-------------------------
Comments
Post a Comment