if-else Statements

Conditional execution of code

Interview Relevant: Foundation for logic-based interview problems
6 min read

Conditional Execution in Java

The if-else statement allows your program to make decisions and execute different code paths based on conditions.

Basic Syntax

  • if: Executes code block if condition is true
  • else: Executes if the if-condition is false
  • else if: Tests additional conditions

āœ“ Key Point: Conditions must evaluate to boolean (true/false). Unlike C, Java doesn't allow integers in conditions.

Types of if Statements

1. Simple if

Executes code only when condition is true.

2. if-else

Provides alternative execution path when condition is false.

3. if-else-if Ladder

Tests multiple conditions sequentially.

4. Nested if

if statements inside other if statements.

āš ļø Best Practice: Avoid deeply nested if statements (more than 3 levels). Use early returns or switch to switch statements for cleaner code.

Short-Circuit Evaluation

Java uses short-circuit evaluation:

  • && stops if first condition is false
  • || stops if first condition is true

Code Examples

Basic if and if-else statements

java
1// Simple if statement
2int age = 18;
3if (age >= 18) {
4    System.out.println("You are an adult");
5}
6
7// if-else statement
8int number = 7;
9if (number % 2 == 0) {
10    System.out.println("Even number");
11} else {
12    System.out.println("Odd number");
13}

if-else-if ladder for multiple conditions

java
1// if-else-if ladder for grading
2int score = 85;
3char grade;
4
5if (score >= 90) {
6    grade = 'A';
7} else if (score >= 80) {
8    grade = 'B';
9} else if (score >= 70) {
10    grade = 'C';
11} else if (score >= 60) {
12    grade = 'D';
13} else {
14    grade = 'F';
15}
16
17System.out.println("Grade: " + grade);  // Output: Grade: B

Nested if statements and cleaner alternatives

java
1// Nested if statements
2int age = 25;
3boolean hasLicense = true;
4
5if (age >= 18) {
6    if (hasLicense) {
7        System.out.println("You can drive");
8    } else {
9        System.out.println("Get a license first");
10    }
11} else {
12    System.out.println("Too young to drive");
13}
14
15// Better approach using && operator
16if (age >= 18 && hasLicense) {
17    System.out.println("You can drive");
18}

Short-circuit evaluation prevents errors

java
1// Short-circuit evaluation
2String str = null;
3
4// Safe null check - second condition not evaluated if str is null
5if (str != null && str.length() > 0) {
6    System.out.println("String has content");
7}
8
9// Without short-circuit, this would throw NullPointerException
10// if (str.length() > 0 && str != null)  // WRONG ORDER!
11
12// Using | and & (no short-circuit - both conditions evaluated)
13int a = 5, b = 10;
14if (a > 0 | ++b > 10) {  // b is always incremented
15    System.out.println("b = " + b);  // b = 11
16}

Use Cases

  • Input validation and error checking
  • User authentication and authorization
  • Business logic implementation
  • Menu selection and navigation
  • Range checking and boundary conditions
  • Null safety checks

Common Mistakes to Avoid

  • Using = instead of == in conditions
  • Not using braces for single-line blocks (leads to bugs)
  • Comparing strings with == instead of .equals()
  • Not handling all possible cases
  • Too many nested if statements (use switch or polymorphism)
  • Forgetting that conditions must be boolean