Repeat with a while loop
A while loop repeats a step while its condition is true.
Simple example
Start1 → 3
Result1
2
3
using System;
int number = 1;
while (number <= 3) {
Console.WriteLine(number);
number += 1;
}
1 2 3
Advertisements
Everyday examples
A ticket roll starts at 21. Show the next three ticket numbers, starting with 21.
Start21 → 23
Result21
22
23
using System;
int ticket = 21;
while (ticket <= 23) {
Console.WriteLine(ticket);
ticket += 1;
}
21 22 23
Run the example
dotnet new console --output Lesson
cp Program.cs Lesson/Program.cs
dotnet run --project Lesson