Skip to content
Learn Netverks

Lesson

Step 9/36 25% through track

control-flow-cpp

Control flow

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Control flow: the syntax, patterns, and safety habits you need before advancing in C++.

Coroutines replace callback hell on Android and in Ktor—structured concurrency is interview-critical.

You will apply Control flow in contexts like: Game engines, trading systems, desktop apps, and performance-critical libraries.

Write C++ in main.cpp with int main(), click Run on server—the dev runner compiles with c++/g++ -std=c++17 -Wall and runs the binary; read template errors in stderr (LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

C++ control flow mirrors Java: if, else, for, while, do-while, and switch. Modern C++ adds range-based for over containers.

Range-based for

std::vector<int> nums = {1, 2, 3};
for (int n : nums) {
    std::cout << n << " ";
}

switch

switch works on integral and enum types. Use break to avoid fall-through unless intentional.

Important interview questions and answers

  1. Q: Range-for requirements?
    A: The type needs begin() and end() in scope—works with arrays, vectors, strings, and more.
  2. Q: switch on strings?
    A: Not directly in older standards—use if/else chain or std::map/hash map for dispatch.

Self-check

  1. How does do-while differ from while?
  2. Why prefer range-for over index loops when possible?

Tip: Range-based for works like enhanced for-loops in Java—prefer it over manual index loops when you do not need the index.

Interview prep

Range-for requirements?

The type needs accessible begin and end—works with arrays, vectors, strings, and custom types.

switch fall-through?

Missing break executes subsequent cases—document intentional fall-through with comments.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • range-for when?
  • switch fallthrough?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump