C++ · Chapter 4 of 49

C++ Syntax

C++ statements end with a semicolon `;`, and blocks of code are grouped using curly braces `{ }`. Unlike Python, indentation is only for readability — it has no effect on how code runs.

C++ is case-sensitive, so `Value` and `value` are different identifiers. Whitespace between tokens is generally ignored by the compiler.

Statements and semicolons

Forgetting a semicolon is one of the most common beginner errors and causes a compile error. Each statement — a declaration, assignment, or function call — needs one.

Blocks with braces

Curly braces `{}` group statements into a block, used for functions, loops, conditionals and classes. Braces can be nested to any depth.

Example 1 (cpp)
#include <iostream>
int main() {
    int x = 5;
    if (x > 0) {
        std::cout << "positive";
    }
}
Output
positive

The if-block is grouped with braces; each statement ends in a semicolon.

Key points

  • Every statement ends with a semicolon.
  • Curly braces {} define code blocks.
  • C++ is case-sensitive.
  • Indentation is for humans, not the compiler.
💡 Note: A missing semicolon often produces a confusing error on the *next* line — always check the line above first.

📝 Quick Quiz

1. What ends a C++ statement?

2. Curly braces {} are used to:

3. Is C++ case-sensitive?