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
- 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:
2. if-else Statement
- 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:
3. if-else-if Ladder
- 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:
4. Nested if Statement
- 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:
5. switch Statement
- Used for multiple conditions with discrete values
- 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:
6. switch Statement with String
- 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:
7. Ternary Operator as Conditional Shortcut
- 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:
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
- Conditional statements control the flow based on boolean conditions
- Types: if, if-else, nested if, if-else-if, switch
- Use ternary operator for simple conditions
- Helps in making decision-based programs