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

🚀 Top 3 Java Projects Every Beginner Should Build (With Code Ideas)

 Tired of just printing "Hello World"? It’s time to build something real. 💻 If you're learning Java and want to stand out, ...