Swipe
SwipeSwipe
Swipe Apps

Paths

Motivation

Information

C++ Program Structure · Lesson 12 of 25

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 are how C++ knows where a group of code starts and ends. 🧱

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;
}
The opening brace right after "int main()" and the closing brace at the end mark the block that is the body of main().
🤖
See how one block can live neatly inside another. 📦

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 block
Marked with comments: the "if" block sits entirely within the block belonging to main().
Quick check

What are curly braces { } used for in C++?

🤖
Miss a closing brace and the compiler will let you know. ⚠️

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.

True or false

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 }.

Fill in the blank

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 →