Control Flow, Functions, Scope, References, and Program Structure
Build logic using conditionals and loops, write reusable functions, and understand scope and reference-based parameter passing.
Inside this chapter
- Conditionals and Loops
- Functions and Declarations
- Pass by Value, Reference, and const Reference
- Scope and Lifetime
- Recursion and Structure
- Real-World Usage Snapshot
Series navigation
Study the chapters in order for the clearest path from C++ basics to modern ownership, templates, concurrency, performance, and production-ready engineering practices. Use the navigation at the bottom to move smoothly through the full series.
Conditionals and Loops
if (score >= 90) {
std::cout << "Excellent\n";
} else if (score >= 75) {
std::cout << "Good\n";
} else {
std::cout << "Keep improving\n";
}
for (int i = 0; i < 5; ++i) {
std::cout << i << '\n';
}
C++ control flow is familiar to C and Java learners, but writing clear, predictable conditions and loops remains an important skill.
Functions and Declarations
int add(int a, int b);
int main() {
std::cout << add(2, 3) << '\n';
}
int add(int a, int b) {
return a + b;
}
Function declarations, definitions, return types, and parameter design all become more important as programs grow across multiple files.
Pass by Value, Reference, and const Reference
void increment(int &value) {
++value;
}
void printName(const std::string &name) {
std::cout << name << '\n';
}
References are one of the big differences between C++ and C. They enable efficient and expressive parameter passing without raw pointer syntax in many cases.
Scope and Lifetime
Variables live only within their scope. Local variables are destroyed when their block ends. Later, this idea becomes central to RAII, destructors, and safe resource management in modern C++.
Recursion and Structure
Recursion is useful in tree traversal, divide-and-conquer algorithms, and some mathematical problems. Students should understand both recursion and iteration because each has strengths and tradeoffs.
Real-World Usage Snapshot
Functions, references, and control flow are the framework for everything that follows, from classes and templates to concurrency and performance-sensitive code. Good structure at this level improves readability everywhere else.