Conditional statements are one of the most important concepts in Java programming. They allow a program to make decisions based on specific conditions. Instead of executing every statement sequentially, Java uses conditional statements to determine which block of code should run according to the result of a condition.
Decision-making is used in almost every Java application, including:
- Login Systems
- Banking Applications
- Student Result Systems
- Online Shopping Websites
- ATM Software
- Employee Management Systems
- Hospital Management Systems
- Android Applications
- Enterprise Java Applications
Java provides several conditional statements:
ifif-elseelse-if ladder- Nested
if switch
These statements help developers control the program flow efficiently.
In this chapter, you’ll solve beginner-friendly and interview-oriented Java practice questions based on conditional statements. Each question includes a complete Java solution, sample input/output, explanation, and concepts covered to strengthen your Java programming skills. Java Conditional Statements practice questions with solutions help to understand the concepts.
1. Java Program to Check Whether a Number is Positive or Negative
Problem Statement
Write a Java program to check whether a given number is positive or negative using the if-else statement.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number;
System.out.print("Enter a Number: ");
number = scanner.nextInt();
if (number >= 0) {
System.out.println("Positive Number");
} else {
System.out.println("Negative Number");
}
scanner.close();
}
}
Sample Input
Enter a Number: 25
Sample Output
Positive Number
Explanation
The program checks whether the entered number is greater than or equal to zero.
- If the condition is true, it prints Positive Number.
- Otherwise, it prints Negative Number.
Concepts Covered
- if-else Statement
- Relational Operator
- Decision Making
- Scanner Class
2. Java Program to Check Whether a Number is Even or Odd
Problem Statement
Write a Java program to determine whether a given number is even or odd.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number;
System.out.print("Enter a Number: ");
number = scanner.nextInt();
if (number % 2 == 0) {
System.out.println("Even Number");
} else {
System.out.println("Odd Number");
}
scanner.close();
}
}
Sample Input
Enter a Number: 18
Sample Output
Even Number
Explanation
The modulus operator (%) checks the remainder after division by 2.
- Remainder = 0 → Even Number
- Otherwise → Odd Number
Concepts Covered
- if-else
- Modulus Operator
- Arithmetic Operators
3. Java Program to Find the Largest of Two Numbers
Problem Statement
Write a Java program to find the larger of two numbers using the if-else statement.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int firstNumber, secondNumber;
System.out.print("Enter First Number: ");
firstNumber = scanner.nextInt();
System.out.print("Enter Second Number: ");
secondNumber = scanner.nextInt();
if (firstNumber > secondNumber) {
System.out.println(firstNumber + " is Larger.");
} else if (secondNumber > firstNumber) {
System.out.println(secondNumber + " is Larger.");
} else {
System.out.println("Both Numbers are Equal.");
}
scanner.close();
}
}
Sample Input
Enter First Number: 45
Enter Second Number: 80
Sample Output
80 is Larger.
Explanation
The program compares both numbers using relational operators.
- If the first number is larger, it prints the first number.
- If the second number is larger, it prints the second number.
- Otherwise, both numbers are equal.
Concepts Covered
- if-else
- else-if
- Relational Operators
- Comparison
4. Java Program to Check Whether a Person is Eligible to Vote
Problem Statement
Write a Java program to determine whether a person is eligible to vote based on their age.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int age;
System.out.print("Enter Your Age: ");
age = scanner.nextInt();
if (age >= 18) {
System.out.println("Eligible to Vote");
} else {
System.out.println("Not Eligible to Vote");
}
scanner.close();
}
}
Sample Input
Enter Your Age: 20
Sample Output
Eligible to Vote
Explanation
The legal voting age in many countries is 18 years or above.
- If the entered age is 18 or greater, the person is eligible to vote.
- Otherwise, the person is not eligible.
Concepts Covered
- if-else Statement
- Relational Operators
- Decision Making
- User Input
5. Java Program to Check Whether a Year is a Leap Year
Problem Statement
Write a Java program to determine whether a given year is a leap year.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int year;
System.out.print("Enter Year: ");
year = scanner.nextInt();
if ((year % 400 == 0) ||
(year % 4 == 0 && year % 100 != 0)) {
System.out.println(year + " is a Leap Year.");
} else {
System.out.println(year + " is Not a Leap Year.");
}
scanner.close();
}
}
Sample Input
Enter Year: 2024
Sample Output
2024 is a Leap Year.
Explanation
A leap year follows these rules:
- Divisible by 400, or
- Divisible by 4 but not divisible by 100
Examples:
| Year | Result |
|---|---|
| 2024 | Leap Year |
| 2023 | Not Leap Year |
| 2000 | Leap Year |
| 1900 | Not Leap Year |
Concepts Covered
- Nested Conditions
- Logical Operators
- Modulus Operator
- Decision Making
6. Java Program to Check Whether a Character is a Vowel or Consonant
Problem Statement
Write a Java program to determine whether a given alphabet is a vowel or a consonant.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
char character;
System.out.print("Enter an Alphabet: ");
character = scanner.next().charAt(0);
if (character == 'a' || character == 'e' ||
character == 'i' || character == 'o' ||
character == 'u' || character == 'A' ||
character == 'E' || character == 'I' ||
character == 'O' || character == 'U') {
System.out.println("Vowel");
} else {
System.out.println("Consonant");
}
scanner.close();
}
}
Sample Input
Enter an Alphabet: A
Sample Output
Vowel
Explanation
The program compares the entered character with all vowels.
If it matches any vowel, it prints Vowel; otherwise, it prints Consonant.
Concepts Covered
- if-else Statement
- Logical OR Operator (
||) - Character Comparison
- Decision Making
7. Java Program to Find the Largest of Three Numbers
Problem Statement
Write a Java program to find the largest among three numbers.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int firstNumber, secondNumber, thirdNumber;
System.out.print("Enter First Number: ");
firstNumber = scanner.nextInt();
System.out.print("Enter Second Number: ");
secondNumber = scanner.nextInt();
System.out.print("Enter Third Number: ");
thirdNumber = scanner.nextInt();
if (firstNumber >= secondNumber &&
firstNumber >= thirdNumber) {
System.out.println("Largest Number = " + firstNumber);
} else if (secondNumber >= firstNumber &&
secondNumber >= thirdNumber) {
System.out.println("Largest Number = " + secondNumber);
} else {
System.out.println("Largest Number = " + thirdNumber);
}
scanner.close();
}
}
Sample Input
Enter First Number: 40
Enter Second Number: 85
Enter Third Number: 65
Sample Output
Largest Number = 85
Explanation
The program compares all three numbers using the if-else-if ladder and displays the largest value.
Concepts Covered
- if-else-if Ladder
- Logical AND (
&&) - Comparison Operators
8. Java Program to Check Whether a Number is Divisible by 5 and 11
Problem Statement
Write a Java program to determine whether a number is divisible by both 5 and 11.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number;
System.out.print("Enter a Number: ");
number = scanner.nextInt();
if (number % 5 == 0 &&
number % 11 == 0) {
System.out.println("Number is Divisible by 5 and 11.");
} else {
System.out.println("Number is Not Divisible by 5 and 11.");
}
scanner.close();
}
}
Sample Input
Enter a Number: 55
Sample Output
Number is Divisible by 5 and 11.
Explanation
The number must satisfy both conditions.
- Divisible by 5
- Divisible by 11
Only then will the program print the positive result.
Concepts Covered
- Logical AND
- Modulus Operator
- Decision Making
9. Java Program to Calculate Student Grade Using Else-if Ladder
Problem Statement
Write a Java program to calculate a student’s grade based on marks.
Grade Criteria
| Marks | Grade |
|---|---|
| 90–100 | A |
| 80–89 | B |
| 70–79 | C |
| 60–69 | D |
| Below 60 | F |
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int marks;
System.out.print("Enter Marks: ");
marks = scanner.nextInt();
if (marks >= 90) {
System.out.println("Grade A");
} else if (marks >= 80) {
System.out.println("Grade B");
} else if (marks >= 70) {
System.out.println("Grade C");
} else if (marks >= 60) {
System.out.println("Grade D");
} else {
System.out.println("Grade F");
}
scanner.close();
}
}
Sample Input
Enter Marks: 87
Sample Output
Grade B
Explanation
The program uses the else-if ladder to determine the appropriate grade according to the entered marks.
Concepts Covered
- else-if Ladder
- Decision Making
- Relational Operators
10. Java Program to Check Whether a Character is Alphabet, Digit, or Special Character
Problem Statement
Write a Java program to identify whether an entered character is an alphabet, digit, or special character.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
char character;
System.out.print("Enter a Character: ");
character = scanner.next().charAt(0);
if ((character >= 'A' && character <= 'Z') ||
(character >= 'a' && character <= 'z')) {
System.out.println("Alphabet");
} else if (character >= '0' &&
character <= '9') {
System.out.println("Digit");
} else {
System.out.println("Special Character");
}
scanner.close();
}
}
Sample Input
Enter a Character: @
Sample Output
Special Character
Explanation
The program checks:
- Whether the character falls within uppercase or lowercase alphabet ranges.
- Whether it is a numeric digit.
- Otherwise, it is treated as a special character.
Concepts Covered
- Character Comparison
- ASCII Values
- Logical Operators
- else-if Ladder
11. Java Program to Check Whether a Number is Positive, Negative, or Zero
Problem Statement
Write a Java program to determine whether a number is positive, negative, or zero.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number;
System.out.print("Enter a Number: ");
number = scanner.nextInt();
if (number > 0) {
System.out.println("Positive Number");
} else if (number < 0) {
System.out.println("Negative Number");
} else {
System.out.println("Zero");
}
scanner.close();
}
}
Sample Input
Enter a Number: 0
Sample Output
Zero
Explanation
The program checks the number using an else-if ladder.
- Greater than 0 → Positive
- Less than 0 → Negative
- Otherwise → Zero
Concepts Covered
- if-else-if Ladder
- Relational Operators
- Decision Making
12. Java Program to Find the Absolute Value of a Number
Problem Statement
Write a Java program to calculate the absolute value of a given number.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number;
System.out.print("Enter a Number: ");
number = scanner.nextInt();
if (number < 0) {
number = -number;
}
System.out.println("Absolute Value = " + number);
scanner.close();
}
}
Sample Input
Enter a Number: -45
Sample Output
Absolute Value = 45
Explanation
If the number is negative, the program multiplies it by -1 to make it positive.
Absolute value is always non-negative.
Concepts Covered
- if Statement
- Unary Minus Operator
- Decision Making
13. Java Program to Determine Electricity Bill Category
Problem Statement
Write a Java program to categorize electricity usage based on consumed units.
Conditions
| Units | Category |
|---|---|
| Less than 100 | Low Consumption |
| 100–300 | Medium Consumption |
| Above 300 | High Consumption |
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int units;
System.out.print("Enter Electricity Units: ");
units = scanner.nextInt();
if (units < 100) {
System.out.println("Low Consumption");
} else if (units <= 300) {
System.out.println("Medium Consumption");
} else {
System.out.println("High Consumption");
}
scanner.close();
}
}
Sample Input
Enter Electricity Units: 240
Sample Output
Medium Consumption
Explanation
The program classifies electricity usage using the else-if ladder according to the entered number of units.
Concepts Covered
- if-else-if
- Decision Making
- Relational Operators
14. Java Program to Demonstrate Nested if Statement
Problem Statement
Write a Java program to demonstrate the use of nested if statements.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int age;
double percentage;
System.out.print("Enter Age: ");
age = scanner.nextInt();
System.out.print("Enter Percentage: ");
percentage = scanner.nextDouble();
if (age >= 18) {
if (percentage >= 60) {
System.out.println("Eligible");
} else {
System.out.println("Percentage Criteria Not Met");
}
} else {
System.out.println("Age Criteria Not Met");
}
scanner.close();
}
}
Sample Input
Enter Age: 21
Enter Percentage: 78
Sample Output
Eligible
Explanation
A nested if statement means one if block is placed inside another if block.
The second condition is checked only if the first condition is true.
Concepts Covered
- Nested if
- Decision Making
- Logical Flow
15. Java Program to Demonstrate the switch Statement
Problem Statement
Write a Java program to display the day of the week using the switch statement.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int day;
System.out.print("Enter Day Number (1-7): ");
day = scanner.nextInt();
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid Day Number");
}
scanner.close();
}
}
Sample Input
Enter Day Number (1-7): 5
Sample Output
Friday
Explanation
The switch statement is used when multiple values need to be compared against a single variable.
The break statement prevents execution from continuing into the next case.
Concepts Covered
- switch Statement
- case Labels
- break Statement
- Default Case
Chapter Summary
In this chapter, you learned how Java Conditional Statements help a program make decisions based on different conditions. Conditional statements control the flow of execution by allowing specific blocks of code to run only when a condition is true.
You practiced solving real-world Java programs using:
ifStatementif-elseStatementelse-ifLadder- Nested
if switchStatement
These decision-making statements are used in almost every Java application, including banking systems, login modules, grading systems, ATM software, e-commerce websites, hospital management systems, Android applications, and enterprise software.
By completing these practice questions, you have built a strong understanding of Java decision-making, which will help you solve coding problems more efficiently and prepare for technical interviews.
Key Takeaways
- Conditional statements control program execution based on conditions.
- The
ifstatement executes code only when a condition is true. if-elsehandles two possible outcomes.- The
else-ifladder is used for multiple conditions. - Nested
ifstatements allow one condition to be checked inside another. - The
switchstatement is useful when comparing a variable against multiple fixed values. - Logical operators (
&&,||,!) improve decision-making. - Conditional statements are widely used in real-world Java applications.
- Understanding conditional statements is essential before learning loops and methods.
- Strong knowledge of decision-making improves coding and interview performance.
Frequently Asked Questions (FAQs)
1. What are conditional statements in Java?
Conditional statements allow a Java program to execute different blocks of code depending on whether a condition is true or false.
2. Which conditional statements are available in Java?
Java provides:
ifif-elseelse-if- Nested
if switch
3. What is the difference between if and if-else?
ifexecutes code only when the condition is true.if-elseexecutes one block if the condition is true and another block if the condition is false.
4. When should we use the switch statement?
The switch statement is best when comparing one variable against multiple fixed values.
Example:
switch(day)
{
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
default:
System.out.println("Invalid");
}
5. What is a nested if statement?
A nested if statement is an if statement placed inside another if statement. The inner condition is checked only if the outer condition is true.
6. What is an else-if ladder?
An else-if ladder checks multiple conditions one after another until one condition becomes true.
7. Why is the break statement used in a switch?
The break statement stops execution after a matching case. Without it, execution continues into the next case (fall-through).
8. Why are Java conditional statements important?
Conditional statements are used in:
- Login Systems
- Student Result Systems
- Banking Applications
- Online Shopping Websites
- ATM Software
- Android Apps
- Enterprise Applications
- Coding Interviews
They are one of the most frequently used concepts in Java programming.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
