Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 8 of 8

Practice & Quiz - Control Flow, Functions & Overloading

Consolidate your knowledge of if/switch, loops, function overloading, default arguments, and recursion with a practical exercise and 5 quiz questions.

10 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Practical Exercise - FizzBuzz with Range-Based For & Overloading

Practical Exercise - FizzBuzz with Range-Based For & Overloading
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>

// Overloaded classify functions
std::string classify(int n) {
    if (n % 15 == 0) return "FizzBuzz";
    if (n %  3 == 0) return "Fizz";
    if (n %  5 == 0) return "Buzz";
    return std::to_string(n);
}
std::string classify(double d) { return "double:" + std::to_string(d); }

// Memoized fibonacci - demonstrates recursive + memoization
std::unordered_map<int, long long> memo;
long long fib(int n) {
    if (n <= 1) return n;
    auto it = memo.find(n);
    if (it != memo.end()) return it->second;
    return memo[n] = fib(n-1) + fib(n-2);
}

int main() {
    // Range-based for - preferred over index loop
    std::vector<int> nums;
    for (int i = 1; i <= 20; ++i) nums.push_back(i);

    std::cout << "FizzBuzz 1-20:\n";
    for (const auto& n : nums) std::cout << classify(n) << " ";
    std::cout << "\n";

    // Default args
    auto log = [](const std::string& msg, bool prefix = true) {
        if (prefix) std::cout << "[LOG] ";
        std::cout << msg << "\n";
    };
    log("Server start");
    log("Raw output", false);

    // Memoized fibonacci
    std::cout << "\nfib(1..10): ";
    for (int i = 1; i <= 10; ++i) std::cout << fib(i) << " ";
    std::cout << "\n";
    return 0;
}
// Build: g++ -Wall -std=c++17 fizzbuzz.cpp -o fizzbuzz && ./fizzbuzz

Module Quiz

Module Quiz
Question 1 of 5

What does `[[fallthrough]]` in a switch statement do?

Tip

Tip

Practice Practice Quiz Control Flow Functions Overloading in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Conditionals, Jump Tables, Loop Mechanics, Inlining, Overloading & Call Stack Execution

Practice Task

Note

Practice Task - (1) Write a working example of Practice Quiz Control Flow Functions Overloading from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Common Mistake

Warning

A common mistake with Practice Quiz Control Flow Functions Overloading is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready cpp code.

Topics in This Module

0/8 done