Chapter - 3 Decision Structures
3.1 The if Statement
Basic Syntax
Used for conditional execution of code blocks.
Syntax:
if (condition) {
   // Code to execute if condition is true
}
Example
int age = 20;
if (age >= 18) {
   System.out.println(“You are eligible to vote.”);
}
Explanation:
Condition (age >= 18) evaluates to true or false.
Executes the block if the condition is true.
3.2 The if-else Statement
Basic Syntax
Provides alternative actions based on a condition.
Syntax:
if (condition) {
   // Code if condition is true
} else {
   // Code if condition is false
}
Example
int num = 10;
if (num % 2 == 0) {
   System.out.println(“Even number”);
} else {
   System.out.println(“Odd number”);
}
3.3 Nested if Statements
Example
int age = 25;
if (age >= 18) {
   if (age >= 21) {
       System.out.println(“You are eligible to drink.”);
   } else {
       System.out.println(“You are an adult, but not old enough to drink.”);
   }
}
Explanation:
An if statement inside another if.
Evaluates conditions hierarchically.
3.4 Logical Operators
Operators
AND (&&): True if both operands are true.
OR (||): True if at least one operand is true.
NOT (!): Inverts a boolean value.
Example
int age = 20;
boolean hasID = true;
if (age >= 18 && hasID) {
 System.out.println(“Entry allowed.”);
}
3.5 The switch Statement
Syntax
switch (variable) {
   case value1:
       // Code block
       break;
   case value2:
       // Code block
       break;
   default:
       // Code block
       break;
}
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”);
}
Explanation:
Matches day to a case and executes the corresponding block.
default executes if no match is found.
3.6 Conditional Operator (?:)
Syntax
result = (condition) ? valueIfTrue : valueIfFalse;
Example
int age = 17;
String eligibility = (age >= 18) ? “Eligible to vote” : “Not eligible to vote”;
System.out.println(eligibility);
Summary
Decision structures control the flow of execution based on conditions.