C# Do-While Loop

HTML
CSS
C#
SQL

while and do-while to Loops in C#:

while Loop:

In C#, a while loop is a control flow statement that repeatedly executes a block of code as long as a specified condition is true. It’s often used when you don’t know in advance how many times you need to execute the loop but want to keep executing as long as a condition remains true.

While Loop Statement

using System;

class Program

{

    static void Main()

    {

        // Initialize a counter variable

        int count = 1;

        // Execute the loop while the count is less than or equal to 5

        while (count <= 5)

        {

            // Print the current count

            Console.WriteLine(“Count: ” + count);

            // Increment the count for the next iteration

            count++;

        }

        // Print a message after the loop completes

        Console.WriteLine(“Loop has ended.”);

    }

}

Output

   Count: 1

   Count: 2

   Count: 3

   Count: 4

   Count: 5

   Loop has ended.

Nested While loop Statement

using System;

 

public class SquareExample

{

    public static void Main(string[] args)

    {

        int sideLength = 4; // Change the side length as needed

        int row = 1;

 

        // Outer loop for rows

        while (row <= sideLength)

        {

            int column = 1;

            // Inner loop for columns

            while (column <= sideLength)

            {

                Console.Write(“* “);

                column++;

            }

            Console.WriteLine(); // Move to the next line after completing each row

            row++;

        }

    }

}

Output

* * * *

* * * *

* * * *

* * * *

Do While Loop

A do-while loop in C# is similar to a while loop, but with one key difference: it always executes its block of code at least once before checking the loop condition. After the first execution, it continues to execute as long as the specified condition remains true.

Do while loop Statement

using System;

class Program

{

    static void Main()

    {

        // Initialize a counter variable

        int count = 1;

        // Execute the loop at least once, and then as long as the count is less than or equal to 5

        do

        {

            // Print the current count

            Console.WriteLine(“Count: ” + count);

            // Increment the count for the next iteration

            count++;

        }

        while (count <= 5);

        // Print a message after the loop completes

        Console.WriteLine(“Loop has ended.”);

    }

}

Output

  Count: 1

   Count: 2

   Count: 3

   Count: 4

   Count: 5

   Loop has ended.

Course Video

Practices Tasks

1. In our group, there are 5 friends if any one friend is not there in the group we all will not go shopping.

For example: first take an array of string with 4 friends’ name and then use a while loop if the length of the array is less than 5 then show the below message and please use a break statement otherwise loop will never end 

2. Imagine you are going with your 10 friends to the restaurant but in that restaurant, your favorite dish is not more than 4 plates, and you want 10 plates. Order another dish if your favorite dish is less than 10 plates using while loop

For example: first take an int variable name as a favorite with value 8, then check in the while loop if the favorite value is less than 10, then show the below message, and don’t forget to use break.

3. Imagine we are playing ludo, and until 6 comes in dice, we can’t open our  game pieces 

For example: take a method of random and create a variable with the name dice value, run do while loop, check-in, and if dice value = 6, then show the below message

If dice value is not equal to 6, then it should run like this until it does not come 6

4. Many times social media, we reset our passwords When we create new password, there is a confirmation password. If we do not add proper password as above, it will again tell us to add new password from start, so you must do this using  do while loop

Prompt for a new password after entering new password click enter to confirm password

Prompt for a confirm password 

After entering confirm password check both are similar or not 

If yes 

If no then again ask for a new password and then confirm password