beginner18 min

Control Flow and Function Overloading

C++'s familiar control flow, plus function overloading -- something plain C doesn't allow.

What you'll learn

  • Write if/else, for, and while exactly as in C -- C++ keeps the same core syntax
  • Define two functions with the same name but different parameter types (overloading)
  • Predict which overload the compiler selects based on an argument's type

Explanation

C++'s core control-flow keywords -- if/else, for, while, do-while, and switch -- work exactly as they do in C, including switch's fallthrough-by-default behavior. If you're comfortable with C's control flow, there's nothing new to learn here syntactically.

Where C++ genuinely diverges from C is function overloading: C++ allows multiple functions to share the same name as long as their parameter types differ, something plain C does not permit at all. void printValue(int x) and void printValue(double x) can coexist -- the compiler picks the right one, called overload resolution, based on the type of the argument at each call site: printValue(5) calls the int version, printValue(5.0) calls the double version.

This is possible because C++ compilers use name mangling internally -- each overload actually gets a distinct internal symbol name encoding its parameter types, even though your source code refers to both simply as printValue. It's the same underlying idea that later lets templates generate a distinct function for each type they're used with. Overloading lets you present one clear, conceptual name for an operation ("print this value") while still handling different types with genuinely different code underneath.

Guided lab

Predict: Overload resolution inside a loop

C++Not executed
This lab does not run in your browser or on VisaSparkSchools's servers. Read the code, predict what it does, then reveal the real expected output.

Read this program and predict exactly what it prints.

#include <iostream>

void printValue(int x) {
    std::cout << "int: " << x << std::endl;
}

void printValue(double x) {
    std::cout << "double: " << x << std::endl;
}

int main() {
    for (int i = 1; i <= 3; i++) {
        if (i % 2 == 0) {
            printValue(i);
        } else {
            printValue(i * 1.0);
        }
    }
    return 0;
}

Stuck? Get a hint.

Common mistakes

  • Assuming plain C allows function overloading the same way -- it doesn't; this is a genuine C++-only feature.
  • Expecting overload resolution to consider a function's return type -- it's based on argument types, not what the caller does with the result.
  • Writing two overloads that are ambiguous for a given call (e.g. both requiring the same implicit conversion), which is a compile error, not a runtime choice.

Knowledge check

Knowledge check

1. Does plain C allow function overloading (same name, different parameter types)?
2. What determines which overload of printValue the compiler selects at a call site?
3. Do C++'s if/for/while keywords work differently from C's?

Takeaway

C++'s control-flow keywords match C's exactly, but C++ adds function overloading -- multiple same-named functions distinguished by parameter type, resolved by the compiler at each call site.

Summary

if/for/while/switch work the same as in C; function overloading, resolved by argument type at each call site, is a genuine C++-only addition C lacks entirely.

References

Your notes

Notes save automatically.

Finished this lesson?

Mark it complete to track your progress and schedule a future review.