![]() |
JScript.Net Tutorial #4
Welcome to the fourth JScript.Net 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;
var i : int;
for(i=1; i<5; i++)
{
Console.WriteLine("The value of i is " + i);
}
Note that you cannot include the variable declaration in the for loop itself like you can with C# but has to be declared at the top in the usual way. Now I prefer to list all the variable declarations at the top of the program anyway rather than pepper them throughout the program where it can be difficult to read them so this isn#t too much of a problem for me. 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 JScript.Net 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;
var i : int;
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;
var i : int;
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 and is called a foreach loop. Regrettably, JScript.Net doesn't support it so we have to use an ordinary for loop instead. 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 varaibles. 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:-
for i=1; i<1000; i++
fetch contents of array i
writeline it
loop around again
Much better. In C# we would use a loop that has been designed for arrays called the foreach loop but this doesn't exist in JScript but we can use a for...in loop which does something very similar. In pseudo code (where we write what is happening in english rather than it code as above) it would look like this:-
for j in the array
writeline the contents
continue looping until no more values
Note that the program doesn't need you the programmer to tell it when there are no more array values - it already knows which is very handy. Here's some code:-
import System;
var aryInt = new Array("c","b","a");
var arySorted;
var i : int;
for(i in aryInt)
{
Console.Write(aryInt[i]);
}
Console.WriteLine();
arySorted = aryInt.sort();
for(i in aryInt)
{
Console.Write(aryInt[i]);
}
This is a for loop accessing each element in the array and writing it to the console. There is also the ability to sort the array which is the second block of code and is very similar to that provided by C#.
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...