• Startseite
  • Tutorials
  • Kontakt
  • Mein Account
Panjutorials
  • Startseite
  • Tutorials
  • Kontakt
  • Mein Account

While loop in Csharp

In this article, you’ll learn about the While loop in CSharp. This is one of the four loop types. In the last article, we have briefly discussed the different types of loops, but we have not yet gone into detail. We have also addressed only three of the four loops. We will discuss the foreach loop in the context of arrays. Why is the While loop so popular? Because it is especially useful in state-controlled loops. We will look at the state-controlled loops, but first let’s have a look at a simple counter. Let’s consider the following situation, we are a bus driver for a school and have to make sure that all children go in the bus and we didn’t forget anyone. Then we can solve it with a simple counter using a while loop. Basically, a While loop looks like this:

int counter = 5;
long factorial = 1;

while (counter > 1)
{
   factorial *= counter--;
}

Lets have a look at the real life example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Panjutorials
{
    class Program

    {

        static void Main(string[] args)
        {
            countKids(7);
        }

        static void countKids(int totalNumber)
        {
            int counter = 0;
            while (counter < totalNumber)
            {
                Console.WriteLine(counter + "Kids are on the bus");
                counter++;
            }
            Console.WriteLine("All " + counter + " kids are on the bus");
            Console.Read();
        }
    }
}

This program now checks if all 7 children are on the bus until this is the case. When this is the case, the While loop is exited and the code below it is executed. It then looks like this:

0 Kids are on the bus

1 Kids are on the bus

2 Kids are on the bus

3 Kids are on the bus

4 Kids are on the bus

5 Kids are on the bus

6 Kids are on the bus

All 7 kids are on the bus

Let’s look at a state-controlled loop with While. For example, Tetris. There you are game over when all lines are filled. Let’s say there are 12 lines. Then game over should be displayed as soon as all 12 lines are filled

Example of a state-controlled loop in CSharp

class Program

{
        
static void Main(string[] args)
{
    textWriter();
}

static void textWriter()
{
    string input = "";
    string completeText = "";
    while(input != "end")
    {
        Console.WriteLine("The current text is " + completeText);
        Console.WriteLine("Please enter the next word");
        input = Console.ReadLine();
        completeText = completeText + input;
    }
    Console.Read();


}

This program will ask for a new text until we enter “end”. This is of course also applicable in other areas. In general, this is useful when you want to repeat a task until a certain condition is met. For example, add water until the barrel is full;

IMPORTANT – DO NOT CREATE AN INFINITE LOOP

Which is a loop in which the condition is permanently fulfilled, this will cause your program to crash.