Variables & Declarations
When we declare variables in C++, we are telling the compiler: “What kind of data will this name store?”
This helps the compiler allocate the correct amount of memory and enforce type safety. Each
declaration consists of a data type and an identifier (a name you choose). int a; This defines an
integer variable named a, but does not assign a value yet. a = 5; This assigns a new value to the
existing variable. The assignment operator stores the value on the right-hand side into the memory
location of the variable. int a = 5; This is both a declaration and initialization. The variable is
created and immediately given a value.
Data Types
C++ provides different data types for different categories of information. Understanding them helps you
choose the most efficient and meaningful representation. int x = 10; An integer holds whole
numbers. char c = 'a'; Characters represent single symbols using their ASCII codes. bool
ready = true; Booleans hold truth values, useful for conditions. float f = 3.14; Floats and
doubles represent numbers with decimal points. std::string s = "Hello"; Strings represent
sequences of characters, handled as objects from the standard library. const int maxValue = 42;
A constant variable cannot be changed after initialization.
Expressions & Statements
An expression is any combination of values, variables, and operators that produces a new value. A
statement is a complete instruction the program can execute. Some statements contain expressions,
such as assignments. 2 + 2 This is an expression that evaluates to 4. int x; This is a declaration
statement. x = 4; This is an expression statement, evaluated for its side effects.
Type Conversion
Type conversion occurs when a value is transformed from one data type to another. Sometimes this
happens automatically, but sometimes the programmer must request it explicitly. int x = 3.14; This
is an implicit conversion. The compiler automatically converts 3.14 to 3. int y = (int)3.14; This is
an explicit cast, where the programmer clearly instructs the compiler to convert the value.
Operators
Operators perform actions on values. Relational operators compare values and return true or false.
Logical operators manipulate Boolean values. 5 > 4 returns true 4 = = 5 returns false !true returns
false These operations are essential for decision-making in programs.
Conditional Statements
Conditions allow programs to make decisions. They evaluate expressions and choose which code
block to execute depending on their results. if(x > 0) { } This executes only if the condition is
true. if(x > 0) { } else { } In this structure, the else block runs only when the condition is false.
if(a){ } else if(b){ } else if(c){ } In an if–else-if chain, the program checks conditions
from top to bottom and executes the first one that evaluates to true.
Namespaces
Namespaces help prevent naming conflicts by grouping related identifiers under a common label. This
allows two variables with the same name to coexist in different namespaces. namespace health {
int Character = 82; } Here, Character belongs to the 'health' namespace. using namespace
health; This brings everything in the namespace into the current scope so you don’t need to prefix
names with health::.
Functions & Scope
Functions allow us to break programs into reusable pieces of code. Local variables defined inside
functions exist only within that function’s scope. int MyFunction(int p){ int local = p + 5;
return local; } Here, local is a local variable. It cannot be accessed outside the function.
Functions take inputs, process them, and return results to the caller.
Comments
Comments help document the code. They are ignored by the compiler but invaluable to humans
reading the code. // single-line comment /* multi-line \n comment */