programming
syntax
else if
coding
conditional statements

What is the correct syntax for 'else if'?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

The "else if" construct allows a program to choose between more than two execution paths by chaining conditions. The syntax varies across languages: C/C++/Java/JavaScript use else if, Python uses elif, Ruby uses elsif, and some languages use elseif (PHP, Visual Basic) or ELSIF (Ada, PL/SQL). Using the wrong keyword is a syntax error in every language. This article covers the correct syntax for all major programming languages.

C, C++, Java, JavaScript, TypeScript

c
1int score = 85;
2
3if (score >= 90) {
4    printf("A");
5} else if (score >= 80) {
6    printf("B");
7} else if (score >= 70) {
8    printf("C");
9} else {
10    printf("F");
11}

else if is two separate keywords. The braces are optional for single statements but always recommended.

Python

python
1score = 85
2
3if score >= 90:
4    print("A")
5elif score >= 80:
6    print("B")
7elif score >= 70:
8    print("C")
9else:
10    print("F")

Python uses elif (not else if). Writing else if creates a nested if inside the else block, which changes the structure:

python
1# This is NOT the same as elif
2if score >= 90:
3    print("A")
4else:
5    if score >= 80:  # This is a nested if, not a chained elif
6        print("B")

Ruby

ruby
1score = 85
2
3if score >= 90
4  puts "A"
5elsif score >= 80
6  puts "B"
7elsif score >= 70
8  puts "C"
9else
10  puts "F"
11end

Ruby uses elsif (not elif or else if).

PHP

php
1$score = 85;
2
3if ($score >= 90) {
4    echo "A";
5} elseif ($score >= 80) {
6    echo "B";
7} elseif ($score >= 70) {
8    echo "C";
9} else {
10    echo "F";
11}

PHP accepts both elseif (one word) and else if (two words) when using curly braces. However, in the alternative colon syntax, only elseif works:

php
1// Alternative syntax — only elseif works (not "else if")
2if ($score >= 90):
3    echo "A";
4elseif ($score >= 80):
5    echo "B";
6else:
7    echo "F";
8endif;

Go

go
1score := 85
2
3if score >= 90 {
4    fmt.Println("A")
5} else if score >= 80 {
6    fmt.Println("B")
7} else if score >= 70 {
8    fmt.Println("C")
9} else {
10    fmt.Println("F")
11}

Go uses else if. Braces are required. The else if must be on the same line as the closing } of the previous block.

Rust

rust
1let score = 85;
2
3if score >= 90 {
4    println!("A");
5} else if score >= 80 {
6    println!("B");
7} else if score >= 70 {
8    println!("C");
9} else {
10    println!("F");
11}
12
13// Rust if/else is an expression — can assign the result
14let grade = if score >= 90 { "A" }
15            else if score >= 80 { "B" }
16            else if score >= 70 { "C" }
17            else { "F" };

Swift

swift
1let score = 85
2
3if score >= 90 {
4    print("A")
5} else if score >= 80 {
6    print("B")
7} else if score >= 70 {
8    print("C")
9} else {
10    print("F")
11}

Kotlin

kotlin
1val score = 85
2
3// Standard if/else if
4if (score >= 90) {
5    println("A")
6} else if (score >= 80) {
7    println("B")
8} else {
9    println("F")
10}
11
12// Kotlin's when expression (preferred for multiple conditions)
13val grade = when {
14    score >= 90 -> "A"
15    score >= 80 -> "B"
16    score >= 70 -> "C"
17    else -> "F"
18}

Quick Reference Table

LanguageSyntaxNotes
C/C++/Javaelse ifTwo words, braces optional
JavaScript/TypeScriptelse ifTwo words
PythonelifOne word, colon required
RubyelsifOne word, end required
PHPelseif or else ifBoth work with braces; only elseif with colon syntax
Goelse ifBraces required, same line as }
Rustelse ifExpression-based
Swiftelse ifBraces required
Kotlinelse if or whenwhen preferred for multiple branches
Shell/BashelifFollowed by then, closed with fi
Visual BasicElseIfOne word, followed by Then

Common Pitfalls

  • Using elif in C/Java/JavaScript: These languages use else if (two words). elif is a syntax error. Conversely, using else if in Python creates an unintended nested structure instead of a flat chain.
  • Using elsif in Python or PHP: Ruby uses elsif, but Python uses elif and PHP uses elseif. These are three different keywords in three different languages. Mixing them up is a common source of syntax errors.
  • Missing braces causing dangling else: In C/Java, without braces, else if attaches to the nearest if. This can cause subtle logic bugs where the else branch executes unexpectedly. Always use braces for multi-branch conditionals.
  • Go else if on a new line: Go requires the else if to appear on the same line as the closing } of the previous block. Placing it on the next line causes a compile error due to Go's automatic semicolon insertion.
  • PHP colon syntax requiring elseif: In PHP's alternative syntax (if: ... endif;), only elseif (one word) is valid. Using else if (two words) causes a parse error in this specific syntax, even though both forms work with curly braces.

Summary

  • Most C-family languages (C, C++, Java, JavaScript, Go, Rust, Swift) use else if (two words)
  • Python uses elif, Ruby uses elsif, PHP uses elseif (or else if with braces)
  • Always use the language-specific keyword — the wrong one is a syntax error
  • Prefer when (Kotlin), match (Rust), or switch for many branches instead of long else if chains
  • Always use braces/blocks to avoid dangling-else ambiguity

Course illustration
Course illustration

All Rights Reserved.