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