Code Blocks
A free lesson from Swipe Coding · C++, level 1 · updated October 2026
By the Swipe team, founded by Colin · how we make lessons
Curly braces { } group multiple statements together into a shared "block" in C++. A block marks a clear beginning and end — for example, the body of a function like main().
int main() {
std::cout << "Hello, C++!";
return 0;
}Blocks can be nested inside one another, like a box inside another box. For example, the block belonging to an "if" statement can sit entirely inside the block of the function that contains it — it starts later and ends earlier than the surrounding block.
int main() { // Start of main's block
if (true) { // Start of the if block
std::cout << "Nested!";
} // End of the if block
return 0;
} // End of main's blockWhat are curly braces { } used for in C++?
Every opening brace { must eventually have a matching closing brace }. If one is missing, the compiler can't tell where a block ends, and it reports an error — even if the rest of the code is otherwise correct.
Every opening curly brace { needs a matching closing brace }.
As a reminder: main() is itself a block, and the individual statements from the previous lesson sit inside it. As programs grow, you'll see many more blocks nested inside one another — but the core rule never changes: every { needs a matching }.
A block that sits entirely within another block is said to be ____ inside it.
Keep your streak going in Swipe Coding
Swipe through every level of C++ on your iPhone — with XP, stars, streaks, a personal advisor and the full track beyond these free levels.
Get the Swipe apps →