Ad space (banner)
#๏ธโƒฃC# Lessons
Lesson 5 / 68

Loops (for statements)

In this lesson you will learn how to repeat work with a for statement in C#, so you can run the same logic efficiently. This is for anyone searching for "C# for loop how to use".

A for statement repeats the same work. Instead of copying the same lines over and over, a few lines express a large number of repetitions - that is the power of a loop.

The sample code prints the numbers 1 through 5 using for (int i = 1; i <= 5; i++). This C-derived form (initialiser, condition, update) is shared with Java, JavaScript, and many other languages.

A common stumbling block for beginners is confusing the roles of the three parts. Each one is different: where the loop starts, whether it keeps going, and what changes on every pass.

In real-world development, this is an indispensable construct that shows up everywhere, such as when processing the elements of an array or a list one at a time.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run C# directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)