C++ Basics: Basic Syntax

Understand comments, statements, whitespace, and case sensitivity, the building blocks of every C++ program.

Comments

Comments are lines the compiler ignores completely. They exist solely for the programmer to explain intent or temporarily disable code. C++ supports two styles: single-line and multi-line.

Comment Syntax

Use comments to explain why code exists, not what it does, well-named identifiers already do that.

  • // text: single-line comment, everything after // on that line is ignored
  • /* text */: multi-line comment, spans as many lines as needed
  • Nested multi-line comments are not allowed in C++
  • Use single-line comments for quick notes; multi-line for larger explanations

Single-line and Multi-line Comments

C++

Both comment styles in one program.

Statements and Expressions

A statement is a complete instruction that ends with a semicolon. An expression is any combination of values and operators that evaluates to a result. Every expression can be turned into a statement by appending a semicolon.

Statement vs Expression

Statements perform actions; expressions produce values. Together they form the body of every function.

  • Expression: x + 5, a * b, true
  • Expression statement: x = 10; (assignment is also an expression)
  • Compound statement: a block wrapped in { } containing multiple statements
  • Missing a semicolon at the end of a statement is one of the most common syntax errors

Statements and Expressions

C++

Declaration, assignment, and output as separate statements.

Whitespace in C++

Whitespace refers to spaces, tabs, and newlines. The C++ compiler ignores extra whitespace between tokens, so you can use it freely to make code readable. The one exception: whitespace inside a string literal is preserved exactly.

Whitespace Rules

The compiler only cares that tokens are separated; indentation style is purely a convention for humans.

  • Spaces and tabs between tokens are ignored by the compiler
  • Newlines are also whitespace and do not end a statement (semicolons do)
  • Whitespace inside string literals is preserved: "Hello World" keeps three spaces
  • Consistent indentation (2 or 4 spaces) makes code far easier to read and debug

Whitespace is Flexible

C++

Extra spaces do not change behavior, but they do affect readability.

Case Sensitivity

C++ is fully case-sensitive. int and Int are completely different tokens. All keywords are lowercase; using the wrong case on a keyword will produce a compile error.

Case Sensitivity

Variable, function, and keyword names must match in case exactly every time they appear.

  • cout works; Cout or COUT do not
  • main is required lowercase; Main() is not the program entry point
  • Variable score and Score are two distinct variables
  • Convention: variables and functions use camelCase or snake_case; types use PascalCase

Case Sensitivity in Practice

C++

score and Score are two separate variables.

Knowledge Check

1. Which symbol starts a single-line comment in C++?

2. What character ends most C++ statements?

3. Is C++ case-sensitive?

4. Which of these is a multi-line comment?

5. What does whitespace do in C++ source code?