Coding/Learn/c/c control flow decisions
C Foundational Course
01.Introduction, Compilation Pipeline & Syntax
02.Variables, Primitives & Explicit Type Casting
03.Operators, Precedence & Booleans (<stdbool.h>)
04.Control Flow: If-Else, Ternary & Switch
05.Loops (For, While, Do-While) & Jump Statements
06.Contiguous Arrays & Multi-Dimensional Matrices
07.Null-Terminated Strings & <string.h>
08.User Input, scanf & fgets Buffer Safety
09.Pointers, Memory Addresses & Dereferencing
10.Functions, Prototypes & Pass-by-Pointer
11.11. Recursion & High-Speed Function Pointers
12.File Streams: Create, Write & Read (fopen/fclose)
13.Structures, Unions & typedef Aliasing
14.Memory Word Alignment & Struct Padding
15.Enums & Bitwise Manipulation (Flags & Masks)
16.Dynamic Memory: malloc, calloc, realloc & free
17.Defensive Error Handling, errno & Assertions
18.Preprocessor Macros & Storage Classes
19.Fixed-Width Types (stdint.h), Math & Time (<time.h>)
20.Capstone: High-Performance In-Memory Key-Value Engine
Course Certificate0%

Complete all lessons to earn

LearnControl Flow: If-Else, Ternary & Switch
4 / 20 0%
Lesson 04 of 20
6 mins read

Control Flow: If-Else, Ternary & Switch

PreviousNext Lesson
In Plain English

Branching in C is controlled by if/else if/else structures, inline ternary expressions (? :), and switch statements that compile into constant-time O(1) jump tables for integer/character dispatch.

The Mental Model

"An if-else chain is a line of tollbooths checking your ticket one by one; a switch statement is an express elevator that jumps directly to floor 404 in one swift move."

Deep Dive: How It Works

Jump Table Optimization: switch compiles into direct memory address jumps for constant-time dispatch.

The break Keyword: Halts case execution to prevent accidental fallthrough into subsequent cases.

Ternary Operator: (cond) ? valA : valB provides clean inline conditional assignments.

Syntax Blueprint

switch (status) {
    case 200:
        printf("OK\n");
        break;
    default:
        break;
}

Evaluate integral expression and jump to matching case label. Break prevents fallthrough.

PointCore Rules to Remember

Point star
O(1) Jump Table: Switch statements on dense integral values compile into instant jump tables.
Point star
Mandatory break: Always terminate switch cases with break unless intentional fallthrough is documented.
Point star
Default Branch: Catches any unhandled edge case values.

Common Beginner Traps & How to Fix Them

Mistake:Omitting break in a switch case.

Why it happens: C will continue executing subsequent case code blocks regardless of their match condition.

How to fix: Ensure every case block terminates with break or return.

Pro Tip: Use switch statements when matching against fixed integral error codes or enum values for cleaner code and compiler optimization.

Live Interactive Example

Hit Run Code to see it live
HTTP Status Code Dispatcher
C11 / GCC 9.2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Output Console
Click "Run Code" to view the rendered output.
How it works: httpStatus matches case 200, prints Success, and break exits the switch.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Handle 404 Not Found in Switch

  • PointSet httpStatus to 404.
  • PointAdd a "case 404:" branch in the switch statement that prints "Status: Not Found\n" with a break.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates