Skip to content

Commit

Permalink
Merge pull request #1561 from mahaveergurjar/loops-in-C#
Browse files Browse the repository at this point in the history
Loops in c#
  • Loading branch information
ajay-dhangar authored Nov 1, 2024
2 parents 23c09bf + 176e3e4 commit a384491
Showing 1 changed file with 145 additions and 0 deletions.
145 changes: 145 additions & 0 deletions docs/languages/csharp/csharp-4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
---
id: loops-in-csharp
sidebar_position: 4
title: "Loops in C#"
sidebar_label: "Loops in C#"
---

# Loops in C#

Loops in C# allow you to execute a block of code repeatedly based on specific conditions.

---

## 1. For Loop

The `for` loop is commonly used when the number of iterations is known.

### Syntax:

```csharp
for (initialization; condition; increment/decrement) {
// code to be executed
}
```

### Example:

```csharp
for (int i = 0; i < 5; i++) {
Console.WriteLine("Iteration " + i);
}
```

---

## 2. While Loop

The `while` loop executes a block of code as long as the specified condition is true.

### Syntax:

```csharp
while (condition) {
// code to be executed
}
```

### Example:

```csharp
int i = 0;
while (i < 5) {
Console.WriteLine("Iteration " + i);
i++;
}
```

---

## 3. Do-While Loop

The `do-while` loop is similar to the `while` loop but guarantees the code block will execute at least once.

### Syntax:

```csharp
do {
// code to be executed
} while (condition);
```

### Example:

```csharp
int i = 0;
do {
Console.WriteLine("Iteration " + i);
i++;
} while (i < 5);
```

---

## 4. Foreach Loop

The `foreach` loop is used to iterate over collections, such as arrays or lists.

### Syntax:

```csharp
foreach (type variable in collection) {
// code to be executed
}
```

### Example:

```csharp
int[] numbers = {1, 2, 3, 4, 5};
foreach (int num in numbers) {
Console.WriteLine("Number: " + num);
}
```

---

## 5. Break and Continue Statements

### a. Break Statement

The `break` statement exits a loop prematurely.

#### Example:

```csharp
for (int i = 0; i < 10; i++) {
if (i == 5) {
break; // Exit the loop when i equals 5
}
Console.WriteLine("Iteration " + i);
}
```

### b. Continue Statement

The `continue` statement skips the current iteration and proceeds to the next one.

#### Example:

```csharp
for (int i = 0; i < 5; i++) {
if (i == 2) {
continue; // Skip the iteration when i equals 2
}
Console.WriteLine("Iteration " + i);
}
```

---

## Summary

Loops are essential in C# for repeating tasks and iterating over data structures efficiently. Understanding these loops and how to control them with `break` and `continue` will help you write more effective code.

Happy coding!

0 comments on commit a384491

Please sign in to comment.