Friday, 1 September 2017

How to Use For Loop C#

How to Use For Loop C#


The C# for loop is used to iterate a part of the program several times. If the number of iteration is fixed, it is recommended to use for loop than while or do-while loops.


The C# for loop is same as C/C++. We can initialize variable, check condition and increment/decrement value.

Syntax:

    for(initialization; condition; incr/decr){ 
    //code to be executed 
    }
    C# For Loop Example
        using System; 
        public class ForExample 
            { 
              public static void Main(string[] args) 
              { 
                  for(int i=1;i<=10;i++){   
                    Console.WriteLine(i);   
                  }   
              } 
            } 

        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);   
                            }   
                    }   
                  } 
                } 

            C# Infinite For Loop                using System; 
                public class ForExample 
                    { 
                      public static void Main(string[] args) 
                      { 
                          for (; ;) 
                          { 
                                  Console.WriteLine("Infinitive For Loop"); 
                          }   
                      } 
                 }

Post a Comment

Whatsapp Button works on Mobile Device only

Start typing and press Enter to search