Loading, please wait...

A to Z Full Forms and Acronyms

Loops in C# | C# Tutorials

Dec 03, 2020 C#, CSharp, 3006 Views
The looping method has a very importance for almost every object oriented programming language, so as for the C#. C# uses different loop methods to carry out functions and statements. Looping is used in a programming language to perform or execute a statement or set of statements for multiple times depending on the condition we provide it to get our desired result. The looping perform only till when the given conditions remains true.

Loops in C# | C# Tutorials

The looping method has a very importance for almost every object oriented programming language, so as for the C#. C# uses different loop methods to carry out functions and statements. Looping is used in a programming language to perform or execute a statement or set of statements for multiple times depending on the condition we provide it to get our desired result. The looping perform only till when the given conditions remains true.

Pre-requisite:

To perform the programs the Visual studio will be used as an integrated development environment. Simply open up the Visual Studio and click on CREATE A NEW PROJECT then select Console App (.Net Core). Name the project then click on Create.

Methods:

The loops are of different types, they can be entry controlled or exit control or even nested loops. There are

  • While loop
  • Do while loop
  • For loop
  • Nested loop

Each of the above has a specific way and control system. For example while loop is an entry controlled loop whereas do while loop is an exit controlled loop. Each of them repeats themselves till they meet the conditions.

They tend to stop their work when they get out of scope. Which means they are out of the loop. This can be either the condition provided is fulfilled or they are forced to get out of the loop after a certain time.

There are different statements to control the loops, and C# supports them all.

  • Break statement: This terminates the loop right away and starts with the other statements out of the loop.
  • Continue statement: Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.

Sometimes it might occur that loops are not ending or keeps on executing which results in an error. This happens due to infinite looping. When there is no condition or no appropriate expressions for the loop and while statement, the execution of the statement or set of statements tends to remain in an infinite loop.

Example:

using System;




namespace Loops {

   class Program {

      static void Main(string[] args) {

         for (; ; ) {

            Console.WriteLine("Hey, I am an infinite loop");

         }

      }

   }

}

 

In the above example there are no expression in the for loop. When the conditional expression is absent, it is assumed to be true. So, it will go on and on resulting in showing an error.

At last there is this one more thing called nested loop. This is like having one loop inside another loop. They come in handy when we are declaring a 2-D Array.

The general syntax for for loop in a nested form would be

for ( init; condition; increment ) {  

     for ( init; condition; increment ) {     

           statement(s);  

     }  

     statement(s);

}

Code:

While Loop

This loop repeatedly executed the statements as long as the given condition remains true. The loop iterates while the condition is true.

Example:

using System; 

namespace Loops {   
    class Program {      
          static void Main(string[] args) {         
                 /* local variable definition */         
                 int a = 1, b = 3;          
                /* while loop execution */         
                
                 while (a <= 10) {            
                      Console.WriteLine("value of {b} * {a}:"b*a);
                      a++;         
                 }      
          }   
    }
}

Do..While Loop

The Do..while loop is much similar to the while loop, except that it unsures to execute the statements for one last time before getting out of the loop after getting the conditions false.

Example:

using System; 

namespace Loops {   
    class Program {      
          static void Main(string[] args) {         
       
                 /* local variable definition */
                 int a = 1, b = 3;
                 /* do..while loop execution */         
  
                 do{            
                     Console.WriteLine("value of {b} * {a}:"b*a);
                     a++;         
                 } while (a <= 10);      

          }   
     }
}

For Loop

The for loop is very much controlled loop with proper expressions defining its starting and ending. In the for loop there are in total 3 segments to provide the expressions and initialize a value. They can be overlooked in exceptional cases.

Example:

using System;

namespace Loops {
   class Program {
      static void Main(string[] args) {
          /* local variable definition */
          int b = 3;
          /* for loop execution */

         for (int a = 1; a <= 10; a ++) {
            Console.WriteLine("value of {b} * {a}:"b*a);
         }
      }
   }
}
A to Z Full Forms and Acronyms