By 22/07/2015 10 Comments

C# Nested Do-While Loops

  • Declaration of one or more do-while loops inside other do-while loop’s body is called nested do-while loops.
  • The do-while loop which is declared inside other do-while loop is called inner do-while loop.
  • C# Nested Do-While Loops are very useful for creating programs which has more than one dimensions such as rows and columns, a basic calculator layout, different shapes etc.
  • Using nested do-while loops we can access multidimensional array’s elements easily.
  • Nested loops are used for complex programs.

Example:

using System;
namespace csharpBasic
{
    // Start class definition / declaration.
    class Program
    {
        // Static main method void type declaration.
        static void Main(string[] args)
        {
            // Initialize an int type outerLoopNumber with 1.
            int outerLoopNumber = 1;

            // Initialize an int type sum with 1.
            int sum = 1;

            /* Following is do while loop declaration, this loop called an outer loop, 
               execute this loop 3 times according to its condition part.
             */
            do
            { // Outer do-while scope is started.
                int innerLoopNumber = 1;

                do
                { // Inner do-while scope is started.

                    // Print sum variable
                    Console.Write(" {0}", sum);
                    sum += 1;
                    innerLoopNumber++;

                } while (innerLoopNumber <= 3);  // End of inner do-while loop.
                Console.WriteLine();

                outerLoopNumber++;
            } while (outerLoopNumber <= 3);  // End of outer do-while loop.

            Console.ReadKey();

        } // End of main method definition. 
    } // End of class. 

    /*
   The Output will be:
   1 2 3
   4 5 6
   7 8 9
  */
}





Next Tutorial →


← Previous Tutorial


Posted in: C# Basics, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .