Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn 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

book
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:


Let's consider an example where we're using this syntax:

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. Similarly, i-- decrements the value by 1.

  • int i = 0 is the initialization part. Initialization occurs when the loop starts. At the start of the loop, a new temporary variable called i is created and initialized 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 an increment or decrement. Since the value of i is incremented after every iteration, the condition i < 10 becomes false after 10 iterations, and the loop stops.

It's important to understand that all three components of the for loop — initialization, condition, and operation — are optional. You can omit any of them, but this might change how the loop behaves.

Although it's not recommended, you can declare the loop variable outside the loop and leave the initialization part empty. This means the loop will still work, but you need to be careful as it might lead to unexpected results if not handled properly.

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

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`.

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?

How can we improve it?

Thanks for your feedback!

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