Sunday, 20 April 2025

If-Else Explained (JAVA)

 

What if your program needs to make choices? Like checking if someone is old enough to vote, or if a number is positive or negative?

That’s where if-else statements come in — they let your code decide what to do based on a condition.

//Basic If-Else Structure

if (condition) {

    // do this if condition is true

} else {

    // do this if condition is false

}



//Example 1 : Even or Odd

int number = 7;

if (number % 2 == 0) {
    System.out.println("The number is even.");
else {
    System.out.println("The number is odd.");
}

No comments:

Post a Comment

👨‍💻 How I Started Learning Java at 15 (and You Can Too)

  When I first heard of Java, I thought it was just something to do with Minecraft mods or Android apps. I didn’t realize it would become my...