DEV Community

Cover image for Day 4 of 30-Day .NET Challenge: For Loops
Sukhpinder Singh
Sukhpinder Singh

Posted on • Originally published at singhsukhpinder.Medium

2 1 1 1 1

Day 4 of 30-Day .NET Challenge: For Loops

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Execute the code from the main method as follows

#region Day 4 - For Loops

ForLoops.ForLoopExample();

#endregion
Enter fullscreen mode Exit fullscreen mode

Console Output

// Console Output
0
1
2
3
4
5
6
7
8
9
Enter fullscreen mode Exit fullscreen mode

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Execute the code from the main method as follows

#region Day 4 - For Loops

ForLoops.BackwardForLoopExample();

#endregion
Enter fullscreen mode Exit fullscreen mode

Console Output

// Console Output
10
9
8
7
6
5
4
3
2
1
0
Enter fullscreen mode Exit fullscreen mode

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Execute the code from the main method as follows

#region Day 4 - For Loops

ForLoops.IterationForLoopExample();

#endregion
Enter fullscreen mode Exit fullscreen mode

Console Output

// Console Output
0
3
6
9
Enter fullscreen mode Exit fullscreen mode

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;
        }
    }
Enter fullscreen mode Exit fullscreen mode

Execute the code from the main method as follows

    #region Day 4 - For Loops

    ForLoops.BreakForLoopExample();

    #endregion
Enter fullscreen mode Exit fullscreen mode

Console Output

    // Console Output
    0
    1
    2
    3
    4
    5
    6
    7
Enter fullscreen mode Exit fullscreen mode

Complete Code on GitHub

GitHub — ssukhpinder/30DayChallenge.Net

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 | LinkedIn | Dev.to | Hashnode | Newsletter | Tumblr

Visit our other platforms: GitHub | Instagram | Tiktok | Quora | Daily.dev

More content at C# Programming

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay