This episode masters all of C's operators: arithmetic, bitwise, logical, comparison, and ternary. You will also practice the if, if-else, and switch conditional statements, all three loop types, and break, continue, and return as control flow tools.

Syntax alone doesn't make a program useful. A program's power lies in its ability to process data and make decisions. Episode 4 covers these two foundations: operators that process values, and control flow that directs the order of execution.
C offers a richer set of operators than most modern languages, especially bitwise operators that work directly at the bit level. These operators matter for flags, encryption, and systems programming. Control flow covers branching with if and switch, and repetition with for, while, and do-while.
By the end of this episode, you will be able to write programs that read decisions, repeat processes, and stop loops precisely — the core of almost every algorithm.
Arithmetic operators work like in mathematics: addition +, subtraction -, multiplication *, division /, and modulo %. Two things often surprise beginners: dividing two integers yields an integer with the remainder discarded, and % only works on integers.
cat > aritmatika.c <<'EOF'
#include <stdio.h>
int main(void) {
int a = 17, b = 5;
printf("a / b = %d\n", a / b);
printf("a %% b = %d\n", a % b);
printf("a + b = %d\n", a + b);
return 0;
}
EOF
gcc -Wall -Wextra aritmatika.c -o aritmatika && ./aritmatikaNote that 17 / 5 yields 3, not 3.4, because both operands are int. For fractional division, at least one operand must be float or double.
Bitwise operators work on the binary representation of integers: & AND, | OR, ^ XOR, ~ NOT, << shift left, and >> shift right. These operators are used to manipulate flags and masks:
cat > bitwise.c <<'EOF'
#include <stdio.h>
int main(void) {
unsigned char flag = 0;
flag |= 1 << 2;
printf("flag = %u\n", flag);
if (flag & (1 << 2)) {
printf("bit 2 aktif\n");
}
return 0;
}
EOF
gcc -Wall -Wextra bitwise.c -o bitwise && ./bitwiseThe pattern flag |= 1 << 2 turns on bit 2, and flag & (1 << 2) checks whether that bit is active. Shift left << multiplies a value by powers of two, while shift right >> divides it.
Comparison operators produce a true or false value: == equal, != not equal, <, >, <=, and >=. Logical operators combine several conditions: && AND, || OR, and ! negation. Remember the difference between = for assignment and == for comparison — swapping them is a classic error the compiler doesn't always catch.
The ternary operator ?: is a short if-else that produces a value. Its form is condition ? true_value : false_value:
int usia = 20;
const char *status = usia >= 18 ? "dewasa" : "anak-anak";The expression usia >= 18 ? "dewasa" : "anak-anak" reads: if age is 18 or above, status holds dewasa; otherwise it holds anak-anak. The ternary is good for short assignments, but avoid nesting it because it becomes hard to read.
The if statement executes a block only if the condition is true. Pair it with else for an alternative branch, and else if for a chain of sequential conditions. Conditions can be built from comparison and logical operators.
When comparing one variable against many constant values, switch is cleaner than a chain of if-else. Each branch ends with break so execution doesn't fall through to the next branch:
#include <stdio.h>
int main(void) {
int nilai = 3;
switch (nilai) {
case 1:
printf("satu\n");
break;
case 2:
printf("dua\n");
break;
default:
printf("lainnya\n");
break;
}
return 0;
}The block switch (nilai) compares nilai against each case. default handles all values that don't match. Omitting break causes fall-through behavior that is sometimes intentional, but often a bug.
for: suited when the number of iterations is known in advance.while: suited when the loop runs as long as a condition is true, for example reading until the end of a file.do-while: executes the body at least once, suited for menus that must always display once.for (int i = 0; i < 5; i++) {
printf("for %d\n", i);
}
int j = 0;
while (j < 5) {
printf("while %d\n", j);
j++;
}
int k = 0;
do {
printf("do-while %d\n", k);
k++;
} while (k < 5);All three forms above print the numbers 0 through 4. Note that the loop variable can be declared directly inside for since the C99 standard.
break stops the innermost loop or switch. continue jumps to the next iteration without leaving the loop. return ends the function entirely, carrying a return value along. These control patterns matter for search algorithms: break when a value is found, continue to skip irrelevant values, and return to exit a function early.
Warning
while and do-while loops risk becoming infinite loops if the condition never changes. Always make sure there is a step inside the loop body that changes the condition variable, such as an increment.
Key takeaways:
= assignment with == comparison.?: is a short if-else for simple assignments.In the next episode 5 we will discuss functions and modular programming — function definitions, prototypes, and variable scope, passing parameters by value, header files and translation units, and recursion, inline functions, and simple function pointers.