Java Conditional Statements – Complete Guide with If, If-Else, Switch Examples


Learn how to use conditional statements in Java, including if, if-else, nested if, switch-case, and best practices with complete examples.

Conditional Statements in Java – Complete Detailed Tutorial

Conditional statements allow your program to make decisions based on certain conditions.

They control the flow of execution depending on whether a condition is true or false.

1. if Statement

  1. Executes a block of code only if condition is true

Syntax:


if(condition) {
// statements
}

Example:


int num = 10;
if(num > 0) {
System.out.println("Number is positive");
}

Output:


Number is positive

2. if-else Statement

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

Syntax:


if(condition) {
// if block
} else {
// else block
}

Example:


int num = -5;
if(num > 0) {
System.out.println("Positive number");
} else {
System.out.println("Negative number");
}

Output:


Negative number

3. if-else-if Ladder

  1. Used when there are multiple conditions

Syntax:


if(condition1) {
// block1
} else if(condition2) {
// block2
} else {
// block3
}

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

  1. An if statement inside another if statement

Syntax:


if(condition1) {
if(condition2) {
// statements
}
}

Example:


int age = 20;
int weight = 60;
if(age > 18) {
if(weight > 50) {
System.out.println("Eligible to donate blood");
}
}

Output:


Eligible to donate blood

5. switch Statement

  1. Used for multiple conditions with discrete values
  2. Can be used instead of multiple if-else

Syntax:


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

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

6. switch Statement with String

  1. Java supports String in switch from Java 7 onwards

Example:


String color = "Red";
switch(color) {
case "Red":
System.out.println("Stop");
break;
case "Green":
System.out.println("Go");
break;
default:
System.out.println("Invalid color");
}

Output:


Stop

7. Ternary Operator as Conditional Shortcut

  1. Short form of if-else

Syntax:


variable = (condition) ? value1 : value2;

Example:


int num = 10;
String result = (num % 2 == 0) ? "Even" : "Odd";
System.out.println(result);

Output:


Even

8. Example Program – Conditional Statements


import java.util.Scanner;

public class ConditionalDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter marks: ");
int marks = sc.nextInt();

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

// Using switch
System.out.print("Enter day number (1-7): ");
int day = sc.nextInt();
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"); break;
}
}
}

9. Summary

  1. Conditional statements control the flow based on boolean conditions
  2. Types: if, if-else, nested if, if-else-if, switch
  3. Use ternary operator for simple conditions
  4. Helps in making decision-based programs