Conditional Statements With Switch
Conditional Statements With Switch
Programming
Understanding Conditional
Statements with Java Examples
What Are Conditional Statements?
• Conditional statements allow programs to
make decisions based on conditions.
• Example in Java:
• int age = 20;
• if (age >= 18) {
• System.out.println("You are an adult");
• }
The if-else Statement
• Used when you want to execute an alternative
block if the condition is False:
• Example in Java:
• int age = 16;
• if (age >= 18) {
• System.out.println("You are an adult");
• } else {
• System.out.println("You are a minor");
The if-else-if Ladder
• Used when there are multiple conditions to
check:
• Example in Java:
• int score = 85;
• if (score >= 90) {
• System.out.println("Grade: A");
• } else if (score >= 80) {
• System.out.println("Grade: B");
Nested Conditional Statements
• Conditions inside other conditions for more
complex decisions:
• Example in Java:
• int age = 20;
• boolean hasLicense = true;
• if (age >= 18) {
• if (hasLicense) {
• System.out.println("You can drive");
Logical Operators in Conditions
• Logical operators help combine multiple
conditions:
• Example:
• if (age >= 18 && hasLicense) {
The Switch Statement
• An alternative to multiple if-else statements
for checking specific values:
• Example in Java:
• int day = 3;
• switch (day) {
• case 1:
• System.out.println("Monday");
• break;
Summary of Conditional
Statements
• - Conditional statements control program flow.
• - `if`, `if-else`, and `if-elif-else` handle different
conditions.
• - Nested conditions allow complex logic.
• - Logical operators (`&&`, `||`, `!`) help
combine conditions.
• - `switch` provides a structured way to check
multiple exact values.