Conditional Statements in Java

Conditional statements are one of the fundamental concepts in Java programming. They allow a program to make decisions and execute different blocks of code based on specific conditions.

Normally, a program executes statements sequentially. However, in real-world applications, we often need to perform different actions depending on the situation. For example, checking whether a student has passed an exam, verifying login credentials, calculating grades, or determining whether a person is eligible to vote.

Java provides conditional statements to handle these decision-making scenarios.

Types of Conditional Statements

Java supports the following conditional statements:

  • if
  • if-else
  • if-else-if ladder
  • Nested if
  • switch

1. if Statement

The if statement executes a block of code only when the given condition is true. If the condition is false, the block is skipped.

Syntax:

if (condition) { // Code executes if condition is true } 

Example:

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

Output:

Eligible to vote 

Here, Java checks whether the age is greater than or equal to 18. Since the condition is true, the statement inside the if block executes.

2. if-else Statement

The if-else statement is used when we need to execute one block of code if the condition is true and another block if it is false.

Syntax:

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

Example:

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

Output:

Odd number 

The modulus operator (%) returns the remainder. If the remainder is zero, the number is even; otherwise, it is odd.

3. if-else-if Ladder

The if-else-if ladder is used to check multiple conditions sequentially. When a condition becomes true, its corresponding block executes, and the remaining conditions are skipped.

Example:

int mark = 85; if (mark >= 90) { System.out.println("Grade A"); } else if (mark >= 80) { System.out.println("Grade B"); } else if (mark >= 70) { System.out.println("Grade C"); } else if (mark >= 50) { System.out.println("Grade D"); } else { System.out.println("Fail"); } 

Output:

Grade B 

Since the mark is 85, the second condition is true. Therefore, Grade B is printed.

The order of conditions is important. Conditions should be arranged carefully to avoid incorrect results.

4. Nested if Statement

A nested if means placing one if statement inside another if statement. It is useful when one condition must be satisfied before checking another condition.

Example:

String username = "admin"; String password = "1234"; if (username.equals("admin")) { if (password.equals("1234")) { System.out.println("Login successful"); } else { System.out.println("Incorrect password"); } } else { System.out.println("Incorrect username"); } 

Output:

Login successful 

First, the username is checked. If it is correct, Java checks the password. If both conditions are satisfied, the login is successful.

5. switch Statement

The switch statement is used to execute different blocks of code based on the value of an expression. It is useful when comparing a variable against multiple fixed values.

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; case 4: System.out.println("Thursday"); break; default: System.out.println("Invalid day"); } 

Output:

Wednesday 

Important Keywords

  • case – Defines a possible matching value.
  • break – Terminates the switch block and prevents fall-through.
  • default – Executes when no case matches.

Without break, Java may continue executing the following cases. This behavior is called fall-through.