C# Loops


C# Loops

C# Loops

There might be a circumstance when you have to execute a square of code a few times. When all is said in done, the announcements are executed successively: The main explanation incapacity is executed first, trailed constantly, etc.

Programming dialects give different control structures that take into consideration progressively confounded execution ways.

A circle articulation permits us to execute an announcement or a gathering of proclamations on numerous occasions and following is the general form of a circle explanation in the vast majority of the programming dialects −

C# gives the following sorts of the loop to deal with circling necessities. Snap the accompanying connects to check their detail.

Sr.No. Loop Type and Description

1 while loop

It rehashes an announcement or a gathering of proclamations while a given condition is valid. It tests the condition before executing the circle body.

2 for loop

It executes a grouping of explanations on numerous occasions and condenses the code that deals with the loop variable.

3 do…while loop

It is like some time articulation, then again, actually it tests the condition toward the finish of the loop body

4 nested loop

A loop within loop also known as nested loop inner or outer

Loop Control Statements

loop control explanations change execution from its ordinary arrangement. At the point when execution leaves a degree, every single programmed object that was made in that extension is decimated.

C# gives the accompanying control explanations. Snap the accompanying connects to check their subtleties.

Sr.No. Control Statement and Description

1 break explanation

Ends the circle or switch explanation and moves execution to the announcement promptly following the circle or switch.

2 continue articulation

Makes the circle avoid the rest of its body and quickly retest its condition preceding emphasizing.

While loop

For Loop

using System;  
public class ForExample  
    {  
      public static void Main(string[] args)  
      {  
          for(int i=1;i<=10;i++){    
            Console.WriteLine(i);    
          }    
      }  
    }  

Infinite loop

using System;

namespace Loops {
   class Program {
      static void Main(string[] args) {
         for (; ; ) {
            Console.WriteLine("Hey! I am Trapped");
         }
      }
   }
} 

C# nested for loop

using System;  
public class ForExample  
    {  
      public static void Main(string[] args)  
      {  
        for(int i=1;i<=3;i++){    
                for(int j=1;j<=3;j++){    
                    Console.WriteLine(i+" "+j);    
                }    
        }    
      }  
    }  

While Loop

using System;  
public class WhileExample  
    {  
      public static void Main(string[] args)  
      {  
          int i=1;    
          while(i<=10)   
          {  
              Console.WriteLine(i);  
              i++;  
          }    
     }  
   }  

do-while loop

The C# do-while loop is utilized to emphasize a piece of the program a few times. On the off chance that the quantity of cycle isn’t fixed and you should need to execute the circle at any rate once, it is prescribed to utilize a do-while loop.

The C# do-while loop is executed in any event once in light of the fact that condition is checked after the loop body.

 

  1. using System;  
  2. public class DoWhileExample  
  3.     {  
  4.       public static void Main(string[] args)  
  5.       {  
  6.           int i = 1;  
  7.             
  8.           do{  
  9.               Console.WriteLine(i);  
  10.               i++;  
  11.           } while (i <= 10) ;  
  12.     
  13.      }  
  14.    }  
  15. C# Loops
    C# if-else statement In C# programming, the if articulation is utilized to test the condition.

    How to display the current date and time c#

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories

%d bloggers like this: