← Back to C++ Course | Chapter 1: Introduction & Basics | Lesson 5 of 15

C++ Syntax & Structure

Semicolons

Every complete statement in C++ must end with a semicolon, which tells the compiler exactly where one instruction stops and the next begins. Forgetting one is one of the most common beginner errors, and it usually causes the compiler to report an error on the following line instead of the one you actually missed.

Example: Semicolons

cpp
#include <iostream>

int main() {
	int x = 5;
	std::cout << x << std::endl;
	return 0;
}

Curly Braces

Curly braces { } group multiple statements into a single block, marking where a function body, loop, or if-statement begins and ends. Without braces, some constructs like if only apply to the single statement immediately following them, which is a frequent source of subtle bugs when a second line is added later.

Example: Curly Braces

cpp
#include <iostream>

int main() {
	if (true) {
		std::cout << "Inside the block" << std::endl;
		std::cout << "Both lines run" << std::endl;
	}
	return 0;
}

Tokens

A token is the smallest meaningful piece the compiler recognizes when it reads your source code — keywords like int, identifiers like variable names, literal values, and operators like + are all tokens. Understanding tokens helps explain compiler error messages, which often point at the specific token where parsing failed.

Example: Tokens

cpp
#include <iostream>

int main() {
	int total = 5 + 3; // int, total, =, 5, +, 3 are all tokens
	std::cout << total << std::endl;
	return 0;
}

Case Sensitivity

C++ treats uppercase and lowercase letters as completely different characters, so main and Main are not interchangeable — only lowercase main is recognized as your program's entry point. This same rule means total and Total can coexist as two entirely separate variables, which can cause confusing bugs if used carelessly.

Example: Case Sensitivity

cpp
#include <iostream>

int main() {
	int total = 1;
	int Total = 2; // a completely different variable from 'total'
	std::cout << total << " " << Total << std::endl;
	return 0;
}

Indentation

Indentation has no effect on how the compiler parses your code — braces alone define the structure — but consistent indentation is what makes nested loops and conditionals readable to a human. Most teams enforce a consistent indentation style precisely because the compiler's leniency here makes it easy for code to become unreadable without one.

Example: Indentation

cpp
#include <iostream>

int main() {
	for (int i = 0; i < 2; i++) {
		if (i == 0) {
			std::cout << "Consistent indentation aids readability" << std::endl;
		}
	}
	return 0;
}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.