Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Java Multiple Conditions in If Statement

Use &&, ||, and ! to combine multiple conditions in Java if statements. This article explains short-circuit evaluation, operator precedence, grouping with parentheses, and readability techniques.

~4 min readJava
#if statements#logical operators#short-circuit evaluation#Java syntax
Diagram showing Java if statement with multiple conditions combined using logical operators

To check several conditions before running a block, Java's if statement can combine boolean expressions with the logical operators && (AND), || (OR), and ! (NOT). These operators reduce multiple checks to a single true or false value, which the if statement uses to decide whether to execute the block.

Logical Operators for Multiple Conditions

The three logical operators form the basis of any multi-condition check. && returns true only when both operands are true. || returns true when at least one operand is true. The unary ! flips the boolean value of its operand. Here is a minimal example:

java
int age = 25; boolean hasLicense = true; if (age >= 18 && hasLicense) { System.out.println("You can drive."); }

The expression age >= 18 && hasLicense evaluates to true only if both conditions hold. If the driver is under 18 or lacks a license, the block is skipped. You can chain more than two conditions as well:

java
boolean isSuspended = false; if (age >= 18 && hasLicense && !isSuspended) { System.out.println("You can drive."); }

Here !isSuspended means the driver is not currently suspended. For chains of the same operator, Java evaluates from left to right, so you can build longer expressions. However, long chains can become hard to read, which is covered later.

Short-Circuit Evaluation and Its Effects

Java's && and || operators use short-circuit evaluation. This means the right-hand operand is evaluated only if the left-hand operand does not already determine the result. For &&, if the left side is false, the whole expression is false regardless of the right side, so the right side is not evaluated. For ||, if the left side is true, the whole expression is true, so the right side is skipped.

This behavior has two important consequences. First, it can avoid unnecessary work. Consider a check that calls a method only if a prior condition is met:

java
if (list != null && list.size() > 0) { // process list }

If list is null, the && short-circuits and list.size() is never called, preventing a NullPointerException. This is a common defensive pattern. Second, short-circuiting can change the behavior of expressions that have side effects. For example:

java
if (first() || second()) { // ... }

If first() returns true, second() is not invoked. If you rely on second() running for its side effect, this can be a subtle bug. Always be aware of whether your conditions have side effects, and prefer pure boolean expressions when possible.

For boolean operands, the non-short-circuit alternatives & and | evaluate both sides unconditionally, but they are rarely used in conditionals because they do not short-circuit and can be confused with integer bitwise operators.

Grouping Conditions with Parentheses

When you combine && and || in the same expression, precedence rules apply. && has higher precedence than ||, which can lead to results that are not obvious at first glance. For example:

java
boolean a = true; boolean b = false; boolean c = true; if (a || b && c) { // a is true, so the whole expression is true. }

Because && binds tighter, the expression is evaluated as a || (b && c). Without parentheses, the first version might be misread as (a || b) && c. Parentheses remove this ambiguity and let you override the default precedence when you need a different order:

java
if ((a || b) && c) { // Now a or b is checked first, then c is required. }

Using parentheses makes the group structure explicit, which is especially important when conditions come from different parts of a business rule. For instance, a discount might apply if the user is a member OR has both a coupon AND a purchase above a threshold:

java
if (isMember || (hasCoupon && purchaseAmount > 100)) { // apply discount }

The parentheses clearly show that the coupon and amount are grouped together, while the membership is an independent alternative.

Readability and Maintainability of Complex Conditions

Long chains of && and || can become difficult to read, especially when each condition is a method call or a comparison. A condition that spans multiple lines or contains many terms taxes the reader's working memory. Several techniques keep the code maintainable.

Extract the condition into a well-named boolean variable:

java
boolean isEligibleForDiscount = isMember || (hasCoupon && purchaseAmount > 100); if (isEligibleForDiscount) { // apply discount }

This separates the decision logic from the action, making the if statement read like plain English. Another approach is to extract a method that encapsulates the rule:

java
if (isEligibleForDiscount(customer, order)) { // apply discount } private boolean isEligibleForDiscount(Customer customer, Order order) { return customer.isMember() || (order.hasCoupon() && order.getTotal() > 100); }

This is particularly useful when the same condition appears in multiple places. It also makes unit testing easier because you can test the rule in isolation.

When the condition is too long for a single line, format it with line breaks and indentation. Java does not require special line continuation characters; you can break after an operator:

java
if (isMember || (hasCoupon && purchaseAmount > 100) || (isEmployee && yearsOfService > 5)) { // apply discount }

This style keeps each operand visible and reduces horizontal scrolling. Consistent formatting helps reviewers spot logical errors quickly.

Common Mistakes When Combining Conditions

One frequent mistake is using = instead of == in a comparison. In Java, = is assignment. Writing if (count = 0) will not compile because count = 0 is not a boolean expression. If the assigned value is boolean, the code may compile, but it assigns instead of comparing, so it is almost always a bug.

Another mistake is confusing && and || when negating a complex condition. De Morgan's laws state that !(A && B) is equivalent to !A || !B, and !(A || B) is equivalent to !A && !B. Misapplying these can invert the logic silently.

For example, to check that a user is not both an admin and an owner, you might write:

java
if (!(isAdmin && isOwner)) { // not both }

This is correct. De Morgan's equivalent is !isAdmin || !isOwner, meaning at least one of the two is false. A common error is to write !isAdmin && !isOwner, which means neither admin nor owner, a stronger condition that is not the same.

Try matching browser tools

All tools

Small utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Java guides

More in Java

All Java guides

The guides filed next to this one in Java, in library order.