C# Tutorial
C# String
C# Array
C# Flow Control
C# Class and Object
C# Inheritance
C# Interface
C# Collection
C# Generic
C# File I/O
C# Delegate and Event
C# Exception
C# Process and Thread
C# ADO.NET Database Operations
In C#, the do...while
loop is a control flow statement that executes a block of code repeatedly as long as a specified condition is true. The key difference between a do...while
loop and a while
loop is that the do...while
loop executes the code block at least once, even if the condition is false initially. This is because the condition is checked after the block has been executed.
In this tutorial, we'll cover the basic structure of a do...while
loop and how to use it.
The basic structure of a do...while
loop is as follows:
do { // code block to be executed } while (condition);
The code block inside the loop will be executed at least once. After each iteration, the condition is checked. If the condition evaluates to true, the loop will continue to execute; otherwise, the loop will terminate.
Here's an example demonstrating the use of a do...while
loop:
using System; namespace DoWhileLoopTutorial { class Program { static void Main(string[] args) { int counter = 0; do { Console.WriteLine("Counter: " + counter); counter++; } while (counter < 5); } } }
In this example, the do...while
loop prints the value of the counter
variable and increments it by 1 after each iteration. The loop will continue as long as counter
is less than 5.
A do...while
loop is useful in situations where you need to execute a block of code at least once, regardless of the initial condition. For example, when you want to read user input until it's valid:
using System; namespace DoWhileLoopUseCase { class Program { static void Main(string[] args) { int number; bool isValid; do { Console.Write("Please enter a number between 1 and 10: "); string input = Console.ReadLine(); isValid = int.TryParse(input, out number) && number >= 1 && number <= 10; } while (!isValid); Console.WriteLine("You entered a valid number: " + number); } } }
In this example, the do...while
loop reads the user input and checks if it's a valid integer between 1 and 10. The loop continues until the user enters a valid input.
This tutorial demonstrates the basics of the do...while
loop in C# and how to use it for various tasks.
C# do...while loop example:
do...while
loop in C# is used to execute a block of code at least once and then repeatedly execute it while a specified condition is true.using System; class Program { static void Main() { int count = 0; do { Console.WriteLine($"Iteration {count}"); count++; } while (count < 5); } }
Using do...while loop for user input in C#:
do...while
loops are commonly used for user input validation to ensure that the code inside the loop executes at least once.using System; class Program { static void Main() { string userInput; do { Console.Write("Enter 'exit' to quit: "); userInput = Console.ReadLine(); } while (userInput.ToLower() != "exit"); } }
Breaking out of a do...while loop in C#:
break
statement can be used to exit a do...while
loop prematurely based on a specific condition.using System; class Program { static void Main() { int count = 0; do { Console.WriteLine($"Iteration {count}"); count++; if (count == 3) break; } while (count < 5); } }
Nested do...while loops in C#:
do...while
loops can be nested, allowing for more complex loop structures.using System; class Program { static void Main() { int outerCount = 0; do { int innerCount = 0; do { Console.WriteLine($"Outer: {outerCount}, Inner: {innerCount}"); innerCount++; } while (innerCount < 3); outerCount++; } while (outerCount < 2); } }
Infinite loop with do...while in C#:
do...while
loops to avoid unintentional infinite loops.using System; class Program { static void Main() { do { Console.WriteLine("This will create an infinite loop!"); } while (true); } }