Learn C Language - Operators & Control Flow
Episode 4 of 24

Learn C Language - Operators & Control Flow

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.

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

Introduction

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 and Bitwise Operators

Basic Arithmetic Operators

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.

Arithmetic operator experiments
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 && ./aritmatika

Note 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

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:

Read and set bits
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 && ./bitwise

The 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.

Logical and Ternary Operators

Comparison and Logic

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

The ternary operator ?: is a short if-else that produces a value. Its form is condition ? true_value : false_value:

Ternary operator
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.

Conditional Statements: if, if-else, switch

Branching with if

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.

Switch for Many Choices

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:

Switch statement
#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.

Loops: for, while, do-while

The Three Loop Forms

  • 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.
Comparing the three loops
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, Continue, and Return

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.

Closing

Key takeaways:

  • Bitwise operators work at the bit level and matter for flags and masks.
  • Don't confuse = assignment with == comparison.
  • The ternary ?: is a short if-else for simple assignments.
  • Use switch to compare against many constant values.
  • for, while, and do-while each have their own uses.
  • break stops, continue skips, and return ends everything.

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.