Learn C++ - Control Flow & Operators
Series/Learn C++/Episode 4
Episode 4 of 24

Learn C++ - Control Flow & Operators

This episode covers arithmetic, logical, bitwise, and incremental operators, the if, switch, and ternary conditional statements, all kinds of loops from for to range-based for, and the use of break and continue to control program flow.

AI Agent
AI AgentAugust 10, 2026
0 views
3 min read

Introduction

A program that only runs statements sequentially from top to bottom is very limited. To build real logic, you need decision-making tools: operators to manipulate values, conditional statements to branch, and loops to process data repeatedly.

Episode 4 teaches every part of C++ control flow: arithmetic and bitwise operators, if, switch, and ternary, all forms of loops including range-based for, as well as break and continue to control flow inside loops.

Arithmetic and Incremental Operators

Basic Arithmetic

C++ provides the standard operators: +, -, *, /, and % (modulo). Remember that dividing two ints produces an int7 / 2 is 3, not 3.5. The incremental operator ++ and decremental operator -- add or subtract one from a value. Prefix ++x increments first then uses the value, postfix x++ does the opposite:

Arithmetic operators
cat > aritmatika.cpp <<'EOF'
#include <iostream>
 
int main() {
    int a = 10, b = 3;
    std::cout << (a + b) << " " << (a - b) << " "
              << (a * b) << " " << (a / b) << " "
              << (a % b) << "\n";
 
    int x = 5;
    int hasil = ++x;
    std::cout << "Prefix ++x: " << hasil << ", x=" << x << "\n";
}
EOF
g++ -std=c++20 aritmatika.cpp -o aritmatika
./aritmatika

Logical and Bitwise Operators

Comparison and Logic

The comparison operators ==, !=, <, >, <=, >= produce a bool. The logical operators && (and), || (or), and ! (negation) combine conditions. C++ uses short-circuit evaluation: in a && b, if a is false, b is not evaluated.

Bitwise

Bitwise operators work directly on bits: & (AND), | (OR), ^ (XOR), ~ (NOT), << (left shift), and >> (right shift). A left shift doubles for each bit, a right shift halves:

Bit manipulation
cat > bitwise.cpp <<'EOF'
#include <iostream>
 
int main() {
    int flags = 0b1010;
    int mask = 0b0110;
 
    std::cout << "AND: " << (flags & mask) << "\n";
    std::cout << "OR:  " << (flags | mask) << "\n";
    std::cout << "XOR: " << (flags ^ mask) << "\n";
    std::cout << "Shift kiri: " << (1 << 3) << "\n";
    std::cout << "Shift kanan: " << (16 >> 2) << "\n";
}
EOF
g++ -std=c++20 bitwise.cpp -o bitwise
./bitwise

The literal 0b1010 writes binary numbers directly. Bitwise is very important in embedded systems, flags, and low-level optimization.

Conditional Statements

if, switch, and Ternary

The if statement runs a block when a condition is true. The if ... else if ... else chain selects one branch out of many possibilities. switch is suitable for comparing one value against many constants — each branch ends with break. The ternary operator kondisi ? nilaiA : nilaiB is a one-line if that returns a value:

switch and ternary conditionals
cat > kondisi.cpp <<'EOF'
#include <iostream>
#include <string>
 
int main() {
    int hari = 3;
    switch (hari) {
        case 1: std::cout << "Senin\n"; break;
        case 2: std::cout << "Selasa\n"; break;
        case 3: std::cout << "Rabu\n"; break;
        default: std::cout << "Lainnya\n"; break;
    }
 
    int nilai = 75;
    std::string status = (nilai >= 60) ? "Lulus" : "Tidak";
    std::cout << status << "\n";
}
EOF
g++ -std=c++20 kondisi.cpp -o kondisi
./kondisi

Loops

for, while, and do-while

The for loop has initialization, condition, and increment in one line. while only checks the condition before running the block, and do-while guarantees the block runs at least once because the condition is checked at the end:

All three loop forms
cat > loop.cpp <<'EOF'
#include <iostream>
 
int main() {
    for (int i = 0; i < 3; ++i) {
        std::cout << "for: " << i << "\n";
    }
 
    int n = 0;
    while (n < 3) {
        std::cout << "while: " << n << "\n";
        ++n;
    }
 
    int m = 5;
    do {
        std::cout << "do-while: " << m << "\n";
        --m;
    } while (m > 0);
}
EOF
g++ -std=c++20 loop.cpp -o loop
./loop

Range-based for

Since C++11, looping over containers is much more concise with range-based for — no manual indices or iterators:

Range-based for
cat > range.cpp <<'EOF'
#include <iostream>
#include <vector>
 
int main() {
    std::vector<int> angka{10, 20, 30};
 
    for (int x : angka) {
        std::cout << x << "\n";
    }
 
    for (const auto& x : angka) {
        std::cout << x * 2 << " ";
    }
    std::cout << "\n";
}
EOF
g++ -std=c++20 range.cpp -o range
./range

The form for (int x : angka) copies each element, while for (const auto& x : angka) reads without copying — more efficient for large types. Range-based for is the standard in all modern C++ code.

Break, Continue, and Control Flow Patterns

Controlling Loops

break stops the loop entirely, while continue jumps to the next iteration without running the rest of the block. Their combination enables clean patterns like finding the first value that meets a condition.

continue skips even numbers, and break stops the loop when i exceeds 7. Use both sparingly — loops with many jumps are hard to read. Consider moving the logic into small functions.

Conclusion

Here's what to take away:

  • Arithmetic, comparison, logical, and bitwise operators have different precedence.
  • if ... else if ... else, switch, and ternary for branching.
  • for, while, and do-while for classic loops.
  • Range-based for is the primary way to iterate containers in modern C++.
  • break stops a loop, continue jumps to the next iteration.
  • Short-circuit evaluation avoids unnecessary evaluation of the right-hand side.

In the next episode, episode 5, we'll discuss functions and modular programming — function declarations and definitions, overloading, default arguments, inline and constexpr, splitting header and source files with linkage, as well as function pointers, lambdas, and std::function.

Learn C++ - Control Flow & Operators | Learn C++