fre c# course

C# Loops

Think of loops as something that repeats over and over (like that annoying friend). Basically a loop will repeat a sequence of instructions inside a contained area until some type of action causes the loop to break out. This is what is known as "breaking out of a loop" in old lament terms.

Keep in mind also that there are actually a series of different types of loops that you can make a program executive over and over. This just provides more variation in between whatever your intentions are.

One of the first loop statement is called a while. The while expression will check a variable usually against a value to jump out of the loop if the value is still counting then the loop is still in process. Look at the example above. The value is checking to make sure the loop counts a total of 49 times. This is because of value < 50 which means if value is less than 50. If the loops has not finished then another variable called count will increase throughout the body.

Finally once the loop is complete the message "Sally has accounted for all the guests and there were 40 in total will be printed on the web page. I say web page since this example was done using ASP.NET.

One important thing to keep in mind is if you don't include a counting loop within the body, such as value = value + 1 in my example, you will encounter what is known as an endless loop. This is a loop that will continue on forever until either the server crashes or you break out of the program.

Subscribe to the Programmermind newsletter
Privacy Notice: Programmer Mind will not compromise your privacy