SEARCH C# PROGRAMMING CODES HERE

loops


Loops in C#
A loop statement allows us to execute a group of statements multiple times, The first statement in a function is executed first, followed by the second, and so on.
C# provides following loops

1-while loop
while loop simply executes a block of code as long as the condition you give is true. First, it tests the condition before executing the loop body.
while loop Example as follows
namespace while_loops
{
    class Program
    {
        static void Main(string[] args)
         {
            /* local variable definition */
         int i = 1;

         /* while_loops execution */
         while (i < 10)
 {
            Console.WriteLine("value of i = {0}", i);
            i++;
         }
         Console.ReadLine();
      
         }
      }
    }


Output
value of i = 1
value of i = 2
value of i = 3
value of i = 4
value of i = 5
value of i = 6
value of i = 7
value of i = 8
value of i = 9

2-for loop
It executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.
for loop Example as follows
using System;

namespace for_loop
 {
   class Program
 {
      static void Main(string[] args)
        {
         
         /* For_ loop execution */
         for (int x = 1; x < 10; x = x + 1) {
            Console.WriteLine("value of x = {0}", x);
         }
         Console.ReadLine();
        }
     }


Output 
value of x = 1
value of x = 2
value of x = 3
value of x = 4
value of x = 5
value of x = 6
value of x = 7
value of x = 8
value of x = 9


3-do...while loop
It is similar to a while statement, except that it tests the condition at the end of the loop body
for loop Example as follows.
using System;
namespace do_while_loop
{
    class Program
    {
        static void Main(string[] args)
         {

             /* local variable definition */
             int x = 1;

             /* do_while loop execution */
             do
             {
                 Console.WriteLine("value of x: {0}", x);
               x = x + 1;
             }
             while (x< 5);
             Console.ReadLine();
         }
     }
}

Output
value of x: 1
value of x: 2
value of x: 3
value of x: 4


4-nested loop
When loops are used inside the other loops, it is known as nested loops.
we can use more than one loop inside any another while, for or do..while loop.
nested loop Example as follows.
using System;

namespace nested_loop
{
    class Program
    {
        static void Main(string[] args)
         {
           /*loop within loop*/ 
         for (int x = 1; x < 5; x++)
         for (int y = 1; y < x; y++)
         Console.WriteLine("Rohit Programming Zone");
         Console.ReadLine();
         }
    }
}

Output
Rohit Programming Zone
Rohit Programming Zone
Rohit Programming Zone
Rohit Programming Zone
Rohit Programming Zone
Rohit Programming Zone


Loop Control Statements
Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.
C# provides the following control statements.

1-break statement
Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch.
break statement Example
using System;
namespace Break_statement
{
    class Program
    {
        static void Main(string[] args)
        {
            /* local variable definition */
            int x= 1;

            /* while loop execution */
            while (x < 10)
            {
                Console.WriteLine("value of x = {0}", x);
                x++;

                if (x >5)
                {
                    /* terminate the loop using break statement */
                    break;
                }
            }
            Console.ReadLine();
        }
    }


Output
value of x = 1
value of x = 2
value of x = 3
value of x = 4
value of x = 5


2- Continue statement
continue statement is used to skip over the execution part of the loop on certain conditions and it's immediate retest its condition prior to reiterating
Continue statement Example as follows.
using System;

namespace continue_statement
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int x = 1; x < 4; x++)
            {
                if (x == 2)
                    continue;

                Console.WriteLine("Rohit");
                
            }
            Console.ReadLine();
        }
    }


Output
Rohit
Rohit

3-Infinite Loop
The loops in which the test conditions do not evaluate false ever tend to execute statements forever until an external force is used to end it and thus they are known as infinite loops. The for loop is traditionally used for this purpose. Since none of the three expressions that form the for loop are required, you can make an endless loop by leaving the conditional expression empty.
Infinite loop Example as follows.

using System;
namespace infinite_loop
{
    class Program
    {
        static void Main(string[] args)
        {
            for (; ; )
                Console.WriteLine("Hello World"); 
        }
    }
}
Output
 // print Infinite times //
Hello world
 ''''''''''''''''''''''''''''