C# Tutorial - Iteration Statements

C# Tutorial - Iteration Statements

Last Revision: 12th September 2018

Iterations helps the process of going through multiple variations of large or small collection groups (Arrays, Lists etc) Today I'll be explaining through 4 iterations with their benefits:
Do ... While Loop
For Loop
For each Loop
While Loop

When do I use Iteration loops in my program?

It's best to use an iteration loop when you need to process variables that contain indexes or a range. Here is example of both:


Iteration 1 - Do While Loop & While Loop

The Do While Loop acts differently to its partner in crime, the While Loop.

The Do While Loop does the execution then the validation as the While Loop does the validation then the execution.

Here is a few code samples:


These two Iterations produces the following outputs:
DoWhileInt Number: 1
DoWhileInt Number: 2
DoWhileInt Number: 3
DoWhileInt Number: 4
DoWhileInt Number: 5
DoWhileInt Number: 6
DoWhileInt Number: 7
DoWhileInt Number: 8
DoWhileInt Number: 9
DoWhileInt Number: 10
WhileInt Number: 1
WhileInt Number: 2
WhileInt Number: 3
WhileInt Number: 4
WhileInt Number: 5
WhileInt Number: 6
WhileInt Number: 7
WhileInt Number: 8
WhileInt Number: 9
WhileInt Number: 10

Here they are as a flow chart:
Do While Loop Flow Chart




While Loop Flow Chart


Iteration 2 - For Loop

The For Loop is a loop which works with a range. You set the starting number, the condition and how much it will increment or decrement. This loop is common when processing types like Arrays or Lists (or any others). 

Program example: Student Names & Scores
We have 2 Array's, Student (which is a string array) and Score (which is a int array), Display their name and their score. (Assuming that both arrays are the same size):


Program example: Scores v2
We have a Multi-dimensional Array which holds 3 tests per student. Add up the 3 scores:

Iteration 3 - For Each Loop

The For Each Loop is an easier way of handling a collection without the need to store the index in memory. This comes handy as it makes your handling of these collections types, Easier to handle.
This example, I'll make a program which greets who ever is added to the List collection, lets start by making a custom class which holds Name, Age, If their over 25.

In our Main() method in our Console (Form1_Loaded if using WinForms/WPF)
Note: You need to include using System.Collections.Generic; at the top since we are using List<T> which isn't part of the System library by default!


This program produces the following output:
Hello, James! You are 200's old. Is over 25? That's True!
Hello, Bob! You are 190's old. Is over 25? That's True!
*cough, cough* Notice the grammar error :D At least this method works, eh?

You can make this method more advanced by adding a constructor which does a conditional to do the isOver25 handling if your up for a challenge, but that isn't the tutorial for today :)

When to use these iterations:

Do While Loop

Use this loop when you want to do an operation first then validate.

While Loop

Use this loop when you want to validate a condition first then perform the operation (Use this method for threading!)

For Loop

Use this loop when you need to access indexes, use these for multi-dimensional arrays or jagged arrays. You can use a For Loop if you want to access a certain column in an array.

Foreach Loop

Use this loop when you don't require the need for working with an index, Don't use this iteration if you require to search for a certain index. Use this loop if you want to look for a certain name, or property in a variable. The Foreach is more robust since you aren't working for an index, so your program won't crash (If you done it right!)

Popular posts from this blog

C# Object Oriented Programming

C# Memory - Tutorial