C

Core Java tutorial for beginners

Clean • Professional

Core Java Conditional Statements – if, if-else & switch

4 minute

Java Conditional Statements

Conditional statements are a key part of Core Java programming.

They help you make decisions in your code — executing different actions based on certain conditions.


What Are Conditional Statements?

Conditional statements allow your program to check conditions (like true or false) and decide which block of code to execute.

For example,

“If the user is above 18, print ‘Eligible to vote’, else print ‘Not eligible’.”


Types of Conditional Statements in Java

learn code with durgesh images

TypeDescription
if statementExecutes a block only if condition is true
if-else statementExecutes one block if true, another if false
if-else-if ladderChecks multiple conditions sequentially
nested ifif inside another if
switch statementSelects one case from multiple options

1. if Statement

The simplest conditional form — checks a condition and executes code if it’s true.

if (condition) {
    // code to execute if condition is true
}

Example:

int age = 20;
if (age >= 18) {
    System.out.println("You are eligible to vote.");
}

Output:

You are eligible to vote.

2. if-else Statement

Executes one block if the condition is true, another block if false.

if (condition) {
    // code if true
} else {
    // code if false
}

Example:

int number = 10;
if (number % 2 == 0) {
    System.out.println("Even Number");
} else {
    System.out.println("Odd Number");
}

Output:

Even Number

3. if-else-if Ladder

Used when you have multiple conditions to check.

if (condition1) {
    // code
} else if (condition2) {
    // code
} else {
    // default code
}

Example:

int marks = 85;

if (marks >= 90) {
    System.out.println("Grade A");
} else if (marks >= 75) {
    System.out.println("Grade B");
} else if (marks >= 50) {
    System.out.println("Grade C");
} else {
    System.out.println("Fail");
}

Output:

Grade B

4. Nested if Statement

You can use one if statement inside another.

It’s useful when multiple conditions depend on each other.

Example:

int age = 25;
boolean hasID = true;

if (age >= 18) {
    if (hasID) {
        System.out.println("Access Granted");
    } else {
        System.out.println("ID Required");
    }
} else {
    System.out.println("Underage");
}

Output:

Access Granted

5. switch Statement

switch is used to select one option from many.

It’s more readable than multiple if-else blocks.

Syntax:

switch (expression) {
    case value1:
        // code
        break;
    case value2:
        // code
        break;
    default:
        // code
}

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");
}

Output:

Wednesday

Java 14+ Enhancement – Switch Expressions

Modern Java allows switch as an expression, returning a value directly.

Example:

int day = 2;
String dayName = switch (day) {
    case 1 -> "Monday";
    case 2 -> "Tuesday";
    case 3 -> "Wednesday";
    default -> "Invalid";
};
System.out.println(dayName);

Output:

Tuesday

Example: Conditional Statements in Action

import java.util.Scanner;

public class ConditionalExample {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter your score: ");
        int score = sc.nextInt();

        if (score >= 90) {
            System.out.println("Excellent!");
        } else if (score >= 75) {
            System.out.println("Good job!");
        } else if (score >= 50) {
            System.out.println("You passed!");
        } else {
            System.out.println("Try again!");
        }

        sc.close();
    }
}

Output Example:

Enter your score: 82
Good job!

🏆 Top 5 Java Interview Questions - Java Conditional Statements

1. What is the difference between if and switch?

Answer:

Featureif Statementswitch Statement
UsageEvaluates boolean expressionsEvaluates a single expression/value
Type of ConditionsComplex conditions with relational/logical operatorsSingle variable/value with multiple discrete cases
Data TypesAny boolean expressionint, byte, short, char, String (Java 7+), enums
SyntaxCan be multiple if-else if-elsecase statements with optional default
PerformanceSlightly slower for multiple discrete valuesFaster for large number of discrete values

2. Can we use String in switch statement?

Answer:

  • Yes, starting from Java 7, String can be used in a switch statement.
  • Example:
String fruit = "Apple";
switch(fruit) {
    case "Apple": System.out.println("Red"); break;
    case "Banana": System.out.println("Yellow"); break;
    default: System.out.println("Unknown");
}

3. What happens if we forget to use break in a switch?

Answer:

  • If break is omitted, control falls through to the next case(s) until a break or the end of the switch.
  • Example:
int day = 2;
switch(day) {
    case 1: System.out.println("Monday");
    case 2: System.out.println("Tuesday");
    case 3: System.out.println("Wednesday");
}

Output:

Tuesday
Wednesday

This is called fall-through behavior.


4. What is a nested if statement?

Answer:

  • A nested if statement is an if or if-else block inside another if or else block.
  • Example:
int num = 10;
if(num > 0) {
    if(num % 2 == 0) {
        System.out.println("Positive even number");
    }
}

5. What are switch expressions introduced in Java 14?

Answer:

  • Switch expressions allow the switch to return a value, not just execute statements.
  • They use > syntax and do not require break.
  • Example:
int day = 3;
String dayName = switch(day) {
    case 1 -> "Monday";
    case 2 -> "Tuesday";
    case 3 -> "Wednesday";
    default -> "Unknown";
};
System.out.println(dayName); // Output: Wednesday

Benefits: More concise, avoids fall-through errors, can directly assign the result to a variable.

Article 0 of 0