Understanding `if-else` in Java

Introduction

The if-else statement is one of the most fundamental decision-making structures in Java. It allows a program to execute a block of code based on a condition. If the condition is true, a specific block of code runs; otherwise, another block of code executes.

Syntax of if-else

if (condition) {
    // Code to execute if the condition is true
} else {
    // Code to execute if the condition is false
}
      

Key Points:

Types of if Statements

  1. Simple if Statement
  2. if-else Statement
  3. if-else if Ladder
  4. Nested if Statements

1. Simple if Statement

int number = 10;

if (number > 0) {
    System.out.println("The number is positive.");
}
      

Output:

The number is positive.

2. if-else Statement

int number = -5;

if (number >= 0) {
    System.out.println("The number is non-negative.");
} else {
    System.out.println("The number is negative.");
}
      

Output:

The number is negative.

3. if-else if Ladder

int marks = 85;

if (marks >= 90) {
    System.out.println("Grade: A+");
} else if (marks >= 80) {
    System.out.println("Grade: A");
} else if (marks >= 70) {
    System.out.println("Grade: B");
} else {
    System.out.println("Grade: C");
}
      

Output:

Grade: A

4. Nested if Statements

int age = 25;
boolean hasLicense = true;

if (age >= 18) {
    if (hasLicense) {
        System.out.println("You are eligible to drive.");
    } else {
        System.out.println("You need a driving license to drive.");
    }
} else {
    System.out.println("You are not old enough to drive.");
}
      

Output:

You are eligible to drive.

Common Examples of if-else

Example 1: Check if a Number is Even or Odd

int number = 15;

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

Example 2: Find the Largest of Two Numbers

int a = 10, b = 20;

if (a > b) {
    System.out.println(a + " is larger.");
} else {
    System.out.println(b + " is larger.");
}
      

Example 3: Check if a Year is a Leap Year

int year = 2024;

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
    System.out.println(year + " is a leap year.");
} else {
    System.out.println(year + " is not a leap year.");
}
      

Unsolved Problems

  1. Write a program to check if a number is positive, negative, or zero.
  2. Write a program to determine whether a character entered by the user is a vowel or a consonant.
  3. Write a program to find the smallest of three numbers using if-else.
  4. Write a program to check if a given number is divisible by both 5 and 11.
  5. Write a program to calculate the total price of a product after applying a discount based on the rules.
  6. Write a program to determine whether a given number is a three-digit number.
  7. Write a program to categorize temperature into cold, moderate, and hot.
  8. Write a program to check if a character is uppercase, lowercase, a digit, or a special character.
  9. Write a program to calculate electricity charges based on consumption.
  10. Write a program to display the grade of a student based on marks.