---
title: "Understanding Loops in C#"  
description: "They allow us to repeat a block of code multiple times, which is especially useful when dealing with repetitive tasks like processing arrays, running"  
author: "Anubhav Sharma"  
published: 2025-04-22  
updated: 2025-04-22  
canonical: https://www.mindstick.com/articles/339099/understanding-loops-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 3 minutes  

---

# Understanding Loops in C#

Loops are fundamental in programming. They allow us to repeat a block of code [multiple](https://www.mindstick.com/forum/33991/how-to-use-multiple-ng-app-in-a-single-page) times, which is especially useful when dealing with [repetitive tasks](https://answers.mindstick.com/qa/102118/describe-the-role-of-software-in-automating-repetitive-tasks) like [processing](https://www.mindstick.com/forum/12859/how-to-processing-textual-inputs-in-the-client-side) arrays, running calculations, or [displaying data](https://www.mindstick.com/forum/309/displaying-data-in-crystal-report-when-data-is-in-xml-file).

In this article, we’ll explore the different types of loops in **C#**, including:

- `for`
- `while`
- `do...while`
- `foreach`

## Why Use Loops?

Imagine printing numbers from 1 to 100 manually — it’s inefficient and error-prone. With loops, we can [automate](https://www.mindstick.com/blog/12040/how-to-automate-your-business) such tasks with just a few lines of code.

### 1. `for` Loop

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

## Syntax:

```cs
for (initialization; condition; increment)
{
    // Code to execute
}
```

## Example:

```cs
for (int i = 1; i <= 5; i++)
{
    Console.WriteLine("Count: " + i);
}
```

## Output:

```plaintext
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
```

### 2. `while` Loop

The `while` loop runs **as long as the [condition](https://www.mindstick.com/forum/160913/writing-a-query-to-update-records-based-on-a-condition-in-sql-server) is true**. It’s useful when you don’t know how many times the loop should run.

## Syntax:

```cs
while (condition)
{
    // Code to execute
}
```

## Example:

```cs
int i = 1;
while (i <= 5)
{
    Console.WriteLine("Number: " + i);
    i++;
}
```

### 3. `do...while` Loop

This loop is similar to `while`, but it **executes the code block at least once**, even if the condition is false.

## Syntax:

```cs
do
{
    // Code to execute
}
while (condition);
```

## Example:

```cs
int i = 1;
do
{
    Console.WriteLine("Value: " + i);
    i++;
}
while (i <= 5);
```

### 4. `foreach` Loop

The `foreach` loop is used to **iterate over [collections](https://www.mindstick.com/articles/12458/what-are-collections-in-c-sharp)** like arrays, lists, or any enumerable data.

## Syntax:

```cs
foreach (datatype item in collection)
{
    // Code to execute
}
```

## Example:

```cs
string[] fruits = { "Apple", "Banana", "Cherry" };

foreach (string fruit in fruits)
{
    Console.WriteLine(fruit);
}
```

## Output:

```plaintext
Apple
Banana
Cherry
```

### Breaking and Continuing Loops

You can control loop [execution](https://www.mindstick.com/blog/178/synchronous-and-asynchronous-command-execution-in-c-sharp-dot-net) using:

| Keyword | Description |
| --- | --- |
| `break` | Exits the loop entirely |
| `continue` | Skips the current iteration and moves to the next |

**Example with** `break`**:**

```cs
for (int i = 1; i <= 10; i++)
{
    if (i == 6) break;
    Console.WriteLine(i);
}
```

## Output:

```plaintext
1 2 3 4 5
```

**Example with** `continue`**:**

```cs
for (int i = 1; i <= 5; i++)
{
    if (i == 3) continue;
    Console.WriteLine(i);
}
```

## Output:

```plaintext
1 2 4 5
```

### Summary Table

| Loop Type | Best Used When... |
| --- | --- |
| `for` | Number of iterations is known |
| `while` | Loop until a condition becomes false |
| `do...while` | Run at least once, then check condition |
| `foreach` | Iterating over a [collection](https://www.mindstick.com/interview/2199/what-is-the-collections-api-in-java) (array/list) |

---

Original Source: https://www.mindstick.com/articles/339099/understanding-loops-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
