Coding/Learn/csharp/while loops
C# Foundational Course
01.Why C# & .NET Power Modern Software
02.Anatomy of a C# Application
03.Variables & The C# Type System
04.Console I/O: Reading & Writing
05.String Interpolation & Modern Formatting
06.Arithmetic, Math Class & Modulo
07.Conditional Branching: if, else if, and else
08.Logical Operators (&&, ||, !)
09.Switch Statements & Modern Switch Expressions
10.While & Do-While Loops
11.The Standard For Loop & Counting
12.Loop Control: break and continue
13.Methods: Modular, Reusable Code Blocks
14.Optional Parameters, Named Arguments & out/ref
15.Fixed-Size Arrays & Zero-Based Indexing
16.The Sacred Foreach Loop
17.Dynamic Collections: List<T> & Generics
18.Classes & Objects: The OOP Blueprint
19.Auto-Properties & Constructors
20.Capstone: RPG Character & Inventory Engine
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-pasting. A while loop checks a condition first and repeats its body as long as the condition holds true. A do-while loop executes its body once before checking the condition.

The Mental Model

"A while loop is like checking if your phone battery has charge before turning on the screen. A do-while loop is taking at least one sip of coffee before checking if the mug is empty."

Deep Dive: How It Works

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

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

Loop Progression: The loop variable must be modified inside the loop (e.g. i++) to prevent infinite loops.

Syntax Blueprint

int i = 0;
while (i < 3) {
  Console.WriteLine(i);
  i++; // Progress counter!
}

Condition checked before every iteration; variable updated inside.

PointCore Rules to Remember

Point star
Condition First: while checks condition prior to entering the loop.
Point star
Guaranteed At Least Once: do-while executes at least once regardless of condition.
Point star
Always Update Counter: Ensure the loop progresses toward termination.

Common Beginner Traps & How to Fix Them

Mistake:Forgetting to increment the loop variable inside a while loop

Why it happens: Causes an infinite loop that locks up CPU execution.

How to fix: Ensure your counter variable increments (e.g. count++) before the loop block closes.

Pro Tip: Use while loops when waiting on unpredictable external conditions (like user input or file loading)!

Live Interactive Example

Hit Run Code to see it live
Countdown Timer with While Loop
C# 12 / .NET
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Output Console
Click "Run Code" to view the rendered output.
How it works: The while loop runs 3 times, decrementing countdown to 0.

Your Turn: Micro Challenge

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

Micro Exercise

Double a number with while loop

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