Loops

There are times when you would want to run the same block of code several times, for it C# has 4 different types of loop structures. FOR Loop: It executes a sequence of statements multiple

There are times when you would want to run the same block of code several times, for it C# has 4 different types of loop structures.

FOR Loop:

It executes a sequence of statements multiple times. It requires a loop counter to keep track of the loop execution.

using System;

namespace Loop
{

    class Program
    {

        static void Main(string[] args)
        {
            Console.WriteLine("Find all numbers b/w 2 values");
            Console.WriteLine("Please enter a start value");
            Int32.TryParse(Console.ReadLine(), out int a);

            Console.WriteLine("Please enter a end value");
            Int32.TryParse(Console.ReadLine(), out int b);

            for (int i = a; i <= b; i++)
            {
                Console.Write("{0},", i);
            }

            Console.ReadKey();
        }
    }
}

FOREACH Loop:

Similar to FOR loop FOREACH also does the same thing. The only difference is that the “FOREACH” loop is used to iterate over the elements of the collection. Instead of declaring and initializing a loop counter variable like (int i), you declare a variable that is the same type as the base type of the array, followed by a colon, which is then followed by the array name.

using System;

namespace Loop
{

    class Program
    {

        static void Main(string[] args)
        {
            Console.WriteLine("Print array:");

            int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7 };

            foreach (int items in array)
            {
                Console.WriteLine(items);
            }

            Console.WriteLine("Print array and var:");
            //we can also use var to store the array value
            foreach (var param in array)
            {
                Console.WriteLine(param);
            }

            Console.ReadKey();
        }
    }
}

While:

It repeats a group of statements while a given condition is true. It tests the condition before executing the loop body.

using System;

namespace Loop
{

    class Program
    {

        static void Main(string[] args)
        {
            int loop = 10;

            while (loop < 20)
            {
                Console.WriteLine("value of a: {0}", loop);
                loop++;
            }

            Console.ReadKey();
        }
    }
}

Do While:

It tests the condition at the end of the loop. So it is guaranteed to execute at least one time.

using System;

namespace Loop
{

    class Program
    {

        static void Main(string[] args)
        {
            int loop = 10;

            do
            {
                Console.WriteLine("value of a: {0}", loop);
                loop++
            }
            while (loop < 20);

            Console.ReadKey();
        }
    }
}

Leave a Reply

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