
Java is a programming language that has been widely used in the software industry for a long time, and it remains popular due to the security it offers for applications built with it.
Most programs need to make decisions. A login system must check whether a username and password are correct. A banking application must verify that an account has sufficient balance before allowing a withdrawal. A student management system may assign grades based on marks.
To perform these tasks, Java uses conditional statements. A conditional statement evaluates a condition and executes different blocks of code depending on whether that condition is true or false.
In this chapter, you will learn how to make decisions in Java using if, if-else, and else-if statements.
A condition is an expression that evaluates to either true or false.
Examples
age >= 18 marks > 90 balance < withdrawAmount
The result of each expression is either:
true false
Conditional statements use these results to determine which block of code should execute.
The if statement executes a block of code only when a condition is true.
Syntax
if(condition) {
// code to execute
}
Example
int age = 18;
if(age >= 18) {
System.out.println("Eligible to vote");
}
Output
Eligible to vote
Since the condition age >= 18 evaluates to true, the code inside the if block is executed.
In many situations, there are two possible outcomes. If the condition is true, one block of code executes. If the condition is false, another block executes.
Syntax
if(condition) {
// code if true
}
else {
// code if false
}
Example
int age = 16;
if(age >= 18) {
System.out.println("Eligible to vote");
}
else {
System.out.println("Not eligible to vote");
}
Output
Not eligible to vote
Since the condition is false, Java skips the if block and executes the else block.
When multiple conditions need to be checked, use else-if.
Java evaluates conditions from top to bottom. As soon as one condition becomes true, the remaining conditions are skipped.
Example
int marks = 85;
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 {
System.out.println("Grade D");
}
Output
Grade B
Since 85 is greater than or equal to 80, the second condition becomes true and Java prints Grade B.
Conditions are usually created using comparison operators.
Example
int age = 20; System.out.println(age >= 18);
Output
true
The expression evaluates to true because 20 is greater than 18.
Logical operators allow multiple conditions to be combined into a single expression.
Returns true only when both conditions are true.
Example
int age = 20;
boolean citizen = true;
if(age >= 18 && citizen) {
System.out.println("Eligible");
}
Output
Eligible
Returns true if at least one condition is true.
Example
int marks = 95;
if(marks >= 90 || marks == 90) {
System.out.println("Excellent");
}
Output
Excellent
Reverses a boolean value.
Example
boolean isLoggedIn = false;
if(!isLoggedIn) {
System.out.println("Please login");
}
Output
Please login
Example
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = sc.nextInt();
if(age >= 18) {
System.out.println("You are eligible to vote");
}
else {
System.out.println("You are not eligible to vote");
}
}
}
Input
20
Output
You are eligible to vote
Explanation
age.18.An if statement can be placed inside another if statement.
This is known as a nested if.
Example
int age = 20;
boolean hasLicense = true;
if(age >= 18) {
if(hasLicense) {
System.out.println("You can drive");
}
}
Output
You can drive
Both conditions must be true for the message to be displayed.
= Instead of ==Wrong
if(age = 18)
Correct
if(age == 18)
Explanation
= assigns a value.== compares two values.Wrong
if(age >= 18)
System.out.println("Eligible");
System.out.println("Welcome");
Only the first statement belongs to the if.
Correct
if(age >= 18) {
System.out.println("Eligible");
System.out.println("Welcome");
}
true or false.if executes code when a condition is true.if-else provides two possible outcomes.else-if is used when multiple conditions need to be checked.