## C# `while` Loop: Explanation, Theory, and Examples
### Explanation
A `while` loop in C# is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The loop continues to execute as long as the condition remains true.
### Theory
The `while` loop consists of two main parts:
1. **Condition**: This boolean expression is evaluated before each iteration of the loop. If the condition is true, the loop body is executed; if false, the loop terminates.
2. **Loop Body**: This is the block of code that is executed repeatedly as long as the condition is true.
The basic syntax of a `while` loop in C# is:
```csharp
while (condition)
{
// Code to be executed
}
```
### Diagram
Here is a flowchart representing the C# `while` loop:
```
+--------------------+
| Condition |
+--------------------+
|
v
+-------------+------------+
| | |
| true | false |
| v |
| +----------------+ |
| | Execute Code | |
| +----------------+ |
| | |
| v |
+-------------+------------+
```
### Examples
#### Example 1: Printing Numbers from 1 to 5
Here is an example of a `while` loop in C# that prints numbers from 1 to 5:
```csharp
using System;
class Program
{
static void Main()
{
int i = 1;
while (i <= 5)
{
Console.WriteLine(i);
i++;
}
}
}
```
**Explanation**:
- **Condition**: `i <= 5;` continues the loop as long as `i` is less than or equal to 5.
- **Loop Body**: `Console.WriteLine(i);` prints the value of `i` and `i++;` increments `i` by 1 after each iteration.
**Output**:
```
1
2
3
4
5
```
#### Example 2: Reading User Input Until "exit" is Entered
Here is an example of a `while` loop in C# that reads user input repeatedly until the user types "exit":
```csharp
using System;
class Program
{
static void Main()
{
string input = "";
while (input != "exit")
{
Console.Write("Enter text (type 'exit' to quit): ");
input = Console.ReadLine();
Console.WriteLine("You entered: " + input);
}
}
}
```
**Explanation**:
- **Condition**: `input != "exit";` continues the loop as long as the input is not "exit".
- **Loop Body**: `Console.ReadLine();` reads user input and `Console.WriteLine();` prints it.
**Output**:
```
Enter text (type 'exit' to quit): hello
You entered: hello
Enter text (type 'exit' to quit): world
You entered: world
Enter text (type 'exit' to quit): exit
You entered: exit
```
#### Example 3: Summing Numbers Until a Negative Number is Entered
Here is an example of a `while` loop in C# that sums numbers entered by the user until a negative number is entered:
```csharp
using System;
class Program
{
static void Main()
{
int sum = 0;
int number;
Console.WriteLine("Enter numbers to sum (negative number to stop):");
while (true)
{
number = int.Parse(Console.ReadLine());
if (number < 0) break;
sum += number;
}
Console.WriteLine("Sum: " + sum);
}
}
```
**Explanation**:
- **Condition**: The loop continues indefinitely (`while (true)`) until a negative number is entered.
- **Loop Body**: `int.Parse(Console.ReadLine());` reads and parses user input, and `sum += number;` adds the number to `sum`.
- **Break**: The loop breaks if a negative number is entered.
**Output**:
```
Enter numbers to sum (negative number to stop):
10
20
-1
Sum: 30
```
### Summary
A `while` loop in C# is a versatile construct that executes a block of code repeatedly based on a given condition. It is used when the number of iterations is not known beforehand, and the loop continues as long as the condition remains true. This makes the `while` loop suitable for scenarios where you need to keep processing data or user input until a specific condition is met.
No comments:
Post a Comment
Thanks for the contribution, our team will check and reply back if response required.