Java Conditional Statements: if, else, else-if and switch
In our previous post, we learned about Java Operators and how to perform math, comparisons, and logic.
✨ Quick Summary
Now it’s time to learn something even more important: how Java makes decisions.
In Java, we use Conditional Statements to run code based on conditions.
In this blog, you will learn:
- ✅ if statement
- ✅ if-else statement
- ✅ else-if ladder
- ✅ nested if
- ✅ switch statement
1) if Statement
The if statement checks a condition. If the condition is true, the code inside it will run.
public class IfExample {
public static void main(String[] args) {
int age = 20;
if (age >= 18) {
System.out.println("You are eligible to vote!");
}
}
}
2) if-else Statement
The if-else statement runs one block if the condition is true, otherwise it runs the else block.
public class IfElseExample {
public static void main(String[] args) {
int number = 10;
if (number % 2 == 0) {
System.out.println("The number is Even");
} else {
System.out.println("The number is Odd");
}
}
}
3) else-if Ladder
The else-if ladder is used when you have multiple conditions.
Java checks conditions from top to bottom. The first true condition will execute.
public class ElseIfExample {
public static void main(String[] args) {
int marks = 85;
if (marks >= 90) {
System.out.println("Grade: A+");
} else if (marks >= 80) {
System.out.println("Grade: A");
} else if (marks >= 70) {
System.out.println("Grade: B");
} else if (marks >= 60) {
System.out.println("Grade: C");
} else {
System.out.println("Grade: Fail");
}
}
}
4) Nested if Statement
A nested if means an if statement inside another if statement.
public class NestedIfExample {
public static void main(String[] args) {
int age = 22;
boolean hasID = true;
if (age >= 18) {
if (hasID) {
System.out.println("Access Granted!");
} else {
System.out.println("ID is required!");
}
} else {
System.out.println("You are underage!");
}
}
}
5) switch Statement
The switch statement is used when you want to compare one value with multiple cases.
It is cleaner than writing many else-if conditions.
public class SwitchExample {
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;
default:
System.out.println("Invalid day");
}
}
}
Bonus: switch with String Example
Java also supports switch with Strings.
public class SwitchStringExample {
public static void main(String[] args) {
String role = "Admin";
switch (role) {
case "Admin":
System.out.println("Full access granted");
break;
case "User":
System.out.println("Limited access granted");
break;
default:
System.out.println("Guest access");
}
}
}
Conclusion
Conditional statements are one of the most important parts of Java programming because they allow your program to:
- ✅ Make decisions
- ✅ Control the flow of execution
- ✅ Build real-life logic
In the next post, we will learn about Java Loops (for, while, do-while) and how to repeat tasks in Java!
Another helpfull blog
ReplyDeleteWell explained
ReplyDelete