Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
While Loop | Loops
C# Basics
course content

Course Content

C# Basics

C# Basics

1. Getting Started
2. Dealing with Data Types
3. Control Structures
4. Loops
5. Arrays
6. Methods

While Loop

A while loop is similar to a for-loop, however, it is used when we need to execute a block of code repeatedly based on a condition. The syntax of a while-loop is simpler than that of a for-loop:

Example:

cs

main

copy
12345678910111213141516
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int i = 0; while(i < 5) { Console.WriteLine(i); i++; } } } }

The following diagram shows the execution process of a while loop:

Although the while loop may seem very similar to the for-loop at first, In more advanced levels of programming the difference becomes apparent. We might explore the usage of different kinds of loops in the Arrays section.

Tip: We can write true as the condition of a while-loop to create an infinite loop:

cs

main

copy
123
while(true) { Console.WriteLine("Hello World"); }

How many iterations will the following loop have?

Select the correct answer

Everything was clear?

Section 4. Chapter 3
We're sorry to hear that something went wrong. What happened?
some-alt