- 1Java Exceptions
- 2Java Keywords
- 3Java abstract Keyword
- 4Java assert Keyword
- 5Java boolean Keyword
- 6Java break Keyword
- 7Java byte Keyword
- 8Java case Keyword
- 9Java catch Keyword
- 10Java char Keyword
- 11Java class Keyword
- 12Java const Keyword
- 13Java continue Keyword
- 14Java default Keyword
- 15Java do Keyword
- 16Java double Keyword
- 17Java else Keyword
- 18Java enum Keyword
- 19Java extends Keyword
- 20Java final Keyword
- 21Java finally Keyword
- 22Java float Keyword
- 23Java for Keyword
- 24Java goto Keyword
- 25Java if Keyword
- 26Java implements Keyword
- 27Java import Keyword
- 28Java instanceof Keyword
- 29Java int Keyword
- 30Java interface Keyword
- 31Java long Keyword
- 32Java native Keyword
- 33Java new Keyword
- 34Java null Keyword
- 35Java package Keyword
- 36Java private Keyword
- 37Java protected Keyword
- 38Java public Keyword
- 39Java return Keyword
- 40Java short Keyword
- 41Java static Keyword
- 42Java strictfp Keyword
- 43Java super Keyword
- 44Java switch Keyword
- 45Java synchronized Keyword
- 46Java this Keyword
- 47Java transient Keyword
- 48Java try Keyword
- 49Java void Keyword
- 50Java volatile Keyword
- 51Java while Keyword
- 52Java String Methods - Syntax and Description
- 53Java String
charAt()
method - 54Java String
codePointAt()
method - 55Java String
codePointBefore()
method - 56Java String
codePointCount()
method - 57Java String
compareTo()
method - 58Java String
compareToIgnoreCase()
method - 59Java String
concat()
method - 60Java String
contains()
method - 61Java String
contentEquals()
method - 62Java String
copyValueOf()
method - 63Java String
endsWith()
method - 64Java String
equals()
method - 65Java String
equalsIgnoreCase()
method - 66Java String
format()
method - 67Java String
getBytes()
method - 68Java String
getChars()
method - 69Java String
hashCode()
method - 70Java String
indexOf()
method - 71Java String
intern()
method - 72Java String
isEmpty()
method - 73Java String
join()
method - 74Java String
lastIndexOf()
method - 75Java String
length()
method - 76Java String
matches()
method - 77Java String
offsetByCodePoints()
method - 78Java String
regionMatches()
method - 79Java String
replace()
method - 80Java String
replaceAll()
method - 81Java String
replaceFirst()
method - 82Java String
split()
method - 83Java String
startsWith()
method - 84Java String
subSequence()
method - 85Java String
substring()
method - 86Java String
toCharArray()
method - 87Java String
toLowerCase()
method - 88Java String
toString()
method - 89Java String
toUpperCase()
method - 90Java String
trim()
method - 91Java String
valueOf()
method - 92Java ArrayList Methods - Complete Reference with Syntax and Description
- 93Java LinkedList Methods - Complete Reference with Syntax and Description
- 94Java HashMap Methods - Syntax and Descriptions
Java else Keyword
Usage and Examples
else
Keyword in Java
The else
keyword in Java is used to define a block of code that executes when the condition in the associated if
statement is false. It gives us a way to provide an alternate path of execution when the primary condition isn’t met.
Syntax of else
if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
Simple Example
public class ElseExample {
public static void main(String[] args) {
int number = 10;
if (number < 5) {
System.out.println("Number is less than 5");
} else {
System.out.println("Number is greater than or equal to 5");
}
}
}
Number is greater than or equal to 5
Since 10 is not less than 5, the condition in the if
block fails. Thus, the else
block gets executed.
Using else with User Input
import java.util.Scanner;
public class ElseWithInput {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = scanner.nextInt();
if (age >= 18) {
System.out.println("You are eligible to vote.");
} else {
System.out.println("You are not eligible to vote yet.");
}
}
}
Sample Output 1
Enter your age: 21
You are eligible to vote.
Sample Output 2
Enter your age: 16
You are not eligible to vote yet.
This shows how else
helps to create a fallback condition in real-world scenarios like input validation.
else without if – Is it Valid?
No, else
cannot be used independently. It must always be paired with an if
block. If used alone, the compiler will throw an error.
// Invalid usage
else {
System.out.println("This will cause a compilation error");
}
Compilation Error
ElseExample.java:3: error: 'else' without 'if'
Nested if-else with else
We can also use else
with nested conditions to create multi-way decision logic.
public class NestedElse {
public static void main(String[] args) {
int marks = 75;
if (marks >= 90) {
System.out.println("Grade: A");
} else if (marks >= 75) {
System.out.println("Grade: B");
} else if (marks >= 50) {
System.out.println("Grade: C");
} else {
System.out.println("Grade: F");
}
}
}
Grade: B
The conditions are evaluated from top to bottom. When marks >= 75
is true, that block executes, and the rest are ignored.
Common Mistakes Using else
- Using
else
without a correspondingif
- Adding a semicolon after
if
condition — this can lead to unexpected behavior
// Mistaken semicolon after if
if (value > 0); {
System.out.println("This block always executes!");
}
Why it’s wrong:
The semicolon ends the if
prematurely, so the block below is always executed regardless of the condition.