Java > Core Java > Control Flow > If-Else Statements
Simple If-Else Example: Determining Even or Odd
This code snippet demonstrates a basic if-else
statement in Java. It checks if a given number is even or odd and prints the corresponding message to the console.
Code Snippet
The code defines a class `EvenOddChecker` with a `main` method. Inside `main`, an integer variable `number` is initialized. The if
statement checks if the remainder of `number` divided by 2 is equal to 0. If it is, then `number` is even, and a message is printed accordingly. Otherwise, the `else` block is executed, indicating that `number` is odd.
public class EvenOddChecker {
public static void main(String[] args) {
int number = 10; // You can change this number to test different values
if (number % 2 == 0) {
System.out.println(number + " is an even number.");
} else {
System.out.println(number + " is an odd number.");
}
}
}
Concepts Behind the Snippet
The if-else
statement is a fundamental control flow construct in Java. It allows you to execute different blocks of code based on a boolean condition. The if
block is executed if the condition is true
, and the else
block is executed if the condition is false
. The modulo operator (%
) returns the remainder of a division. If a number is divisible by 2 (remainder is 0), it's even; otherwise, it's odd.
Real-Life Use Case
if-else
statements are ubiquitous in programming. Examples include:
Best Practices
When using if-else
statements:
{}
even for single-line blocks to improve readability and prevent errors.if-else if-else
chains for multiple conditions.if-else
statements as they can become difficult to understand and maintain.
Interview Tip
Be prepared to explain the purpose of if-else
statements and how they contribute to control flow. You might be asked to write a simple if-else
statement or to identify potential issues in a more complex one. Understanding boolean logic and common operators (==
, !=
, >
, <
, &&
, ||
) is crucial.
When to use them
Use if-else
statements when you need to execute different code paths depending on a specific condition. If you have only one condition to check, a simple if
statement might suffice. If you have multiple conditions, consider using if-else if-else
chains or a switch
statement (for certain cases).
Alternatives
Alternatives to if-else
statements include:
? :
): A concise way to write a simple if-else
statement in a single line.
FAQ
-
What happens if the `else` block is omitted?
If theelse
block is omitted, and the condition in theif
statement isfalse
, then the code execution simply continues to the next statement after theif
block. No special action is taken. -
Can I nest
if-else
statements?
Yes, you can nestif-else
statements within each other. However, deeply nested statements can become difficult to read and maintain. Consider refactoring the code if the nesting becomes too complex. -
What is the difference between `==` and `=`?
`==` is the equality operator, used to compare two values for equality. `=` is the assignment operator, used to assign a value to a variable. Using `=` instead of `==` in anif
condition is a common error that can lead to unexpected behavior.