Looping Statements In C#: Part One

Here we will discuss  looping statements in C# Programming

Synopsis
  1. Introduction
  2. Definition
  3. Types of looping
  4. Single Loops
  5. 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.

statement



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:

  1. class Program  
  2. {  
  3.   
  4.     static void Main(string[] args)  
  5.   {  
  6.         int i;  
  7.         Console.WriteLine("Output");  
  8.         for (i = 1; i <= 10; i++)   
  9.         {  
  10.             Console.Write(i + " ");  
  11.         }  
  12.         Console.Read();  
  13.     }  
  14. }  
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:
  1. class Program  
  2. {  
  3.   
  4.     static void Main(string[] args)  
  5.     {  
  6.         int i;  
  7.         Console.WriteLine("Output");  
  8.         for (i = 10; i >= 1; i--)  
  9.         {  
  10.             Console.Write(i + " ");  
  11.         }  
  12.         Console.Read();  
  13.     }  
  14. }  
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:
  1. class Program   
  2. {  
  3.   
  4.     static void Main(string[] args)  
  5.     {  
  6.         int i = 1;  
  7.         Console.WriteLine("Output");  
  8.         while (i <= 10)   
  9.         {  
  10.             Console.Write(i + " ");  
  11.             i++;  
  12.         }  
  13.         Console.Read();  
  14.     }  
  15. }  
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:
  1. class Program  
  2. {  
  3.   
  4.     static void Main(string[] args)  
  5.     {  
  6.         int i = 10;  
  7.         Console.WriteLine("Output");  
  8.         while (i >= 0)  
  9.         {  
  10.             Console.Write(i + " ");  
  11.             i--;  
  12.         }  
  13.         Console.Read();  
  14.     }  
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:
  1. class Program  
  2. {  
  3.   
  4.     static void Main(string[] args)  
  5.     {  
  6.         int i = 1;  
  7.         Console.WriteLine("Output");  
  8.         do  
  9.         {  
  10.             Console.Write(i + " ");  
  11.             i++;  
  12.         } while (i <= 10);  
  13.         Console.Read();  
  14.     }  
  15. }  
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:
  1. class Program   
  2. {  
  3.   
  4.     static void Main(string[] args)  
  5.     {  
  6.         int i = 10;  
  7.         Console.WriteLine("Output");  
  8.         do  
  9.         {  
  10.             Console.Write(i + " ");  
  11.             i--;  
  12.         } while (i >= 1);  
  13.         Console.Read();  
  14.     }  
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:
  1. class Program  
  2. {  
  3.   
  4.     static void Main(string[] args)   
  5.     {  
  6.         int i = 1;  
  7.         Console.WriteLine("Output");  
  8.         do  
  9.         {  
  10.             Console.Write(i + " ");  
  11.             i++;  
  12.         } while (i >= 10);  
  13.   
  14.         Console.Read();  
  15.     }  
  16. }
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:


Recommended Free Ebook
Similar Articles