Coding/Learn/cpp/while loops
C++ Foundational Course
01.Why C++ Powers the Modern World
02.Anatomy of a C++ Program
03.Semicolons, Statements & Comments
04.Variables & Fundamental Data Types
05.Console I/O: std::cout and std::cin
06.Arithmetic, Modulo & Increment Operators
07.Branching Decisions: if, else if, and else
08.Logical Operators (&&, ||, !)
09.Switch Statements: Fast Multi-Way Selection
10.While & Do-While Loops
11.The Standard For Loop & Counting
12.Loop Control: break and continue
13.Functions: Modular, Reusable Code Blocks
14.Pass by Value vs Pass by Reference (&)
15.Fixed-Size Arrays & Contiguous Memory
16.Dynamic Arrays: std::vector and STL
17.Strings Deep Dive & Manipulation
18.Unmasking Memory: Pointers & Addresses (& and *)
19.Custom Data Types: Structs & OOP Basics
20.Capstone: Command-Line Bank & Account System
Course Certificate0%

Complete all lessons to earn

LearnWhile & Do-While Loops
10 / 20 0%
Lesson 10 of 20
5 mins read

While & Do-While Loops

PreviousNext Lesson
In Plain English

Loops prevent repetitive copy-pasted code. A while loop repeatedly executes its body as long as its condition evaluates to true. A do-while loop guarantees the loop body runs at least once before checking the condition.

The Mental Model

"A while loop is like checking if you have gas before starting a road trip. A do-while loop is taking at least one bite of food before deciding whether you want to keep eating."

Deep Dive: How It Works

while (condition): Checks condition FIRST. If initially false, the loop body runs 0 times.

do { ... } while (condition);: Executes the body FIRST, then checks condition at the end. Runs at least 1 time.

The Infinite Loop Trap: If you forget to increment your counter (e.g. i++), the condition stays true forever and your program freezes!

Syntax Blueprint

int i = 0;
while (i < 3) {
  std::cout << i << " ";
  i++; // Update step!
}

Condition checked before every iteration; variable must be updated inside.

PointCore Rules to Remember

Point star
Condition First: while checks condition prior to entering the loop.
Point star
Guaranteed Once: do-while always executes at least once.
Point star
Update Loop Variable: Always ensure the loop variable progresses toward termination.

Common Beginner Traps & How to Fix Them

Mistake:Putting a semicolon immediately after while(condition);

Why it happens: Forgets that while takes a block { }, creating an empty infinite loop.

How to fix: Never put a semicolon after while(condition) header.

Pro Tip: Use while loops when you don't know beforehand how many times the loop will repeat (e.g. waiting for network packets).

Live Interactive Example

Hit Run Code to see it live
Countdown Timer with While Loop
C++ 20
1
2
3
4
5
6
7
8
9
10
11
12
13
Output Console
Click "Run Code" to view the rendered output.
How it works: The loop executes 3 times until countdown reaches 0.

Your Turn: Micro Challenge

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

Micro Exercise

Print powers of 2

  • PointDeclare int value = 1;
  • PointWrite a while loop that prints value and multiplies value *= 2 as long as value <= 8.
1
2
3
4
5
6
7
8
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