Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

C# Tutorial

C# HOME C# Intro C# Get Started C# Syntax C# Output C# Comments C# Variables C# Data Types C# Type Casting C# User Input C# Operators C# Math C# Strings C# Booleans C# If...Else C# Switch C# While Loop C# For Loop C# Break/Continue C# Arrays

C# Methods

C# Methods C# Method Parameters C# Method Overloading C# Scope C# Recursion

C# Classes

C# OOP C# Classes/Objects C# Class Members C# Constructors C# this Keyword C# Access Modifiers C# Properties C# Static C# Inheritance C# Polymorphism C# Abstraction C# Interface C# Enums C# Structs

C# Errors

C# Errors C# Debugging C# Exceptions

C# Data Structures

C# Data Structures C# List C# Dictionary C# HashSet C# Stack C# Queue

C# Advanced

C# Files C# Date and Time C# Nullable Types C# Generics C# Lambda C# LINQ

C# Cert

C# Certificate

C# How To

C# How Tos

C# Examples

C# Examples C# Compiler C# Exercises C# Quiz C# Practice Problems C# Server C# Syllabus C# Study Plan


C# While Loop - Real-Life Examples


Share

Real-Life Examples

A while loop repeats a block of code as long as a condition is true.

This chapter shows how you can use it in small programs that solve everyday problems.


Countdown

This program counts down from 5, like before a race starts, and then prints "Go!":

Example

int countdown = 5;

while (countdown > 0)
{
  Console.WriteLine(countdown);
  countdown--;
}
Console.WriteLine("Go!");

Try it Yourself »

countdown-- makes the number 1 smaller each time. When countdown reaches 0, the condition is false, and the loop stops.


Add Up Numbers

A loop can keep a running total. This program adds the numbers 1 to 5, one at a time:

Example

int number = 1;
int total = 0;

while (number <= 5)
{
  total = total + number;
  Console.WriteLine("Add " + number + ": the total is " + total);
  number++;
}

Try it Yourself »

Each time the loop runs, the current number is added to total. At the end, total is 15 (1 + 2 + 3 + 4 + 5).



Loop with if...else

You can use an if...else statement inside a loop. In this game, there are 5 levels, and level 5 is the final level:

Example

int level = 1;

while (level <= 5)
{
  if (level < 5)
  {
    Console.WriteLine("Level " + level);
  }
  else
  {
    Console.WriteLine("Level " + level + ": Final level!");
  }
  level++;
}

Try it Yourself »

For levels 1 to 4, the if block runs. For level 5, the else block runs and prints "Final level!".


Play Until You Win

A while loop is a good choice when you do not know how many times the code should run.

In this game, the player gets 30 points each round, and wins with 100 points or more. How many rounds does it take?

Example

int points = 0;
int round = 0;

while (points < 100)
{
  round++;
  points = points + 30;
  Console.WriteLine("Round " + round + ": " + points + " points");
}
Console.WriteLine("You won after " + round + " rounds!");

Try it Yourself »

The loop runs as long as points is less than 100. You do not need to know the number of rounds before the loop starts: the loop finds it for you.


Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.