Moving on to C# 4: Loops

Loops in C#

We're going to look at loops in C# and we'll find that there are some similarities to the loops in Small Basic and some differences.

While there are only a couple of loops in SB, there are four in C#. Let's do the easiest one first - the while loop.

The while loop

You'll remember the while loop example from the SB tutorial. It repeatedly divides a number by two while its value remains greater than one. It's a very simple program and the C# equivalent is shown below:
   
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SmallBasic.Library;

namespace CSSB1
{
    class Program
    {
        static void Main()
        {
            float n = 100;
            while (n > 1)
            {
                TextWindow.WriteLine(n);
                n = n/2;
            }

            TextWindow.Read();
        }
    }
}

 

You're probably getting used to the C# syntax now and might have guessed that this is what the loop would look like. As you can see, the syntax looks very similar to SB and this loop in C# performs the same function as in SB.

The main thing to note is that the code to be repeated is enclosed in braces ({}) just like it was in the if statement.

The for loop

The for loop in C# is quite different than in SB. This form of loop has been inherited from the C language and is found in other languages such as Java, C++ and PHP.

The example in the SB tutorial simply printed out the numbers 1 to 24; the second example printed out every other number from 1 to 24. I'll show you the C# and then explain it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SmallBasic.Library;

namespace CSSB1
{
    class Program
    {
        static void Main()
        {
            for (int i = 1; i <= 24; i++)
            {
                TextWindow.WriteLine(i);
            }

            TextWindow.Read();
        }
    }
}


Now that is a little different to SB, isn't it.

The structure of the loop should be clear: the code to be repeated is enclosed in the braces, as we might have expected, and this is preceded by the the code that controls the loop.

We start with the word for followed by the some more code enclosed in brackets. This code is in three parts separated by semicolons (;). The first part

 int i = 1;

is doing two things; first it is declaring a variable call i that is an integer and then it is giving it the value 1. So this is the equivalent of the "i = 1" part of the SB loop. The second part,

 i <= 24; 

is the condition for ending the loop. It means that while the value of i is less than or equal to 24 we continue the loop (this is the equivalent of the "To 24" part of the SB code). The third part is what we want to do with the variable i . The SB loop assumes that we want to increment the variable by one each time but in C# we have to be explicit. i++ is a shorthand way of writing  i = i + 1 , i.e. add one to i.

So, if we want to increment the loop variable by two, we simply replace

 i++

with

i = i + 2

and this is the equivalent of the second SB tutorial example.

More loops

I mentioned that there are four types of loop in C#. However, I'm going to leave the remaining two for later.

For completeness, I'll tell you that they are the
do... while
loop and the
foreach
loop.



Comments

Popular posts from this blog

Just Enough Python

Learn to code with C

Introduction to Python by Andrew Ng