Here we will discuss looping statements in C# Programming.
Synopsis
- Introduction
- Definition
- Types of looping
- Single Loops
- Nested Loops
Introduction
Looping statement is a basic concept in programming languages. It's concept levels are all are same but syntax-wise it is different. Looping statements should have starting and ending points; if the not have ending points they execute endlessly without stopping.
Definition
Looping is continually repeated until a certain condition is reached. It has a starting and ending point. If it does not have an ending point it is called infinite.
Types of single Loops
Different types of looping statements are available.
- For loop
- While loop
- Do While
For Loop
For loop is a one of the most famous looping statements in all programming language. It has staring point and ending point.
Syntax
for( initialization; condition; increment or decrement)
{
//Statements
}
Program 1:
- class Program
- {
-
- static void Main(string[] args)
- {
- int i;
- Console.WriteLine("Output");
- for (i = 1; i <= 10; i++)
- {
- Console.Write(i + " ");
- }
- Console.Read();
- }
- }
Output:
1 2 3 4 5 6 7 8 9 10
Explanation:
Program 1 will execute ten times because the starting value is 1 and the ending value is 10; as well as it will increase value one each time. When enteing looping statement check condition, if conditionis true it will continue to execute, otherwise terminate looping statement.
Program 2:
- class Program
- {
-
- static void Main(string[] args)
- {
- int i;
- Console.WriteLine("Output");
- for (i = 10; i >= 1; i--)
- {
- Console.Write(i + " ");
- }
- Console.Read();
- }
- }
Output:
10 9 8 7 6 5 4 3 2 1
Explanation:
Program 2 will execute ten time but it is staring at a valu of 10 and ending value of 1; as well it will decrease value one each time. When entering looping statement check condition, if condition is true it will continue to execute, otherwise terminate looping statement.
While Loop
While loop is another looping statement. It contains initialization, condition and increment or decrement in different place. While loop’s syntax is different compared to For loop.
Syntax:
//Initialization;
while(condition)
{
//Statements;
//Increment or decrement;
}
Program 3:
- class Program
- {
-
- static void Main(string[] args)
- {
- int i = 1;
- Console.WriteLine("Output");
- while (i <= 10)
- {
- Console.Write(i + " ");
- i++;
- }
- Console.Read();
- }
- }
Output:
1 2 3 4 5 6 7 8 9 10
Explanation:
Program 3 will execute ten time because staring value is 1 and the ending value is 10; as well it will increase value by one each time. In While loop first initialized (i=1) then check condition (i<10) finally increment or decrement (i++).
Program 4:
- class Program
- {
-
- static void Main(string[] args)
- {
- int i = 10;
- Console.WriteLine("Output");
- while (i >= 0)
- {
- Console.Write(i + " ");
- i--;
- }
- Console.Read();
- }
- }
Output:
10 9 8 7 6 5 4 3 2 1
Explanation:
Program 4 will execute ten time but it's staring value is 10 and the ending valueis 1 as well it will decrease value by one each time. In While loop first initialized (i=10) then check condition (i>10) finally increment or decrement (i--).
Do While
This is a little bit different compared tothe two other looping statements. Do while first executes once then checks the condition. If condition is false it will execute once.
Syntax
//Initialization;
do
{
//Statements;
//Increment or decrement;
}while(condition);
Program 5:
- class Program
- {
-
- static void Main(string[] args)
- {
- int i = 1;
- Console.WriteLine("Output");
- do
- {
- Console.Write(i + " ");
- i++;
- } while (i <= 10);
- Console.Read();
- }
- }
Output:
1 2 3 4 5 6 7 8 9 10
Explanation:
Program 5 will execute ten times because the staring valu is 1 and the ending value is 10; as well it will increase value by one each time. In do while loop first initialized (i=1) then check condition (i<10) finally increment or decrement (i++).
Program 6:
- class Program
- {
-
- static void Main(string[] args)
- {
- int i = 10;
- Console.WriteLine("Output");
- do
- {
- Console.Write(i + " ");
- i--;
- } while (i >= 1);
- Console.Read();
- }
- }
Output:
10 9 8 7 6 5 4 3 2 1
Explanation:
Program 6 will executes ten time but it's staring valueis 10 and the ending value is 1; as well it will decrease value by one each time. In do while loop first initialized (i=10) then check condition (i>10) finally increment or decrement (i--).
Note:
In do while should be executed onece also condition is false.
Program 7:
- class Program
- {
-
- static void Main(string[] args)
- {
- int i = 1;
- Console.WriteLine("Output");
- do
- {
- Console.Write(i + " ");
- i++;
- } while (i >= 10);
-
- Console.Read();
- }
- }
Output:
1
Explanation:
Program will execute one time because condition is false. In do while before checking condition executes first time.
Conclusion
This article especially helps those who are new to learning programming language. For other nested looping statement see conditional statements part two.
Read more articles on C# Programming: