Java Control Statements: Switch & For Loop Essentials

Day 7 – Learning Java Full Stack 🚀 Today’s let's learn about two important control statements: 1.Switch Statements 2.For Loop Both are widely used to control the flow of execution in Java programs. 🔹 Switch Statement The switch statement is used when we want to compare a single value against multiple possible cases. Instead of writing multiple if-else conditions, switch makes the code cleaner and more readable. Syntax: switch(choice) { case label1: // statements break; case label2: // statements break; case label3: // statements break; default: // statements } 🔹 The break statement stops execution after a matching case. 🔹 The default block runs if none of the cases match. Example: int day = 2; 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: Tuesday 🔹 For Loop The for loop is used when we know how many times a block of code should execute. It is commonly used for counting, printing patterns, and iterating over values. Syntax: for(initialization; condition; operation) { // body } Initialization → starting value Condition → loop runs while this is true Operation → increment/decrement step Example: for(int i = 1; i <= 5; i++) { System.out.println(i); } Output: 1 2 3 4 5 📌 Key takeaway: Switch improves readability when handling multiple choices. For loop is powerful when repetition is required. Both are essential for writing structured and logical Java programs. #Java #JavaFullStack #SwitchStatement #ForLoop #ControlStatements #LearningInPublic #CoreJava

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories