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

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 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


Clap if you believe in unicorns & well-structured paragraphs! 🦄📝

Follow for more updates on

C# Publication | LinkedIn | Instagram | Twitter | Dev.to



To view or add a comment, sign in

More articles by Sukhpinder Singh

Insights from the community

Others also viewed

Explore topics