Basics
1.2 — Statements and the Structure of a Program¶
A C++ program is a sequence of instructions that the computer executes to achieve a specific result.
-
Statements: The smallest independent unit of a C++ program. They are instructions to the computer to perform a specific action. Most statements in C++ end with a semicolon (
;). -
Functions: A collection of statements that execute sequentially. Every C++ program must have a function named
main(), which serves as the entry point for execution. -
Syntax vs. Semantics:
-
Syntax: The rules that govern how symbols are arranged to create a valid program (the "grammar").
-
Semantics: The meaning of the statements. A program can be syntactically correct but semantically wrong (it runs but does the wrong thing).
-
1.3 — Comments¶
Comments are notes written by the programmer to document the code. They are ignored by the compiler.
-
Single-line comments (
//): Everything from the//to the end of the line is ignored. -
Multi-line comments (
/* */): Everything between the stars is ignored. These cannot be nested. -
Best Practice: Use comments to explain why the code is doing something, rather than what it is doing (which should be clear from the code itself).
1.4 — Introduction to Objects and Variables¶
-
Objects: A region of storage (usually RAM) that has a value and other associated properties.
-
Variables: A named object. We use names (identifiers) to refer to specific memory locations.
-
Data Types: Tells the compiler what kind of value the variable will hold (e.g.,
intfor integers). -
Definition/Instantiation: The process of creating the object in memory.
- Example:
int x; // Define a variable named x of type integer
- Example:
1.5 — Variable Assignment and Initialization¶
Once a variable is defined, it needs a value.
-
Assignment: Using the
=operator to give a value to an already existing variable. -
Initialization: Giving a variable a value at the moment of creation.
-
Types of Initialization:
-
Copy Initialization:
int x = 5; -
Direct Initialization:
int x(5); -
List Initialization (Uniform):
int x{5};(Preferred in modern C++ because it prevents "narrowing conversions" and is more consistent).
-
1.6 — Introduction to iostream: cout, cin, and endl¶
The iostream library is part of the C++ Standard Library and handles input and output.
-
std::cout: The "character output" stream. It sends data to the console.
- Uses the insertion operator (
<<).
- Uses the insertion operator (
-
std::cin: The "character input" stream. It reads data from the keyboard.
- Uses the extraction operator (
>>).
- Uses the extraction operator (
-
std::endl vs \n: Both move the cursor to the next line.
std::endlalso "flushes" the buffer (forces the output to appear immediately), which can be slower.\nis usually preferred for performance. -
Namespaces: The
std::prefix tells the compiler thatcoutandcinlive in the "standard" namespace.
1.7 — Uninitialized Variables and Undefined Behavior¶
-
Uninitialized Variables: Variables that have been defined but not given a value. They contain whatever "garbage" value was already in that memory location.
-
Undefined Behavior (UB): The result of executing code whose behavior is not defined by the C++ language. Reading an uninitialized variable is UB.
-
Consequences of UB: The program might crash, produce wrong results, or behave differently on different computers. Always initialize your variables.
1.8 — Keywords and Naming Identifiers¶
-
Keywords: Reserved words (like
int,return,if) that have special meaning and cannot be used as variable names. -
Identifier Rules:
-
Cannot start with a digit.
-
Can only contain letters, digits, and underscores.
-
Case-sensitive (
Valueandvalueare different).
-
-
Naming Conventions: * Variable names usually start with a lowercase letter (e.g.,
myVariableormy_variable).- Avoid starting with underscores, as those are often reserved for OS/Library use.
1.9 — Whitespace and Basic Formatting¶
-
Whitespace: Characters like spaces, tabs, and newlines. C++ is mostly "whitespace independent," meaning the compiler doesn't care about the layout.
-
Code Style: While the compiler doesn't care, humans do. Consistent indentation and spacing make code readable and maintainable.
1.10 — Introduction to Literals and Operators¶
-
Literals: Fixed values inserted directly into the code (e.g.,
5,3.14,"Hello"). -
Operators: Symbols that perform a specific mathematical or logical operation.
-
Unary: Operates on one operand (e.g.,
-5). -
Binary: Operates on two operands (e.g.,
x + y). -
Ternary: Operates on three operands (the conditional operator).
-
1.11 — Introduction to Expressions¶
-
Expression: A combination of literals, variables, operators, and functions that produces a single value.
- Example:
2 + 3is an expression that evaluates to5.
- Example:
-
Expression Statements: A statement consisting of an expression followed by a semicolon. The compiler evaluates the expression and then discards the result.
- Example:
x = 2 + 3;(The expressionx = 5is evaluated, and the side effect is thatxnow holds 5).
- Example: