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 while
loop is a control flow statement that allows you to execute a block of code repeatedly as long as a specified condition remains true. In this tutorial, we'll cover the basics of using the while
loop in C#.
The basic syntax of a while
loop consists of the while
keyword, followed by a condition in parentheses, and then a block of code in curly braces.
while (condition) { // Code to be executed }
Here is a simple example of a while
loop that counts from 1 to 5:
int counter = 1; while (counter <= 5) { Console.WriteLine(counter); counter++; } // Output: // 1 // 2 // 3 // 4 // 5
An infinite loop is a loop that continues indefinitely because its condition never becomes false. Be cautious with infinite loops, as they can cause your program to become unresponsive or crash.
// Warning: This is an infinite loop while (true) { Console.WriteLine("This loop will run forever"); }
You can nest while
loops inside other while
loops to create more complex looping structures.
int outerCounter = 1; while (outerCounter <= 3) { Console.WriteLine("Outer loop iteration: " + outerCounter); int innerCounter = 1; while (innerCounter <= 3) { Console.WriteLine(" Inner loop iteration: " + innerCounter); innerCounter++; } outerCounter++; } // Output: // Outer loop iteration: 1 // Inner loop iteration: 1 // Inner loop iteration: 2 // Inner loop iteration: 3 // Outer loop iteration: 2 // Inner loop iteration: 1 // Inner loop iteration: 2 // Inner loop iteration: 3 // Outer loop iteration: 3 // Inner loop iteration: 1 // Inner loop iteration: 2 // Inner loop iteration: 3
In this tutorial, we covered the basics of using the while
loop in C#. The while
loop is a control flow statement that allows you to execute a block of code repeatedly as long as a specified condition remains true. You can use simple while
loops, infinite loops, or nested loops to achieve various looping structures in your C# programs.
C# while loop examples
The while
loop in C# is used to repeatedly execute a block of code as long as the specified condition is true.
int count = 0; while (count < 5) { Console.WriteLine("Iteration: " + count); count++; }
Infinite while loop in C#
An infinite while
loop continues to execute as long as the condition remains true. It's essential to include a break condition to avoid infinite loops.
while (true) { // Infinite loop }
Breaking out of a while loop in C#
Use the break
statement to exit a while
loop based on a certain condition.
int count = 0; while (true) { Console.WriteLine("Iteration: " + count); if (count == 4) break; count++; }
Nested while loops in C#
while
loops can be nested within each other to create more complex looping structures.
int outer = 0; while (outer < 3) { int inner = 0; while (inner < 2) { Console.WriteLine($"Outer: {outer}, Inner: {inner}"); inner++; } outer++; }
Do-while loop in C#
The do-while
loop ensures that the loop body is executed at least once before checking the loop condition.
int count = 0; do { Console.WriteLine("Iteration: " + count); count++; } while (count < 5);
Using while loop for user input in C#
while
loops are commonly used to obtain user input until a specified condition is met.
string userInput; while (true) { Console.WriteLine("Enter 'exit' to end: "); userInput = Console.ReadLine(); if (userInput.ToLower() == "exit") break; }
C# while loop vs. for loop
Both while
and for
loops are used for iteration. The key difference is that while
loops are more flexible, allowing you to control the loop condition and initialization separately.
int count = 0; while (count < 5) { Console.WriteLine("While Loop: " + count); count++; } for (int i = 0; i < 5; i++) { Console.WriteLine("For Loop: " + i); }
Common mistakes with while loops in C#
Common mistakes include forgetting to increment or update the loop variable, resulting in an infinite loop.
int count = 0; // Incorrect: Missing count++ while (count < 5) { Console.WriteLine("Iteration: " + count); }
Conditional statements with while loops in C#
while
loops can be combined with conditional statements to create more dynamic looping behavior.
int count = 0; while (count < 5) { if (count % 2 == 0) Console.WriteLine("Even: " + count); else Console.WriteLine("Odd: " + count); count++; }