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 learn to use while loops in C#.
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.Working of C# while loop
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.Let's see what happens in the given program on each iteration.
Initially, i = 1, sum = 0
Iteration | Value of i | i<=5 | Value of sum |
---|---|---|---|
1 | 1 | true | 0+1 = 1 |
2 | 2 | true | 1+2 = 3 |
3 | 3 | true | 3+3 = 6 |
4 | 4 | true | 6+4 = 10 |
5 | 5 | true | 10+5 = 15 |
6 | 6 | false | Loop terminates |
So, the final value of sum will be 15.
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.Working of C# do...while loop
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.If the test expression in the while and do...while loop never evaluates to false
, the body of loop will run forever. Such loops are called infinite loop.
For example:
while (true)
{
// body of while loop
}
do
{
// body of while loop
} while (true);
The infinite loop is useful when we need a loop to run as long as our program runs.
For example, if your program is an animation, you will need to constantly run it until it is stopped. In such cases, an infinite loop is necessary to keep running the animation repeatedly.
#csharp #c#