Multiple statements in a for loop

A for loop can contain multiple statements in the iterator section

Home DailyDrop

Daily Knowledge Drop

Not only can the iterator section (the last section) in a for loop contain multiple operations, but it can also contain statements.


Iterator section

Generally, when defining and using a for loop, the iterator section only increases (or decreased) the initializer variable, loopCount in the below example:

for (var loopCount = 0; loopCount < 100; loopCount++) 
{ 
}

This iterator section can actually contain statements, separated by a comma!


Operations

In this simple example, each loop handles its iterations with the i variable, but also decreases a shared progress variable representing overall progress:

var valueArray = new int[] { 100, 25, 70 };
var progress = valueArray.Sum();

for (int i = 0; i < valueArray[0]; i++, progress--)
{
    // Simulate some processing
    Thread.Sleep(10);
    
    Console.Clear();
    Console.WriteLine($"{progress} items remaining to be processed");
}

for (int i = 0; i < valueArray[1]; i++, progress--)
{
    // Simulate some processing
    Thread.Sleep(10);
    
    Console.Clear();
    Console.WriteLine($"{progress} items remaining to be processed");
}

for (int i = 0; i <= valueArray[2]; i++, progress--)
{
    // Simulate some processing
    Thread.Sleep(10);
    
    Console.Clear();
    Console.WriteLine($"{progress} items remaining to be processed");
}

Statements

The iterator section can doesn't only have to contain operations, but can also contain statements.

For example purposes, consider logging is to be performed for each record being processed - you might do it as follows:

for (int i = 1; i <= recordsToProcess.Count(); i++)
{
    // log some information about the record
    Console.WriteLine($"Current value of process record: {recordsToProcess[i]}");

    // Simulate some processing
    Thread.Sleep(10);
}

The keep the code contained within the for loop block clear, one could move the log statement into the for loop iterator section:

// logging moved to into the for loop statement
for (int i = 1; i <= recordsToProcess.Count(); 
    Console.WriteLine($"Current value of process record: {recordsToProcess[i]}"), i++)
{
    // actual code block is cleaner

    // Simulate some processing
    Thread.Sleep(10);
}

Methods

Expanding on the previous example, methods can also be invoked in the iterator section - the logging can be moved to its own method to keep the code slightly cleaner:

for (int i = 0; i <= recordsToProcess.Count(); LogRecord(recordsToProcess[i]), i++)
{
    // Simulate some processing
    Thread.Sleep(10);
}

// ---------------------

void LogRecord(int recordValue)
{
    Console.WriteLine($"Current value of process record: {recordValue}");
}

Notes

This bit of knowledge does have limited practical application, and one could argue that it makes the code harder to read - however it does have it's place, and I think could be especially useful in the first example to keep track of multiple "progress-type" counters.


References

Khalid Abuhakmeh Tweet

Daily Drop 107: 30-06-2022

At the start of 2022 I set myself the goal of learning one new coding related piece of knowledge a day.
It could be anything - some.NET / C# functionality I wasn't aware of, a design practice, a cool new coding technique, or just something I find interesting. It could be something I knew at one point but had forgotten, or something completely new, which I may or may never actually use.

The Daily Drop is a record of these pieces of knowledge - writing about and summarizing them helps re-enforce the information for myself, as well as potentially helps others learn something new as well.
c# .net for loop iteration