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

For LoopFor 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.cs

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

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

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

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

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

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

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

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.

Виберіть правильну відповідь

Все було зрозуміло?

Секція 4. Розділ 1
course content

Зміст курсу

C# Basics

For LoopFor 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.cs

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

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

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

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

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

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

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

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.

Виберіть правильну відповідь

Все було зрозуміло?

Секція 4. Розділ 1
some-alt