Cambo Freelance
ទំព័រដើមសេវាកម្មអត្ថបទការបង្រៀនក្រុមវគ្គបណ្តុះបណ្តាលទំនាក់ទំនង
Cambo Freelance

Professional freelance team from Cambodia delivering technology-driven solutions with cultural insight and modern expertise.

ផ្ញើសាររបស់អ្នក

ត្រៀមចាប់ផ្តើមគម្រោងរបស់អ្នក? ទំនាក់ទំនងក្រុមរបស់យើង។

ទំនាក់ទំនង

សេវាកម្ម

  • Web & Mobile Development Services
  • Graphic Design & Branding Services

តំណភ្ជាប់មានប្រយោជន៍

  • ទំព័រដើម
  • សេវាកម្ម
  • ការសិក្សា
  • អំពីយើង
  • ទំនាក់ទំនង
  • តម្លៃ

© 2026 Cambo Freelance. រក្សាសិទ្ធិទាំងអស់។

TelegramFacebookLinkedInEmail
HomeTutorialsC++C++ Loops
⚡C++ Tutorials

50 lessons

Beginner(21)1C++ Introduction2C++ Getting Started3C++ Syntax4C++ Output5C++ Comments6C++ Variables7C++ Data Types8C++ Constants9C++ User Input10C++ Operators11C++ Strings12C++ Math13C++ Booleans14C++ Conditions15C++ Switch16C++ Loops17C++ Break and Continue18C++ Arrays19C++ Functions20C++ References21C++ Pointers
Intermediate(19)22C++ Classes23C++ Objects24C++ Constructors25C++ Access Specifiers
Advanced(10)41C++ Smart Pointers42C++ Templates43C++ Lambda Functions44
⚡

C++ Tutorials

Lesson 16 of 50

All lessons
Beginner (21)1C++ Introduction2C++ Getting Started
26
C++ Encapsulation
27C++ Inheritance
28C++ Polymorphism
29C++ Abstraction
30C++ Files
31C++ Exception Handling
32C++ Namespaces
33C++ Structures
34C++ Enumerations
35C++ STL Introduction
36C++ Vectors
37C++ Maps
38C++ Sets
39C++ Queues
40C++ Stacks
C++ Move Semantics
45C++ Multithreading
46C++ Design Patterns
47C++ Memory Management
48C++17 Features
49C++20 Features
50C++ Performance Optimization
3
C++ Syntax
4C++ Output
5C++ Comments
6C++ Variables
7C++ Data Types
8C++ Constants
9C++ User Input
10C++ Operators
11C++ Strings
12C++ Math
13C++ Booleans
14C++ Conditions
15C++ Switch
16C++ Loops
17C++ Break and Continue
18C++ Arrays
19C++ Functions
20C++ References
21C++ Pointers
Intermediate (19)22C++ Classes23C++ Objects24C++ Constructors25C++ Access Specifiers26C++ Encapsulation27C++ Inheritance28C++ Polymorphism29C++ Abstraction30C++ Files31C++ Exception Handling32C++ Namespaces33C++ Structures34C++ Enumerations35C++ STL Introduction36C++ Vectors37C++ Maps38C++ Sets39C++ Queues40C++ Stacks
Advanced (10)41C++ Smart Pointers42C++ Templates43C++ Lambda Functions44C++ Move Semantics45C++ Multithreading46C++ Design Patterns47C++ Memory Management48C++17 Features49C++20 Features50C++ Performance Optimization
⚡C++BeginnerLesson 16 of 50CamboFreelanceJune 19, 2026

C++ Loops

Master all three C++ loop types: while, do-while, and for. Learn the range-based for loop and when to use each loop style.


Tutorials › C++ › Loops
Beginner9 min readLesson 16 of 50

In This Lesson

  • while Loop
  • do-while Loop
  • for Loop
  • Range-based for
  • Quiz

while Loop

The while loop repeats a block as long as the condition is true. The condition is checked before each iteration.

#include <iostream>
using namespace std;

int main() {
    int i = 1;
    while (i <= 5) {
        cout << "i = " << i << endl;
        i++;
    }
    return 0;
}

Output:

i = 1
i = 2
i = 3
i = 4
i = 5

do-while Loop

The do-while loop always runs the block at least once, then checks the condition.

int n;
do {
    cout << "Enter a positive number: ";
    cin >> n;
} while (n <= 0);
cout << "You entered: " << n << endl;

for Loop

The for loop is perfect when you know how many times to iterate. It bundles init, condition, and update in one line.

for (int i = 0; i < 5; i++) {
    cout << "Iteration " << i << endl;
}
// Print multiplication table of 7
for (int i = 1; i <= 10; i++) {
    cout << "7 x " << i << " = " << 7 * i << endl;
}

Range-Based for Loop (C++11)

The cleanest way to iterate over a collection:

int numbers[] = {10, 20, 30, 40, 50};
for (int n : numbers) {
    cout << n << " ";
}
// Output: 10 20 30 40 50
Tip: Use for when the number of iterations is known. Use while when it depends on a condition. Use do-while when you need at least one run.

Exercise

Use a for loop to print the sum of all numbers from 1 to 100.

Show Solution
#include <iostream>
using namespace std;

int main() {
    int sum = 0;
    for (int i = 1; i <= 100; i++) {
        sum += i;
    }
    cout << "Sum 1-100 = " << sum << endl;  // 5050
    return 0;
}

Quiz

  1. Which loop always executes its body at least once?

    • A) for
    • B) while
    • C) do-while
    • D) range-based for
    Answer

    C) do-while — it checks the condition after the body runs.

  2. What are the three parts of a for loop header?

    • A) condition, body, update
    • B) init, condition, update
    • C) start, end, step
    • D) declare, loop, exit
    Answer

    B) init, condition, update — e.g., for (int i=0; i<5; i++).

Summary

  • while — checks condition first, runs 0 or more times.
  • do-while — runs at least once, checks condition after.
  • for — compact loop for a known number of iterations.
  • Range-based for — cleanly iterates over arrays and collections.
← Previous: Switch Next: Break & Continue →
c++loopsforwhilebeginner
PreviousLesson 15: C++ SwitchNextLesson 17: C++ Break and Continue
Back to All Tutorials