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:
- The condition inside the
if
statement must evaluate to a boolean (true
orfalse
). - The
else
block is optional and is executed only when the condition in theif
block is false.
Types of if
Statements
- Simple
if
Statement if-else
Statementif-else if
Ladder- 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
- Write a program to check if a number is positive, negative, or zero.
- Write a program to determine whether a character entered by the user is a vowel or a consonant.
- Write a program to find the smallest of three numbers using
if-else
. - Write a program to check if a given number is divisible by both 5 and 11.
- Write a program to calculate the total price of a product after applying a discount based on the rules.
- Write a program to determine whether a given number is a three-digit number.
- Write a program to categorize temperature into cold, moderate, and hot.
- Write a program to check if a character is uppercase, lowercase, a digit, or a special character.
- Write a program to calculate electricity charges based on consumption.
- Write a program to display the grade of a student based on marks.