Loops in C sharp



There may be times when you want to execute certain code of lines multiple times. In C sharp , there are certain loop structure who's function is to execute lines of code multiple times until the condition specified is true.




  • WHILE loop- Repeatedly executes a group of statements until given condition is true. The condition is tested before execution of statements.

Syntax

         
           while(Condition)
           {
                    //group of statements to execute;
           }


Here the group of statements are the statements which needs to be executed if the condition provided in WHILE loop is true. The statements are executed in order. i.e first statement will execute first, second will execute second. If the Condition provided in WHILE loop is not true , then the WHILE loop will be skipped and the statements after the loop will be executed.
Program.


using System;
namespace constant_var_program
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 5;
            while (a < 10)
            {
                Console.WriteLine("Value of a {0}", a);
                a++;
             
            }
            Console.ReadKey();

        }
    }
}


Output
value of a 5
value of a 6
value of a 7
value of a 8
value of a 9



  • DO..WHILE loop-  unlike WHILE and FOR loop, DO..WHILE loop tests the condition at the end of the loop. Means the statements will be executed at least once even if the condition is wrong.
  • After checking the condition , if the condition is true , then the control will go to start of the loop again and will continue to execute until the condition is false.


Syntax

           do
          {
                    //statements;

          }while(condition);

Program


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace constant_var_program
{
    class Program
    {
        static void Main(string[] args)
        {
            int k=1;
            do
            {
                Console.WriteLine("value of k is {0}", k);
                k++;
            } while (k < 10);
            Console.ReadKey();
        }
    }
}



Output



value of k 1
value of k 2
value of k 3
value of k 4
value of k 5
value of k 6
value of k 7
value of k 8
value of k 9



  • FOR loop- unlike other loops , FOR loop is more efficient in syntax and execution.
Syntax

         

          for(initialisation;condition;increment)
         {
               //Statements ;
         }


In Initialization step , the variables for the loop will be declared and initialized. This step is executed only once.
Next in Condition step , the condition will be specified. If the condition is true , then the statements in body of the FOR loop will be executed. Otherwise the body of the loop is skipped and the control goes to the next statement after the loop.
After the Condition evaluates to be true and the body of the loop is executed then the control is transferred to the Increment step. In this step , the loop control variables can be updated by any number.
Program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace constant_var_program
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 5; i++)
            {
                Console.WriteLine("Value of i {0}",i);
             
            }
            Console.ReadKey();
        }
    }
}



Output

value of i 1
value of i 2
value of i 3
value of i 4
value of i 5


We will be working on advanced program based on this basic.
Loops in C sharp Loops in C sharp Reviewed by vishal on July 30, 2017 Rating: 5

No comments