C#, CodeProject

Programming basics : Using loops

Welcome reader,

This blog post will cover the use of loops. Loops are used to repeat the same operation(s) at least once. Usually, most programs you can think of will need this kind of functionnality. We can use loops to go through data or let a program start from the start, they can even be use when we need to compute data. In order to execute a loop, a condition must be validate, which was why we had to know how to manage the program flow of a software application.

The first loop we’re going to see is the while loop. In order to know what is happening in a loop, we must first know how to write a loop. When using a while loop, we’re telling the computer the following statement: while the following condition is true, please perform all the stuff that is inside the loop. It is thanks to this condition that the loop will know when it has to stop the looping operation. While this must sound like an easy task to do, this is a well known issue for beginners: infinite loops. For example, like we saw earlier with the comparison operators, if we were to tell while the value of 1,  the while loop will never know when to stop because the value, in the condition, does not change. We’ll see below how to write a while loop with an example :

char selectedAnswerFromUser='Y';

while(selectedAnswerFromUser== 'Y') //We are evaluating the condition the the    equality operator and not the assign operator !


Console.WriteLine("Would you like to stop this loop? Please press y for yes or n for no");

selectedAnswerFromUser =Console.ReadLine();

if(selectedAnswerFromUser== 'n')


Console.WriteLine("GoodBye ! ");


if(selectedAnswerFromUser !='y' || selectedAnswerFromUser != 'n')
   Console.WriteLine("Please, you can either press the y or the n letter!");


To continue, there is a similar loop to the while loop; it is called the do while loop. The single difference is that the do while will be performed at least once even if the condition is not true. That is the difference between the while and do while loop. We’ll see below an example of the do while loop :

int a=2;

int b=5;

do{ //We start the loop without giving a condition, it will be given below

a++; //The ++ operator tells the computer to increment the value of the a variable by one

Console.WriteLine(a); //This will print the value of a while we are performing the loop

}while(a<b); //in the do while loop, after giving your condition, finish your statement with the ';'

While we are using loops, you can also use variables to count the number of times the loop will perform. We saw the ++ operator earlier, which is meant to increment the value of a variable by one after the line has been read. We have an equal operator in case we want to decrease the value of a variable; the — operator. Here where it gets tricky: depending on which side you put the operators, the have different effects :

  1. variable++ :  The value we use is the value before the incrementation (Postfix incrementation)
  2. ++variable:The value of the variable we’re using is the value after the incrementation (Prefix incrementation)
  3. variable–: The value we use is the value before the variable decreases by 1(Postfix decrementation)
  4. –variable: The value we use is the value after the variable decreases by 1 (Prefix decrementation)

Thanks to those, we’ll now see how to create a condition in a while loop.

int a=5;

while(a<20) { ++a; Console.WriteLine("This the value of the a variable : {0}",a); //This the modulo operator. //It divide the variable a by 2 and looks for remainders //if there are none ->the value will be 0

// else -> We'll see the remainder

if( a%2 == 0 )


Console.WriteLine("The value of a will be diminish by 2. This is the value of a now: {0},a);



int remainderFromModulo=a%2;

Console.WriteLine("This is the remainder of the modulo : {0}",remainderFromModulo);




The last loop we’re going to see in this post is called the for loop. This loop is different from the previous ones that we first saw. This kind of a loop is used when we know exactly the number of times we are going to repeat the block of code until the condition becomes false. The structure is a bit different as we can see below :

for(int i=0;i<5;i++)


//do stuff

//when the i variable has the value of 5, then the for loop will stop its iteration


In the for loop, we first have to initialize which variable is used to iterate, but it does not have to hold the value of 0. Then, after the semi-colon, we have to give a condition to the loop, which, like we know, use to know how long we have to iterate. And finally, we have to specify if the value of this variable is going to increase or to decrease. Also, we don’t have to increase/decrease by the value of 1. It was used to serve as an example.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s