Allaka Yamuna lakshmi’s Post

🚀 Day 4/100 — If-Else & Switch in Java 🚦 Conditions are the brain of a program. They help your program make decisions based on different situations. In Java, the most common decision-making statements are if, else if, else, and switch. 🔹 If-Else Statement Used when a program needs to execute code based on a condition. Example: int age = 20; if(age >= 18){ System.out.println("Eligible to vote"); }else{ System.out.println("Not eligible to vote"); } ✔ If the condition is true, the first block runs. ✔ If it's false, the else block runs. 🔹 Else If Ladder Used when there are multiple conditions. ⚠️ Important: Java checks conditions top to bottom, and once one condition becomes true, the rest are skipped. Example: int marks = 85; if(marks >= 90){ System.out.println("Grade A"); } else if(marks >= 75){ System.out.println("Grade B"); } else if(marks >= 60){ System.out.println("Grade C"); } else{ System.out.println("Grade D"); } 🔹 Switch Statement Used when comparing one variable with multiple values. 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"); } ⚠️ Important: If you forget break, Java will execute all cases after that one (called fall-through). Example without break: int num = 1; switch(num){ case 1: System.out.println("One"); case 2: System.out.println("Two"); case 3: System.out.println("Three"); } Output: One Two Three 🔴 Mini Example — Pass or Fail int marks = 40; if(marks >= 35){ System.out.println("Pass"); }else{ System.out.println("Fail"); } 🎯 Challenge: Build a Grade Calculator using if-else. Example logic: 90+ → Grade A 75–89 → Grade B 60–74 → Grade C Below 60 → Grade D Drop your solution in the comments 👇 #Java #CoreJava #100DaysOfCode #JavaLearning #ProgrammingJourney

  • graphical user interface

To view or add a comment, sign in

Explore content categories