How to Fix “expected a statement” in C++ (Visual Studio)
Short answer: Visual Studio found something that can’t start a statement, nearly always an else that isn’t attached to its if. Something between them ended the if early: a stray ;, a second line under the if without braces, or another else before it. Remove what’s in between, or add the missing braces.
| Where | What you see |
|---|---|
| Visual Studio editor (red squiggle) | expected a statement |
| Visual Studio build | C2181: illegal else without matching if |
| GCC (g++, MinGW, Code::Blocks) | 'else' without a previous 'if' |
| Clang (Xcode, macOS) | expected expression |
Hit “expected a statement”? It's a classic beginner trap, and there are others like it.
Free 1-page checklist: The 10 Mistakes Every C++ Beginner Makes. It shows the exact traps that slow down every C++ beginner, so you can avoid them from day one.
🔒 No spam. Unsubscribe anytime.
Cause 1: A Semicolon After the if
#include <iostream>
int main() {
int x = 3;
if (x > 5); {
std::cout << "Big\n";
}
else {
std::cout << "Small\n";
}
}
Visual Studio editor: expected a statement
MSVC build: error C2181: illegal else without matching if
warning C4390: ';': empty controlled statement found; is this the intent?
GCC: warning: suggest braces around empty body in an 'if' statement
error: 'else' without a previous 'if'
Clang: warning: if statement has empty body
error: expected expression
The ; after if (x > 5) is the if’s whole body: “if x is more than 5, do nothing”. The block underneath is just a block that always runs, and the else after it has no if to belong to. The warnings point straight at it.
Fix: delete the semicolon:
#include <iostream>
int main() {
int x = 3;
if (x > 5) {
std::cout << "Big\n";
}
else {
std::cout << "Small\n";
}
}
Small
Cause 2: A Semicolon After the Closing Brace
#include <iostream>
int main() {
int x = 3;
if (x > 5) {
std::cout << "Big\n";
};
else {
std::cout << "Small\n";
}
}
Visual Studio editor: expected a statement
MSVC build: error C2181: illegal else without matching if
GCC: error: 'else' without a previous 'if'
Clang: error: expected expression
Here the ; sits after }. It’s an empty statement of its own, and it separates the if from the else. Fix: delete it: } then else. Classes and structs need };, but if, loops and functions don’t.
Want the fundamentals explained properly? C++ Better Explained covers the core of C++ in plain English, with analogies and code diagrams, including the parts that trip beginners up most: pointers, memory and OOP.
Get the book — $27Cause 3: Two Lines Under an if Without Braces
#include <iostream>
int main() {
int x = 3;
if (x > 5)
std::cout << "Big\n";
std::cout << "Really big\n";
else
std::cout << "Small\n";
}
Visual Studio editor: expected a statement
MSVC build: error C2181: illegal else without matching if
GCC: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
error: 'else' without a previous 'if'
Clang: warning: misleading indentation; statement is not part of the previous 'if'
error: expected expression
Without braces, an if controls one statement. The indentation suggests two, but C++ ignores indentation: the second std::cout ends the if, and the else arrives too late. Fix: use braces whenever there’s more than one line:
#include <iostream>
int main() {
int x = 7;
if (x > 5) {
std::cout << "Big\n";
std::cout << "Really big\n";
}
else {
std::cout << "Small\n";
}
}
Big
Really big
Many programmers use braces on every if, even one-liners, to avoid this.
Cause 4: Two else Blocks
#include <iostream>
int main() {
int x = 3;
if (x > 5) {
std::cout << "Big\n";
}
else {
std::cout << "Small\n";
}
else {
std::cout << "Other\n";
}
}
Visual Studio editor: expected a statement
MSVC build: error C2181: illegal else without matching if
GCC: error: 'else' without a previous 'if'
An if can only have one plain else, and it must come last. For more choices, use else if:
#include <iostream>
int main() {
int x = 3;
if (x > 5) {
std::cout << "Big\n";
}
else if (x > 0) {
std::cout << "Small\n";
}
else {
std::cout << "Zero or less\n";
}
}
Small
See the conditionals tutorial for more on if, else if and else.
Cause 5: A Label at the End of a switch
#include <iostream>
int main() {
int choice = 3;
switch (choice) {
case 1:
std::cout << "One\n";
break;
default:
}
}
Visual Studio editor: expected a statement
MSVC build: error C5299: a label appearing at the end of a compound statement requires at least '/std:c++23preview'
GCC: error: expected primary-expression before '}' token
Clang: warning: label at end of compound statement is a C++23 extension
Before C++23, a label such as default: or case 2: must be followed by a statement, even an empty one. Fix: put break; after it:
#include <iostream>
int main() {
int choice = 3;
switch (choice) {
case 1:
std::cout << "One\n";
break;
default:
break;
}
std::cout << "Done\n";
}
Done
More in the switch statement guide.
Quick Reference
| You wrote | Fix |
|---|---|
if (x > 5); { | if (x > 5) { |
}; before else | } then else |
Two lines under an if with no braces | Wrap them in { } |
A second else | else if (...) for the middle case |
default: right before } | default: break; |
For other common messages, see C++ error messages explained.
Take Your C++ Further

If you'd like the fundamentals explained in one place, C++ Better Explained covers them in 87 plain-English pages, with analogies and code diagrams, including the parts that trip beginners up most: pointers, memory and OOP.
Get the book — $2787 pages · plain English · 30-day money-back guarantee
Related Articles
- C++ Error Messages Explained — the most common errors and what they mean.
- C++ Conditionals Tutorial — if, else if and else.
- How to Fix Expected ’;’ Errors — and where semicolons don’t belong.
- How to Fix
expected a '('— an if without its brackets. - C++ Switch Statement — case, default and break.