Control Flow Logic

Master decision making, loops, and machine-level execution. Learn how to direct the CPU's path through your code effectively.

Conditional Logic

The if statement executes code only if a condition is true. The else clause provides an alternative path.

Basic If

Execute code only when condition is true.

if (age >= 18) { printf("Adult"); }

If-Else

Handle both true and false outcomes.

if (score >= 60) { printf("Pass"); } else { printf("Fail"); }

Ternary

Shorthand for simple if-else assignment.

// condition ? true : false int max = (a > b) ? a : b;
decisions.c
#include <stdio.h> int main() { int age = 25; int income = 50000; // Nested If Statements if (age >= 18) { printf("Adult\n"); if (income >= 30000) { printf("Good income\n"); } } else { printf("Minor\n"); } return 0; }

Switch Architecture

Multi-way branching optimization. More efficient than multiple if-else statements for fixed values.

switch.c
switch (option) { case 1: printf("Creating file...\n"); break; // Exit switch case 2: printf("Opening file...\n"); break; default: printf("Unknown option\n"); }

Machine Level: The Jump Table

Compilers optimize consecutive switch cases into a Jump Table. Instead of checking conditions sequentially (O(n)), the CPU uses the value to calculate the memory address of the code to run directly (O(1)).

// Pseudo-Assembly Representation jump_table[] = { &case1, &case2, &case3, &default }; goto jump_table[option]; // Direct Jump!
⚠ Common Mistake: Fall-Through Forgetting the break; statement causes execution to "fall through" to the next case automatically. Always check your breaks!

Iteration Loops

Repeating code blocks efficiently.

For Loop

Best when iteration count is known.

for (int i=0; i<10; i++) { printf("%d", i); }

While Loop

Best for unknown iteration counts.

while (count < 5) { count++; }

Do-While

Guarantees execution at least once.

do { // runs once first } while(condition);
Loop Optimization: Unrolling
Compilers often "unroll" loops to reduce jump overhead. Instead of checking the condition every time, it might execute the body 4 times in a row before checking again.

Jump Statements

Altering the natural flow of execution.

control.c
for (int i = 0; i < 10; i++) { if (i == 3) { continue; // Skips rest of this iteration } if (i == 8) { break; // Exits the loop entirely } printf("%d ", i); } // Output: 0 1 2 4 5 6 7
⚠ Note on GOTO
C supports goto label;, but it is considered bad practice as it creates "spaghetti code". It is typically only used for error handling cleanup in system programming.

Advanced Architecture

How the CPU handles your decisions.

Branch Prediction

Modern CPUs use pipelines to execute instructions. A "branch" (if statement) breaks this pipeline because the CPU doesn't know which code to load next.

To fix this, CPUs guess the outcome (Branch Prediction). If guessed correctly, execution is instant. If wrong, the pipeline is flushed (performance penalty).


Optimization Tip: Sort data before processing to make branches predictable!
// BAD: Unpredictable Branching if (rand() % 2 == 0) { ... } // GOOD: Predictable (e.g. sorted array) if (arr[i] > 128) { ... } // CPU learns "True" happens often after sorting