Posts

Showing posts with the label switch in java

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 (n...