In this article, we will learn about for loop in C# and different ways to use them in a program.
In programming, it is often desired to execute certain block of statements for a specified number of times. A possible solution will be to type those statements for the required number of times. However, the number of repetition may not be known in advance (during compile time) or maybe large enough (say 10000).
The best solution to such problem is loop. Loops are used in programming to repeatedly execute a certain block of statements until some condition is met.
In this article, we’ll look at for loop in C#.
The for keyword is used to create for loop in C#. The syntax for for loop is:
for (initialization; condition; iterator) { // body of for loop }
initialization
, condition
and iterator
.initialization
statement is executed at first and only once. Here, the variable is usually declared and initialized.condition
is evaluated. The condition
is a boolean expression, i.e. it returns either true
or false
.condition
is evaluated to true
:
iterator
statement is executed which usually changes the value of the initialized variable.condition
is evaluated.condition
is evaluated to false
.condition
is evaluated to false
, the for loop terminates.
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);
}
}
}
}
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++
When the program runs,
i <= 5
) is evaluated.true
, the program then executes the body of the for loop. It prints the given line with Iteration 1 (Iteration simply means repetition).i++
) is evaluated. This increments the value of i to 2.i <= 5
) is evaluated again and at the end, the value of i is incremented by 1. The condition will evaluate to true
for the first 5 times.false
, hence the loop will terminate.
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);
}
}
}
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 respectively. The iteration variable i is initialized to 1 and incremented on each iteration.
Inside the for loop, value of sum is incremented by i i.e. sum = sum + i
. The for loop continues until i is less than or equal to n (user’s input).
We can also use multiple expressions inside a for loop. It means we can have more than one initialization and/or iterator statements within a for loop. Let’s see the example below.
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);
}
}
}
}
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 declared and initialized two variables: i and j in the initialization statement.
Also, we have two expressions in the iterator part. That means both i and j are incremented by 1 on each iteration.
The initialization, condition and the iterator statement are optional in a for loop. It means we can run a for loop without these statements as well.
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++;
}
}
}
}
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.
This loop now works like a while loop which we will discuss about later.
The variable i is initialized above the for loop and its value is incremented inside the body of loop. This program is same as the one in Example 1.
Similarly, the condition is also an optional statement. However if we don’t use test expression, the for loop won’t test any condition and will run forever (infinite loop).
The while keyword is used to create while loop in C#. The syntax for while loop is:
while (test-expression) { // body of while }
test-expression
.test-expression
is evaluated to true
,
test-expression
is evaluated again.test-expression
is evaluated to false
, the while loop terminates.using System;
namespace Loop {
class WhileLoop {
public static void Main(string[] args) {
int i = 1;
while (i <= 5) {
Console.WriteLine("C# For Loop: Iteration {0}", i);
i++;
}
}
}
}
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
Initially the value of i is 1.
When the program reaches the while loop statement,
i <=5
is evaluated. Since i is 1 and 1 <= 5
is true
, it executes the body of the while loop. Here, the line is printed on the screen with Iteration 1, and the value of i is increased by 1 to become 2.i <=5
) is evaluated again. This time too, the expression returns true
(2 <= 5), so the line is printed on the screen and the value of i is now incremented to 3..false
and hence the loop terminates.using System;
namespace Loop {
class WhileLoop {
public static void Main(string[] args) {
int i = 1, sum = 0;
while (i <= 5) {
sum += i;
i++;
}
Console.WriteLine("Sum = {0}", sum);
}
}
}
When we run the program, the output will be:
Sum = 15
This program computes the sum of first 5 natural numbers.
sum+i
and the value of i is incremented by 1.i<=5
will return false and the loop terminates.The do and while keyword is used to create a do…while loop. It is similar to a while loop, however there is a major difference between them.
In while loop, the condition is checked before the body is executed. It is the exact opposite in do…while loop, i.e. condition is checked after the body is executed.
This is why, the body of do…while loop will execute at least once irrespective to the test-expression.
The syntax for do…while loop is:
do { // body of do while loop } while (test-expression);
test-expression
is evaluated.test-expression
is true
, the body of loop is executed.test-expression
is false
, do...while loop terminates.using System;
namespace Loop {
class DoWhileLoop {
public static void Main(string[] args) {
int i = 1, n = 5, product;
do {
product = n * i;
Console.WriteLine("{0} * {1} = {2}", n, i, product);
i++;
} while (i <= 10);
}
}
}
When we run the program, the output will be:
5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45 5 * 10 = 50
As we can see, the above program prints the multiplication table of a number (5).
i <= 10
is evaluated. In total, the do...while loop will run for 10 times.false
and hence terminates the loop.In the editor below, perform the different tasks as directed.