What is the correct syntax for 'else if'?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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
else if is two separate keywords. The braces are optional for single statements but always recommended.
Python
Python uses elif (not else if). Writing else if creates a nested if inside the else block, which changes the structure:
Ruby
Ruby uses elsif (not elif or else if).
PHP
PHP accepts both elseif (one word) and else if (two words) when using curly braces. However, in the alternative colon syntax, only elseif works:
Go
Go uses else if. Braces are required. The else if must be on the same line as the closing } of the previous block.
Rust
Swift
Kotlin
Quick Reference Table
| Language | Syntax | Notes |
| C/C++/Java | else if | Two words, braces optional |
| JavaScript/TypeScript | else if | Two words |
| Python | elif | One word, colon required |
| Ruby | elsif | One word, end required |
| PHP | elseif or else if | Both work with braces; only elseif with colon syntax |
| Go | else if | Braces required, same line as } |
| Rust | else if | Expression-based |
| Swift | else if | Braces required |
| Kotlin | else if or when | when preferred for multiple branches |
| Shell/Bash | elif | Followed by then, closed with fi |
| Visual Basic | ElseIf | One word, followed by Then |
Common Pitfalls
- Using
elifin C/Java/JavaScript: These languages useelse if(two words).elifis a syntax error. Conversely, usingelse ifin Python creates an unintended nested structure instead of a flat chain. - Using
elsifin Python or PHP: Ruby useselsif, but Python useselifand PHP useselseif. 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 ifattaches to the nearestif. This can cause subtle logic bugs where the else branch executes unexpectedly. Always use braces for multi-branch conditionals. - Go
else ifon a new line: Go requires theelse ifto 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;), onlyelseif(one word) is valid. Usingelse 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 useselsif, PHP useselseif(orelse ifwith braces) - Always use the language-specific keyword — the wrong one is a syntax error
- Prefer
when(Kotlin),match(Rust), orswitchfor many branches instead of longelse ifchains - Always use braces/blocks to avoid dangling-else ambiguity
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.