TRT
Sign in

Foundations · Module 2

Decisions, Loops and Functions

Control the flow of a C++ program with branches and loops, and organize it into functions that you can call, reuse and split across files.

Start the moduleFree10 lessons · about 80 minutes

Every program beyond a straight line of statements has to decide, repeat and reuse. This module covers branches, loops and functions, and its traces show what the machine does underneath: comparisons and jumps for decisions, and a call stack that grows and shrinks with every function call. By the end you can write programs that make decisions, process repeated data, and are split into small functions across several files.

  1. 1Conditions and Branches: if, else and switchSee how if, else and switch are compiled into comparisons and jumps, trace which blocks run and which are skipped, and learn the classic mistakes: stray semicolons, missing break and assignment in a condition.8 min
  2. 2Boolean Logic and Short-Circuit EvaluationCombine conditions with &&, || and !, learn the precedence and De Morgan rules, and see how short-circuit evaluation is built from jumps and why operand order is part of your program's logic.7 min
  3. 3Loops: while, do-while and forRepeat code with while, do-while and for, trace the exact order in which a for loop's three parts run, and see that a loop is a conditional jump that goes backwards.8 min
  4. 4Off-by-One Errors and Loop InvariantsLearn why loops are so often wrong by one, use half-open ranges to make boundaries predictable, and prove a loop correct with an invariant instead of hoping that tests will catch the mistake.8 min
  5. 5Functions: Parameters, Return Values and ScopeDefine and call functions, follow an argument as it is copied into a parameter and a value comes back, and learn how declarations, scope and lifetime decide which variable a name refers to.8 min
  6. 6The Call Stack: What Happens When a Function Is CalledFollow nested function calls as frames pushed onto and popped off the stack: the return address, parameters and locals of each call, why locals contain leftovers, and what a stack overflow is.8 min
  7. 7Passing by Value, by Reference and by Const ReferenceChoose between pass by value, reference and const reference: see what each does to the caller's variable, what it costs at the machine level, and why a reference is an alias implemented as an address.8 min
  8. 8Overloading and Default ArgumentsGive several functions one name, watch the compiler rank the candidates and sometimes refuse to choose, see the mangled symbols the linker receives, and use default arguments that the compiler fills in at the call site.8 min
  9. 9Recursion: A Function That Calls ItselfUnderstand recursion as a base case plus a smaller version of the same problem, watch frames pile up and unwind on the call stack, and learn what it costs in time and stack space.8 min
  10. 10Splitting a Program Across Files: Headers and Include GuardsSplit a program into headers and source files, build it as separate translation units, understand what an include guard does, and learn what belongs in a header and what causes multiple-definition errors.9 min