R
Rishtaara
Java Fundamentals
Lesson 6 of 36Article14 min

Booleans & If/Else

boolean holds only true or false. Use it for yes/no decisions — login valid, age eligible, stock available.

Booleans — true or false

boolean holds only true or false. Use it for yes/no decisions — login valid, age eligible, stock available.

Comparison and logical operators produce boolean results.

Real-life example: A light switch is boolean — ON (true) or OFF (false). No maybe, no half-on.

Boolean variables
boolean isLoggedIn = true;
boolean hasPermission = false;
System.out.println(isLoggedIn && hasPermission); // false
System.out.println(isLoggedIn || hasPermission); // true

If/Else — making decisions

if (condition) { ... } runs when condition is true. else if adds more checks. else runs when nothing matched.

Use curly braces even for one line — it prevents bugs when you add more lines later.

Real-life example: If it rains, take umbrella. Else if sunny, wear hat. Else, just go out. Your code picks one path.

if, else if, else
int marks = 85;

if (marks >= 90) {
    System.out.println("Grade A");
} else if (marks >= 75) {
    System.out.println("Grade B");
} else {
    System.out.println("Keep practicing");
}