While Loop

Visual C# While Loop

It ensures that the desired operations are repeated as long as the specified condition is met. 

When using a for loop, the number of times the loop will return is known from the beginning, or at least it can be predicted. 

The while loop is preferred when it is not known how many times the loop will loop. If the condition specified at the beginning is not met, the loop may not return at all, and if it does, it may return many times.

while (our condition)

{

//operations that will be repeated as long as the condition holds...

}

A counter is not kept and incremented as in the for loop. If we need a variable that is constantly increasing or decreasing, such as a counter, we must define it ourselves and do the increase/decrease ourselves.

Also, the loop must be designed to end somewhere. In other words, we must determine the condition we wrote in such a way that the condition does not come true somewhere and the cycle ends. Otherwise, it enters an infinite loop.

While Loop Example

The user will enter a number between 0 and 100 in the text box, the computer will generate a random number consecutively and compare it with this number. The loop will continue until the generated number is the same as the entered number. Each number produced by the computer will be written in label1, and the number of attempts will be written in label2 using a counter.

int counter = 1;
 
label1.Text = "";
 
int a = int.Parse(textBox1.Text);
 
Random random = new Random();
 
int heldNumber = random.Next(0, 100);
 
while (heldNumber !=a && counter<=19)
{
   label1.Text += heldNumber.ToString() + "\n";
   heldNumber = random.Next(0, 100);
   counter++;
}

label1.Text += heldNumber.ToString();
label2.Text = counter.ToString();

This example we made with a while loop is actually more suitable for solving with a do-while loop . Click to see the solution made with do-while in next page.

visual C# While Loop tutorials, while examples, using while loop

EXERCISES

While Loop Tutorials

Write a program that assigns consecutive numbers between 0 and 200 to the computer, writes these numbers one after the other in label1, and terminates the loop when the number held is greater than 100.

While Loop Tutorials
  • A program that adds the numbers entered by the user until 0 is entered from the keyboard. (With InputBox, continuous numbers will be taken from the user, the received numbers will be added to the total each time, if 0 is entered, the loop will end and the total value will be written into label1.)
  • A program that requests grade information from the user 6 times with the InputBox object and calculates the average of the grades received and writes it into label1. (This time we must keep a counter and ensure that the loop ends when the counter is 6. We must add each number received to the total variable, and find the mean by dividing the last total variable by 6.)


COMMENTS




Read 800 times.

Online Users: 3902



using-while-loop