Java If...Else

Java
int marks = 78;

if (marks >= 80) {
    System.out.println("Distinction");
} else if (marks >= 60) {
    System.out.println("First division");
} else if (marks >= 45) {
    System.out.println("Second division");
} else {
    System.out.println("Needs improvement");
}

Unlike PHP or JavaScript, the condition must be a boolean. There is no truthiness in Java:

Java
int x = 5;
// if (x) { }        ← compile error
if (x != 0) { }      // correct

That restriction also prevents the classic if (x = 5) typo — assignment returns int, not boolean, so it won't compile.

Nesting and combining

Java
if (age >= 18 && hasCitizenship) {
    System.out.println("Eligible to vote");
}

if (role.equals("admin") || role.equals("staff")) {
    System.out.println("Has access");
}

Note .equals() — not == — for Strings.

Guard clauses

Deeply nested ifs get hard to read. Return early instead:

Java
// harder to follow
public String check(User user) {
    if (user != null) {
        if (user.isActive()) {
            return "ok";
        } else {
            return "inactive";
        }
    } else {
        return "missing";
    }
}

// clearer
public String check(User user) {
    if (user == null)      return "missing";
    if (!user.isActive())  return "inactive";
    return "ok";
}

Ternary for simple choices

Java
String label = marks >= 40 ? "Pass" : "Fail";

Keep ternaries to one level — nested ternaries are hard to read and a plain if is almost always better.