>RunTimeFix
BookStudyPracticeInterviewsYouTubeCoursesAbout
Log inGet Started

Table of Contents

01C++ Foundations & Input/Output02Loops & Direct Simulations03Arrays, Strings & Character Operations04Functions & Recursion05Strings & Character Operations06Structs, Lambdas & Structured Bindings07Preprocessor, Debug Macros & Compilation08Vectors, Pairs & Utility Algorithms09Sets, Maps & Multi-Containers10Sorting & Frequency Arrays11Complexity & Problem Solving12Basic Number Theory & Modular Arithmetic13Prefix Sums & Difference Arrays14Two Pointers & Sliding Window15Binary Search (Array & Answer Space)16Greedy Algorithms & Constructive Problems17Bit Manipulation & Masking18Stacks, Queues & Monotonicity19Disjoint Set Union (DSU)20Coordinate Compression, Fenwick Tree & Segment Tree21Graph Representation, DFS & BFS22Cycle Detection, Bipartite Checking & Topological Sort23Dijkstra, 0-1 BFS & Kruskal's MST24Rooted Trees — DFS, Subtree Size & Diameter25Binary Lifting, LCA & Euler Tour26Dynamic Programming — Foundations27Dynamic Programming — Knapsack & Subsets28Backtracking & Meet-in-the-Middle29Modular Inverse, Combinatorics & Fast Math30Hashing, String Algorithms & Geometry Basics
Study Library/Ch 2
Chapter 02 of 30

Loops & Direct Simulations

Master the three loop structures in C++, loop controls, nested loops, and core loop patterns for competitive programming.

The Three Loop StructuresLoop Controls: break & continueNested LoopsCore Loop Patterns
Share:

Loops & Direct Simulations

Loops are how you tell the computer to repeat work. In competitive programming, the simplest approach to many problems is a direct simulation — just code exactly what the problem describes, step by step, using loops.

The Three Loop Structures

for Loop

Use when you know the number of iterations in advance:

hljs cpp
for (int i = 0; i < n; i++) {
    // runs n times: i = 0, 1, 2, ..., n-1
}

The for loop has three parts: initialization, condition, and update — all in one line. This makes it the most common loop in competitive programming.

while Loop

Use when the number of iterations depends on a condition:

hljs cpp
int x = 100;
while (x > 0) {
    x /= 2;  // halve x until it reaches 0
}

do-while Loop

Runs at least once, then checks the condition:

hljs cpp
int n;
do {
    cin >> n;
} while (n <= 0);  // keeps asking until n is positive

Loop Controls: break & continue

break

Exits the loop immediately:

hljs cpp
for (int i = 0; i < n; i++) {
    if (a[i] == target) {
        cout << "Found at index " << i << "\n";
        break;  // stop searching
    }
}

continue

Skips the rest of the current iteration and jumps to the next one:

hljs cpp
for (int i = 0; i < n; i++) {
    if (a[i] % 2 == 0) continue;  // skip even numbers
    cout << a[i] << " ";           // prints only odd numbers
}

Nested Loops

When you need to iterate over combinations or 2D structures:

hljs cpp
// Print all pairs (i, j) where i < j
for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
        cout << a[i] << " " << a[j] << "\n";
    }
}

Complexity warning: Nested loops multiply their complexities. Two loops of n iterations each give O(n²). Three give O(n³). Always check whether the total work fits within the time limit (typically ~10⁸ operations per second).

Core Loop Patterns

Counting

hljs cpp
int count = 0;
for (int i = 0; i < n; i++) {
    if (a[i] > 0) count++;
}

Accumulation

hljs cpp
long long sum = 0;
for (int i = 0; i < n; i++) {
    sum += a[i];
}

Finding Minimum/Maximum

hljs cpp
int mn = a[0];
for (int i = 1; i < n; i++) {
    if (a[i] < mn) mn = a[i];
}

Linear Search

hljs cpp
int idx = -1;
for (int i = 0; i < n; i++) {
    if (a[i] == target) {
        idx = i;
        break;
    }
}

Simulation

Many problems just ask you to simulate a process:

hljs cpp
// Problem: Given n, repeatedly divide by 2 until odd. Count steps.
int steps = 0;
while (n % 2 == 0) {
    n /= 2;
    steps++;
}
cout << steps << "\n";

Summary

  • Prefer for when the iteration count is known
  • Prefer while when the termination depends on a condition
  • break exits the loop; continue skips to the next iteration
  • Nested loops multiply complexities — O(n²) for two levels, O(n³) for three
  • Direct simulation: code exactly what the problem says, step by step
Ch 1: C++ Foundations & Input/OutputCh 3: Arrays, Strings & Character Operations

Prefer reading offline? Download the full book as a PDF.

Download Free Book
>RunTimeFix

Practical programming education. Books, videos, courses, and hands-on learning for competitive programming, DSA, and software engineering.

RunTimeFix

  • About
  • YouTube
  • Book
  • Study Library
  • Courses

Learn

  • Competitive Programming
  • DSA
  • Development
  • Databases

Legal

  • Privacy
  • Terms
  • Refund Policy

RunTimeFix — debug → solve → run

© 2026 RunTimeFix. All rights reserved.