in , ,

C# for loop

csharp for loop
csharp for loop

In this tutorial, we will learn about for loop in C# and various approaches to use them in a program.

In programming, it is frequently desired to execute certain block of statements for a specified number of times. A possible arrangement will be to type those statements for the necessary number of times. Notwithstanding, the quantity of redundancy may not be known ahead of time (during compile time) or possibly enormous enough (say 10000).

The best solution for such issue is loop. Loops are used in programming to over and again execute a specific block of statements until some condition is met.

At the point when you know precisely how often you need to loop through a block of code, use the for loop rather than some while loop:

In this tutorial, we’ll look at for loop in C#.


C# for loop

The for watchword is used to create for loop in C#. The syntax for loop is:

for (initialization; condition; iterator)
{
	// body of for loop
}

How for loop works?

  1. C# for loop has three statements: initialization, condition and iterator.
  2. The initialization statement is executed at first and only once. Here, the variable is typically declared and initialized.
  3. Then, the condition is assessed. The condition is a boolean expression, for example it returns either true or false.
  4. If the condition is assessed to true:

a. The statements inside the for loop are executed.

b. Then, the iterator statement is executed which ordinarily changes the value of the initialized variable.

c. Again the condition is assessed.

d. The measure proceeds until the condition is assessed to false.

  1. If the condition is assessed to false, the for loop terminates.

for Loop Flowchart


Example 1: C# for Loop

using System;

namespace Loop
{
	class ForLoop
	{
		public static void Main(string[] args)
		{
			for (int i=1; i<=5; i++)
			{
				Console.WriteLine("C# For Loop: Iteration {0}", i);
			}
		}
	}	
}

At the point when we run the program, the output will be:

C# For Loop: Iteration 1
C# For Loop: Iteration 2
C# For Loop: Iteration 3
C# For Loop: Iteration 4
C# For Loop: Iteration 5

in this program,

• initialization statement is int i=1

• condition statement is i<=5

• iterator statement is i++

At the point when the program runs,

• First, the variable I is proclaimed and initialized to 1.

• Then, the condition (i<=5) is assessed.

• Since, the condition returns true, the program at that point executes the body of the for loop. It prints the given line with Iteration 1 (Iteration basically implies redundancy).

• Now, the iterator (i++) is assessed. This augmentations the value of I to 2.

• The condition (i<=5) is assessed again and toward the end, the value of I is augmented by 1. The condition will assess to true for the initial 5 times.

• When the value of I will be 6 and the condition will be false, subsequently the loop will end.

Example 2: for loop to compute sum of first n natural numbers

using System;

namespace Loop
{
	class ForLoop
	{
		public static void Main(string[] args)
		{
			int n = 5,sum = 0;

			for (int i=1; i<=n; i++)
			{
				// sum = sum + i;
				sum += i;
			}

			Console.WriteLine("Sum of first {0} natural numbers = {1}", n, sum);
		}
	}
}

At the point when we run the program, the output will be:

Sum of first 5 natural numbers = 15

Here, the value of sum and n are initialized to 0 and 5 individually. The iteration variable I is initialized to 1 and increased on each iteration.

Inside the for loop, value of sum is augmented by I for example sum = sum+ I. The for loop proceeds until I is not exactly or equivalent to n (user’s input).

Let’s see what happens in the given program on each iteration.

Initially, i = 1, sum = 0 and n = 3

IterationValue of ii<=5Value of sum
11true0+1 = 1
22true1+2 = 3
33true3+3 = 6
44true6+4 = 10
55true10+5 = 15
66falseLoop terminates

Thus, the last value of sum will be 15 when n = 5.


Multiple expressions inside a for loop

We can likewise use numerous expression inside a for loop. It implies we can have more than one initialization and/or iterator statements inside a for loop. Let’s see the example below.

Example 3: for loop with multiple initialization and iterator expressions

using System;

namespace Loop
{
	class ForLoop
	{
		public static void Main(string[] args)
		{
			for (int i=0, j=0; i+j<=5; i++, j++)
			{
				Console.WriteLine("i = {0} and j = {1}", i,j);
			}         
		}
	}
}

At the point when we run the program, the output will be:

i = 0 and j = 0
i = 1 and j = 1
i = 2 and j = 2

In this program, we have proclaimed and initialized two variables: I and j in the initialization statement.

Likewise, we have two expressions in the iterator part. That implies both I and j are augmented by 1 on each iteration.


For loop without initialization and iterator statements

The initialization, condition and the iterator statements are optional in a for loop. It implies we can run a for loop without these statements also.

In such cases, for loop goes about as some while loop. Let’s see the example below.

Example 4: for loop without initialization and iterator statement

using System;

namespace Loop
{
	class ForLoop
	{
		public static void Main(string[] args)
		{
			int i = 1;
			for ( ; i<=5; )
			{
				Console.WriteLine("C# For Loop: Iteration {0}", i);
				i++;
			}
		}
	}
}

At the point when we run the program, the output will be:

C# For Loop: Iteration 1
C# For Loop: Iteration 2
C# For Loop: Iteration 3
C# For Loop: Iteration 4
C# For Loop: Iteration 5

In this example, we haven’t used the initialization and iterator statement.

The variable I is initialized over the for loop and its value is augmented inside the body of loop. This program is same as the one in Example 1.

Likewise, the condition is additionally an optional statement. Notwithstanding in the event that we don’t use test expression, the for loop will not test any condition and will run everlastingly (infinite loop).


Infinite for loop

On the off chance that the condition in a for loop is in every case true, for loop will run for forever. This is called infinite for loop.

Example 5: Infinite for loop

using System;

namespace Loop
{
	class ForLoop
	{
		public static void Main(string[] args)
		{
			for (int i=1 ; i>0; i++)
			{
				Console.WriteLine("C# For Loop: Iteration {0}", i);
			}
		}
	}
}

Here, I is initialized to 1 and the condition is i>0. On each iteration we are increasing the value of I by 1, so the condition won’t ever be false. This will cause the loop execute infinitely.

We can likewise create an infinite loop by replacing the condition with a clear. For instance,

for ( ; ; )
{
	// body of for loop
}

or

for (initialization ; ; iterator)
{
	// body of for loop
}

Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

csharp if else Statement

C# if, if…else, if…else if and Nested if Statement

csharp do while loop

C# while and do…while loop