Course Content
C# Basics
C# Basics
For Loop
A loop allows us to execute a piece of code as many times as we want. There are different types of loops. In C#, if we want to execute a piece of code a specific number of times we use the for-loop.
The template (syntax) of a for loop is the following:
Example:
main
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { // Output "Hello World" ten times. for(int i = 0; i < 10; i++) { Console.WriteLine("Hello World"); } } } }
Note
i++
is the same asi = i + 1
. It increments the value of the variable by1
.i–
decrements the value by1
.
int i = 0
is the initiation part. Initiation is done when the loop starts. So at the start of the loop a new temporary variable calledi
is created and initiated with a value of0
;i < 10
is the condition. The condition is checked on every iteration (repetition) of the loop. The loop continues executing the enclosed code block as long as the condition is true;i++
is the operation to be performed after every iteration.The operation is usually increment of decrement. Since the value ofi
is incremented after every iteration, the conditioni < 10
becomes false after 10 iterations and the loop stops;
Following diagram explains the flow of a for-loop in a visual manner:
It is important to note that all the three components initiation
, condition
and operation
are optional and hence any of them can be omitted but the loop might behave differently based on the changes.
It is not a recommended practice but we can declare the loop variable outside of the loop and leave the initiation
part empty.
main
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int i = 0; for(; i < 10; i++) { Console.WriteLine("Hello World"); } } } }
We can also remove the operation
part and put it at the end of the loop's code block. The loop will behave the same as before, as the variable i
is still being incremented after every iteration.
main
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int i = 0; for(; i < 10;) { Console.WriteLine("Hello World"); i++; } } } }
Note that in the above code we still placed a semi-colon (;
) in the loop syntax where the int i = 0
was expected. This semi-colon must always be there whether we create a loop variable or not.
Removing the loop condition is possible as well but it makes the loop run infinitely in case it's not manually ended (which we will learn more about in the later chapters).
main
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { // In this case the variable `i` is useless so we don't create it. // The loop never ends for(;;) { Console.WriteLine("Hello World"); } } } }
Note that we still included both the semi-colons in the loop syntax for(;;)
, these are essential otherwise the compiler will show errors.
Since the value of i
changes every iteration, we can use it to our advantage. To understand this let's look at a simple program which outputs the value of i
every iteration:
main
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { for(int i = 0; i < 10; i++) { Console.WriteLine(i); } } } }
Note that it ends at 9
because it starts with the digit 0
. The total number of iterations were ten. We can modify the initiation, condition and the operation to output the ten even numbers from 2 to 20.
Note
Even numbers are the numbers that are divisible by
2
. For-example 2, 4, 6, 8, 10, etc. Even numbers also include the number0
but we will only deal with even numbers above0
in these examples.
main
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { for(int i = 2; i <= 20; i += 2) { Console.WriteLine(i); } } } }
Note
i += 2
is a shorter way of writingi = i + 2
. This syntax is also valid for other operators for-examplei -= 2
,i *= 2
,i /= 2
,i %= 2
etc.
The loop starts with 0
and increases the value of i
by 2
every iteration. We changed the condition to i <= 20
so that the loop stops at 20
.
The above logic was for the sake of understanding the workings of a for-loop. We can use a much simpler logic to output first ten even numbers:
main
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { for(int i = 1; i <= 10; i++) { Console.WriteLine(i * 2); } } } }
In this case the condition is much simpler. For example, in case we want the first 27 even numbers, we will have to know what is the 27th even number to be able to form the condition for the previous method, however in this case we simply need to modify the condition to i <= 27
:
main
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { for(int i = 1; i <= 27; i++) { Console.WriteLine(i * 2); } } } }
Everything was clear?