Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
For Loop | Loops
C# Basics
course content

Course Content

C# Basics

C# Basics

1. Getting Started
2. Dealing with Data Types
3. Control Structures
4. Loops
5. Arrays
6. Methods

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:

cs

main

copy
123456789101112131415
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 as i = i + 1. It increments the value of the variable by 1. i– decrements the value by 1.

  • 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 called i is created and initiated with a value of 0;
  • 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 of i is incremented after every iteration, the condition i < 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.

cs

main

copy
12345678910111213141516
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.

cs

main

copy
1234567891011121314151617
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).

cs

main

copy
1234567891011121314151617
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:

cs

main

copy
123456789101112131415
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 number 0 but we will only deal with even numbers above 0 in these examples.

cs

main

copy
123456789101112131415
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 writing i = i + 2. This syntax is also valid for other operators for-example i -= 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:

cs

main

copy
1234567891011121314
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:

cs

main

copy
1234567891011121314
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { for(int i = 1; i <= 27; i++) { Console.WriteLine(i * 2); } } } }

What will be the output of the following program? The value of sum increases every iteration by i, therefore its value during the first four iterations will be: 1, 3, 6, 10.

Select the correct answer

Everything was clear?

Section 4. Chapter 1
We're sorry to hear that something went wrong. What happened?
some-alt