Day 4 of 30-Day .NET Challenge: For Loops
Tags: beginners, webdev, csharp, dotnet
Published: 2024-03-23 05:25:51

## Introduction

Welcome to this module Day 4 of 30-Day .NET Challenge: For Loops , where let's dive into the world of for statements. Explore how to write for statements that iterate a set number of times.

### Learning Objectives

 1. Utilize the for statement to iterate through a set of code.

### Prerequisites for Developers

* Proficiency with the foreach iteration statement.

* Familiarity with working with variables.

## Getting Started

### What is the for statement?

The for statement allows you to iterate through a code block a fixed number of times, providing precise control over the iteration process.

### Basic For Loop Example

To begin, create a static class file called “ForLoop.cs” within the console application. Insert the provided code snippet into this file.

    /// <summary>
    /// Outputs
    /// 0
    /// 1
    /// 2
    /// 3
    /// 4
    /// 5
    /// 6
    /// 7
    /// 8
    /// 9
    /// </summary>
    public static void ForLoopExample()
    {
        for (int i = 0; i < 10; i++)
        {
            Console.WriteLine(i);
        }
    }

Execute the code from the main method as follows

    #region Day 4 - For Loops
    
    ForLoops.ForLoopExample();
    
    #endregion

### Console Output

    // Console Output
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9

### Run For Loop in reverse

The goal is to iterate through a code block while counting down instead of counting up.

Add another method into the same static class as shown below

    /// <summary>
    /// Outputs
    /// 10
    /// 9
    /// 8
    /// 7
    /// 6
    /// 5
    /// 4
    /// 3
    /// 2
    /// 1
    /// 0
    /// </summary>
    public static void BackwardForLoopExample()
    {
        for (int i = 10; i >= 0; i--)
        {
            Console.WriteLine(i);
        }
    }

Execute the code from the main method as follows

    #region Day 4 - For Loops
    
    ForLoops.BackwardForLoopExample();
    
    #endregion

### Console Output

    // Console Output
    10
    9
    8
    7
    6
    5
    4
    3
    2
    1
    0

### Iterative Pattern With For

The goal is to skip specific values in the iterator variable. Add another method into the same static class as shown below

    /// <summary>
    /// Outputs
    /// 3
    /// 6
    /// 0
    /// 9
    /// </summary>
    public static void IterationForLoopExample()
    {
        for (int i = 0; i < 10; i += 3)
        {
            Console.WriteLine(i);
        }
    }

Execute the code from the main method as follows

    #region Day 4 - For Loops
    
    ForLoops.IterationForLoopExample();
    
    #endregion

### Console Output

    // Console Output
    0
    3
    6
    9

### Break Loop

The goal is to exit the iteration statement prematurely based on some conditions. Add another method into the same static class as shown below
```
    /// <summary>
    /// Outputs
    /// 0
    /// 1
    /// 2
    /// 3
    /// 4
    /// 5
    /// 6
    /// 7
    /// </summary>
    public static void BreakForLoopExample()
    {
        for (int i = 0; i < 10; i++)
        {
            Console.WriteLine(i);
            if (i == 7) break;
        }
    }
```
Execute the code from the main method as follows
```
    #region Day 4 - For Loops
    
    ForLoops.BreakForLoopExample();
    
    #endregion
```
### Console Output
```
    // Console Output
    0
    1
    2
    3
    4
    5
    6
    7
```
### Complete Code on GitHub
[GitHub — ssukhpinder/30DayChallenge.Net](https://github.com/ssukhpinder/30DayChallenge.Net/tree/main)

### C# Programming🚀

Thank you for being a part of the C# community! Before you leave:

#### If you’ve made it this far, please show your appreciation with a clap and follow the author! 👏️️

Follow us: [X](https://twitter.com/sukhsukhpinder) | [LinkedIn](https://www.linkedin.com/in/sukhpinder-singh/) | [Dev.to](https://dev.to/ssukhpinder) | [Hashnode](https://dotnet.hashnode.dev/) | [Newsletter](https://www.linkedin.com/newsletters/net-programming-7031098498754195456/) | [Tumblr](https://www.tumblr.com/settings/blog/codewithsukh)

Visit our other platforms: [GitHub](https://github.com/ssukhpinder) | [Instagram](https://www.instagram.com/codewithsukh/) | [Tiktok](https://www.tiktok.com/@codewithsukh) | [Quora](https://www.quora.com/profile/Sukhpinder-Singh-4) | [Daily.dev](https://app.daily.dev/devcard)

More content at [C# Programming](https://medium.com/c-sharp-progarmming)