Conditional Statements
Conditional statements allow programs to execute different code based on different conditions. This lesson covers conditional logic in Java.
if Statement
The if statement is the most basic conditional check.
Syntax
JAVA
if (condition) {
// Executed when condition is true
}
Example: Simple if
JAVA
public class IfDemo {
public static void main(String[] args) {
int age = 20;
if (age >= 18) {
System.out.println("You are an adult");
}
}
}
if-else Statement
When the condition is false, the code in the else block is executed.
Syntax
JAVA
if (condition) {
// Executed when condition is true
} else {
// Executed when condition is false
}
Example: if-else
JAVA
public class IfElseDemo {
public static void main(String[] args) {
int age = 15;
if (age >= 18) {
System.out.println("You are an adult");
} else {
System.out.println("You are a minor");
}
}
}
if-else if-else Statement
Used for multiple condition checks.
Syntax
JAVA
if (condition1) {
// Executed when condition1 is true
} else if (condition2) {
// Executed when condition2 is true
} else if (condition3) {
// Executed when condition3 is true
} else {
// Executed when none of the above conditions are met
}
Example: Grade Calculator
JAVA
public class GradeDemo {
public static void main(String[] args) {
int score = 85;
if (score >= 90) {
System.out.println("Excellent");
} else if (score >= 80) {
System.out.println("Good");
} else if (score >= 70) {
System.out.println("Average");
} else if (score >= 60) {
System.out.println("Pass");
} else {
System.out.println("Fail");
}
}
}
💡 Note: Conditions are evaluated from top to bottom. Once a condition is true, the corresponding code executes and no further conditions are checked.
Nested if
if statements can be nested inside each other.
Example: Nested if
JAVA
public class NestedIfDemo {
public static void main(String[] args) {
int age = 25;
boolean hasID = true;
if (age >= 18) {
if (hasID) {
System.out.println("Entry allowed");
} else {
System.out.println("Please show your ID");
}
} else {
System.out.println("Minors are not allowed");
}
}
}
switch Statement
The switch statement is ideal for checking a variable against multiple fixed values.
Syntax
JAVA
switch (variable) {
case value1:
// Executed when variable equals value1
break;
case value2:
// Executed when variable equals value2
break;
default:
// Executed when no cases match
}
Example: Day of the Week
JAVA
public class SwitchDemo {
public static void main(String[] args) {
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;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid day");
}
}
}
⚠️ Purpose of break: Without break, the program continues executing the next case. This is called "fall-through."
Using Fall-through
Sometimes fall-through is useful, such as checking for weekends:
JAVA
public class SwitchFallThrough {
public static void main(String[] args) {
int day = 6;
String type;
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5:
type = "Weekday";
break;
case 6:
case 7:
type = "Weekend";
break;
default:
type = "Invalid";
}
System.out.println("Day " + day + " is a " + type);
}
}
Enhanced switch (Java 14+)
Java 14 introduced an enhanced switch syntax that's more concise:
JAVA
public class EnhancedSwitch {
public static void main(String[] args) {
int day = 3;
String dayName = switch (day) {
case 1 -> "Monday";
case 2 -> "Tuesday";
case 3 -> "Wednesday";
case 4 -> "Thursday";
case 5 -> "Friday";
case 6 -> "Saturday";
case 7 -> "Sunday";
default -> "Invalid";
};
System.out.println(dayName); // Wednesday
}
}
Scanner for User Input
The Scanner class can capture keyboard input from users.
Usage Steps
JAVA
// 1. Import the Scanner class
import java.util.Scanner;
// 2. Create a Scanner object
Scanner scanner = new Scanner(System.in);
// 3. Read input
int num = scanner.nextInt(); // Read an integer
double d = scanner.nextDouble(); // Read a floating-point number
String s = scanner.nextLine(); // Read a line of text
// 4. Close the scanner
scanner.close();
Example: User Input Check
JAVA
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = scanner.nextInt();
if (age >= 18) {
System.out.println("You are an adult");
} else {
System.out.println("You are a minor");
}
scanner.close();
}
}
Example: Simple Calculator
JAVA
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first number: ");
double num1 = scanner.nextDouble();
System.out.print("Enter operator (+ - * /): ");
char operator = scanner.next().charAt(0);
System.out.print("Enter second number: ");
double num2 = scanner.nextDouble();
double result;
switch (operator) {
case '+':
result = num1 + num2;
System.out.println(num1 + " + " + num2 + " = " + result);
break;
case '-':
result = num1 - num2;
System.out.println(num1 + " - " + num2 + " = " + result);
break;
case '*':
result = num1 * num2;
System.out.println(num1 + " * " + num2 + " = " + result);
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
System.out.println(num1 + " / " + num2 + " = " + result);
} else {
System.out.println("Error: Cannot divide by zero");
}
break;
default:
System.out.println("Error: Unsupported operator");
}
scanner.close();
}
}
❓ Frequently Asked Questions
Q Are parentheses required for if conditions?
A Yes, conditions must be enclosed in parentheses. Even for a single statement, it's recommended to use curly braces for the code block.
Q What types does switch support?
A switch supports byte, short, int, char, String (Java 7+), and enum. It does not support long, float, or double.
Q Can break be omitted?
A Yes, but it will cause fall-through. If fall-through is intentional, add a comment to explain.
📖 Summary
- if is for single condition checks, if-else is for two choices, if-else if-else is for multiple choices
- switch is ideal for checking a variable against multiple fixed values
- break prevents switch fall-through
- Scanner can capture user keyboard input
📝 Exercises
- Leap year: Input a year and determine if it's a leap year
- Grade calculator: Input a score (0-100) and output the grade (Excellent/Good/Average/Pass/Fail)
- Calculator: Build a simple calculator using Scanner
Next Lesson
In the next lesson, we'll learn about Loops — making programs repeat certain operations.



