Coding/Learn/c/c loops and jump statements
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

LearnLoops (For, While, Do-While) & Jump Statements
5 / 20 0%
Lesson 05 of 20
6 mins read

Loops (For, While, Do-While) & Jump Statements

PreviousNext Lesson
In Plain English

C provides 3 core iteration constructs: for loops for bounded iteration, while loops for precondition polling, and do-while loops for at-least-once execution. Use continue to skip cycles and break to terminate immediately.

The Mental Model

"A for loop is a fixed 100-meter sprint; a while loop is jogging until it starts raining; a do-while loop is taking at least one sip of soup before deciding if it is too hot."

Deep Dive: How It Works

for Loop: Composed of initialization, condition check, and increment expression.

while Loop: Evaluates condition before executing the loop body.

do-while Loop: Guarantees execution at least once before testing condition.

Syntax Blueprint

for (int i = 0; i < N; i++) {
    if (i == 2) continue;
    printf("%d\n", i);
}

Standard indexed iteration loop with continue to skip to the next iteration.

PointCore Rules to Remember

Point star
for Loop Mechanics: Ideal when step count and boundaries are known at compile or run time.
Point star
continue vs break: continue skips the rest of the current iteration; break exits the loop immediately.
Point star
Accumulation Patterns: Initialize accumulators to 0 before the loop to avoid reading uninitialized stack memory.

Common Beginner Traps & How to Fix Them

Mistake:Off-by-one errors with <= vs <.

Why it happens: Iterating for (int i = 0; i <= size; i++) over an array of length size accesses invalid memory.

How to fix: Use i < size for 0-indexed structures.

Pro Tip: Prefer for loops for indexed traversal so iteration variable scope is confined to the loop block.

Live Interactive Example

Hit Run Code to see it live
Filter Multiples with Continue
C11 / GCC 9.2
1
2
3
4
5
6
7
8
9
10
11
Output Console
Click "Run Code" to view the rendered output.
How it works: Numbers not divisible by 3 trigger continue, printing only 3, 6, 9, 12, and 15.

Your Turn: Micro Challenge

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

Micro Exercise

Calculate Range Sum with For Loop

  • PointCalculate the sum of integers from 1 to 5 inclusive using a second for loop.
  • PointPrint "Sum: %d\n" with the calculated sum.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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