J#.Net Tutorial

J#.Net Tutorial #4

Welcome to the fourth J# tutorial in which we'll be going loopy. Loops are quite an important construct (you won't get very far in programming without them) but they can be a little difficult to get your head around sometimes. So on to our first loop which is known as the for loop:-

import System.*;

class hello
{
	public static void main()
	{
		// int i;

		for(int i=1; i<5; i++)
		{
			Console.WriteLine("The value of i is " + i);
		}
	}
}

Note that I have commented out (denoted by the \\ backslashes) the variable declaration because it is actually being declared in the for loop itself. Now this is down to persoanal taste but I prefer to list all the variable declarations at the top of the program rather than pepper them throughout the program where it can be difficult to read them but it is up to you how you do it. So what is this doing? The for statements breaks down into something like this:-

start loop
set variable to 1
if less than 5
add 1 to variable
go back to if statement above

otherwise
stop looping

which in J# terms is:-

for(variable is 1; if variable < 5; loop round)
{
write value to see whats happening
}

It can be difficult sometimes to really understand what is happening here so take some time to think about it. You'll also need to compile and watch the program running to gain further insight. You can also play around with the start value of i (start it at zero for example) and use i<=5 and see what effect that has. Once you think you've got your head around it then move on to the next::-

import System.*;

class hello
{
	public static void main()
	{
		int i;
		i=1;

		do
		{
			// i=i+1;
			Console.WriteLine("The value of i is " + i);
			i=i+1;

		} while(i<5);
	}
}

This is the do loop (sometimes also known as a do..while) which gives the programmer a little more control over how and when the loop variable is updated. Note that the index i variable is set before the loop starts. We also have a choice over which side of the writeline statement we add 1 to the loop variable. The while does the test for deciding when to stop the loop. Sometimes it is possible to get into a situation when the loop never stops and the it becomes difficult to shut it down so some care has to be exercised. This essentially works the same as the for loop did above just in a slightly different way. Only experience will help you to decide which loop to use in a real situation. Now something very similar again:-

import System.*;

class hello
{
	public static void main()
	{
		int i;
		i=1;

		while(i<5)
		{
			// i=i+1;
			Console.WriteLine("The value of i is " + i);
			i=i+1;
		}
	}
}

This is a while loop and does away with the do. Again, you have to be careful about the start value and when it is updated inside the loop. Again, this does much the same as both previous loops but again in a slightly different way. You decide which to use. It is my aim to introduce only what you need to know in order to progress towards our ultimate aim of databases and GUI programs so you will meet these again and hopefully a few more examples will help you get to grips with it. I know that loops was one of the most difficult concepts I had to face when beginning programming but it does make sense in the end.

There is one other loop but it is involved with something called an array. An array can be though of as a series of boxes holding values. So we could for example have 3 boxes each containing an integer value as follows:-

box 1 contains the number 1
box 2 contains the number 3
box 3 contains the number 2

These are of course memory locations but don't worry so much about that as the concept. So we need to get at each of these boxes either to fill with data or update it or retrieve it. Why use arrays? Because it is more convenient to do it in this way than have a whole list of variables. For example, if we asigned our values using standard variables then we would need to write code like this:-

int1 = 1
int2 = 2
int3 = 3
writeline int1
writeline int2
writeline int3

Now this may not seem too bad but what if you had 1000 items. If we could use a loop then all we would have to adjust is the number of boxes - the code would stay the same. So instead we would write (in pseudo code) :-

for i=1; i<1000; i++
fetch contents of array i
writeline it
loop around again

Much better. In J# we use an ordinary loop (other languages such as C# has a special for...each loop but J# doesn't have it). The for...each loop is very useful because it stops when the end of the array is reached - J# programmers have no such luxury so you must specify how big the array is in the loop. So take a look at the next program:-

import System.*;

public class ArrayExample
{
    public static void main()
    {
		int j,k;
		int[] aryInt={1,3,2};
		for(j=0; j<3; j++)
		{
			Console.Write(aryInt[j]);
		}

		Array.Sort(aryInt);
		Console.WriteLine();
		for(k=0; k<3; k++)
		{
			Console.Write(aryInt[k]);
		}
    }
}

The first block of code is doing what the pseudo code above is doing. The second block will sort the array - you don't need to write all the code to do a sort - it is available for you to use with the command Array.sort with the name of your array in the brackets. Now you can see perhaps why I used the array values of 1,3,2 instead of 1,2,3. The array is declared as int[] with the square brackets denoting that we wish to use an array of integers. This array is then assigned values. We run around a foreach loop pulling out the values and writing them to the console. Then we sort the array and print it all out again in the new order. Very nice elegant code.

So that just about wraps it up for loops. Now it is possible to nest loops within loops but I won't burden you with that unless it becomes absolutely necessary. As always, if you have any problems or queries then please email me at the address below. On to some slightly more exotic stuff next time...

Back to J# Tutor

Written by Nick Cheesman. Last updated: 01/06/2007
Please eMail me at:
nickjc@nickjc.co.uk