---
schema_version: "1"
id: 5a2aa50922cf48d76b393fdcb7bf8844c358150b99b8b330c9086e2c8d648a85
title: Conditional (If-Else) Statements
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexcode/langCon_apex_if_else.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_if_else.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: 7f85c4b2a9ef87c95791d9411a7ada1789793a3e0ebf201909e9d13e3cadb8db
description: The conditional statement in Apex works similarly to Java.
---
# Conditional (If-Else) Statements

The conditional statement in Apex works similarly to Java.

if (\[Boolean\_condition\]) 
    // Statement 1
else
    // Statement 2

The else portion is always optional, and always groups with the closest if. For example:

Integer x, sign;
// Your code
if (x <= 0) **if** (x == 0) sign = 0; **else** sign = -1;

is equivalent to:

Integer x, sign;
// Your code
if (x <= 0) {
    **if** (x == 0) {
           sign = 0; 
    } **else**  {
           sign = -1;
    }
}

Repeated else if statements are also allowed. For example:

if (place == 1) {
    medal\_color = 'gold';
} else if (place == 2) {
    medal\_color = 'silver';
} else if (place == 3) {
    medal\_color = 'bronze';
} else {
    medal\_color = null;
}