Coding Schools


 
Python | C Sharp | Azure AI | HTML | JavaScript | CSS | SQL Server
OOPS in C#
C# Data Types
Boxing and Unboxing in C#
Garbage Collection in C#
C# - Conditional Statements
C# - Loops
Interfaces in C#
Generics in C#
Collections in C#
C# 8.0 new features
C# Singleton Design Pattern
C# Factory Design Pattern
LINQ in C#
C# - Program to Find Prime Numbers
C# - Fibonacci Sequence
C# - Factorial of a number
C# - Recursive methods
C# - Anonymous Methods
C# - String Methods
C# TDD - XUnit
C# TDD - NUnit
C# Multiton Design Pattern
C# Facade Design Pattern
C# Abstract Factory Design Pattern
C# Decorator Design Pattern
C# Composite Design Pattern

C# - Loops



In C#, loops are used to execute a block of code repeatedly until a specified condition is met. Here are the main types of loops you'll encounter in C#: for, while, do..while, and foreach.

1. for Loop

The for loop is used when you know in advance how many times you want to execute a statement or a block of statements.

Syntax of for Loop:

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

Example of for Loop:

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

This loop will print the numbers 0 through 4.

2. while Loop

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

Syntax of while Loop:

csharp
while (condition)
{
    // Code to be executed
}

Example of while Loop:

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

This loops continuously checks the condition before executing the block of code.

3. do..while Loop

The do..while loop is similar to the while loop, but it checks the condition after executing the block of code. This guarantees the code block is executed at least once.

Syntax of do..while Loop:

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

Example of do..while Loop:

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

This loop ensures that the block of code runs at least once before checking the condition.

4. foreach Loop

The foreach loop is used to iterate over elements in a collection, such as an array or a list, without needing an explicit index.

Syntax of foreach Loop:

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

Example of foreach Loop:

csharp
string[] fruits = { "Apple", "Banana", "Cherry" };
foreach (string fruit in fruits)
{
    Console.WriteLine(fruit);
}

This loop will print each fruit in the fruits array.

Example Code:

Here's a complete example showing all types of loops:

csharp
using System;

class Program
{
    static void Main()
    {
        // for loop example
        for (int i = 0; i < 3; i++)
        {
            Console.WriteLine("for loop iteration: " + i);
        }

        // while loop example
        int j = 0;
        while (j < 3)
        {
            Console.WriteLine("while loop iteration: " + j);
            j++;
        }

        // do..while loop example
        int k = 0;
        do
        {
            Console.WriteLine("do..while loop iteration: " + k);
            k++;
        }
        while (k < 3);

        // foreach loop example
        string[] colors = { "Red", "Green", "Blue" };
        foreach (string color in colors)
        {
            Console.WriteLine("foreach loop iteration: " + color);
        }
    }
}

These loops provide a versatile way to repeatedly execute code in your programs. If you have any specific questions or need more detailed examples, feel free to ask!




All rights reserved | Privacy Policy | Sitemap