Java Control Statements Explained

🚀 Starting My Java Learning Journey – Day 5 🔹 Topic: Control Statements in Java Control statements are used to control the flow of execution of a program based on certain conditions. In Java, the main control statements are: 1️⃣ if Statement The if statement executes a block of code only if the condition is true. Example: public class Main { public static void main(String[] args) { int num = 10; if (num > 0) { System.out.println("Number is positive"); } } } 2️⃣ if-else Statement The if-else statement executes one block of code if the condition is true and another block if it is false. Example: public class Main { public static void main(String[] args) { int num = -5; if (num > 0) { System.out.println("Positive number"); } else { System.out.println("Negative number"); } } } 3️⃣ switch Statement The switch statement is used when we want to compare one value with multiple possible cases. Example: public class Main { public static void main(String[] args) { int day = 2; switch(day) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; default: System.out.println("Other day"); } } } 💡 Key Point: Control statements help make programs dynamic and decision-based. #Java #JavaLearning #Programming #BackendDevelopment #CodingJourney #ControlStatements

To view or add a comment, sign in

Explore content categories