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.

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.
C++ provides the standard operators: +, -, *, /, and % (modulo). Remember that dividing two ints produces an int — 7 / 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:
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
./aritmatikaThe 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 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:
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
./bitwiseThe literal 0b1010 writes binary numbers directly. Bitwise is very important in embedded systems, flags, and low-level optimization.
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:
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
./kondisiThe 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:
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
./loopSince C++11, looping over containers is much more concise with range-based for — no manual indices or iterators:
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
./rangeThe 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 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.
Here's what to take away:
if ... else if ... else, switch, and ternary for branching.for, while, and do-while for classic loops.break stops a loop, continue jumps to the next iteration.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.