Saturday, 2 July 2016

Looping constructs

Looping constructs
v  The following loop constructs are supported by C#:
Ø  The while loop
Ø  The do…while loop
Ø  The for loop

Ø  foreach loop


The while loop


The while statement executes a statement or a block of statements until a specified expression evaluates to false.
It should be used if number of iteration is not known

Example
using System;

namespace while_loop
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter a Number");
            int numb = int.Parse(Console.ReadLine());
            int reverse = 0;
            while (numb > 0)
            {
                int rem = numb % 10;
                reverse = (reverse * 10) + rem;
                numb = numb / 10;

            }
            Console.WriteLine("Reverse number={0}", reverse);
            Console.ReadLine();
        }
    }
}



The do…while loop


Unlike while loops, which test the loop condition at the start of the loop, the do...while loop checks its condition at the end of the loop.

A do...while loop iterates the elements for the infinite number of times like while loop. But, code is executed at least once whether condition is true or false.

Example


using System;
namespace do_while
{
    class Program
    {
        static void Main(string[] args)
        {
            int table, i, res;
            table = 12;
            i = 1;
            do
            {
               res = table * i;
               Console.WriteLine("{0} x {1} = {2}", table, i, res);
               i++;
            }
     // must put semi-colon(;) at the end of while condition in do...while loop.
            while (i <= 10);

            Console.ReadLine();
        }
    }

}


The for loop


The for loop iterates the elements for the fixed number of times. It should be used if number of iteration is known.

Example

using System;
namespace for_loop
{
    class Program
    {
        static void Main(string[] args)
        {
            int i;
            for (i = 1; i <= 10; i++)
            {
                Console.WriteLine("2 * "+i+" = "+2*i);
            }
            Console.ReadLine();
        }
    }

}


foreach loop

foreach loop is a different kind of looping constructs in C# programming that doesn’t includes initialization, termination and increment/decrement characteristics. It uses collection to take value one by one and then processes them.

Example


using System;

namespace foreach_loop
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] name = new string[5]; // declaring array

            //Storing value in array element
            name[0] = "amit";
            name[1] = "anil";
            name[2] = "jay";
            name[3] = "anuj";
            name[4] = "akshay";

            //retrieving value using foreach loop
            foreach (string n in name)
            {
                Console.WriteLine("Hello " + n);
            }
            Console.ReadLine();
        }
    }

}


No comments:

Post a Comment