Pancakes
2007-07-03 04:53:37
How To Design And Use Loops - By Pancakes
Hello all, and welcome to my first tutorial for SCAR :) Be forewarned, I'm not that good at explaining things, but I'll do my best here.
Ah loops. They're the magic that make scripts work. Without them, most scripts wouldn't be here today. They're a very important part of coding in all programming languages. First off lets take a look at a sample piece of code:
Now what does that do? All it does, is send a message to the debug window, saying 'Hello everybody!' (without the quotation marks). Not very useful at all.
But what if you wanted to make it say 'Hello everybody!' more than once?
Now that looks a little bit… pointless, doesn't it? All you are saying is say 'Hello everybody!' over and over again. Of course, you may actually want to do this. I'll introduce you the the first loop I'm going to teach you…
The 'For' Loop!
This is how it is coded in Scar.
Lets take this one line at a time.
Now if you run this script, you should see 'Hello everybody!' written 5 times in the debug window. Thats great!
There is one more feature of the 'for' loop. Remember we declared that variable 'i' as an Integer? Well, that is our loop counter. You maybe be saying 'WTF is a loop counter????', I'm about to explain it to you. A loop counter is pretty much the amount of times the loop has executed. So if it is up to the 3rd time the loop has run, and you made the for loop as stated up, 'i' would be equal to 3. As a rule of thumb, always make 'for' loops start at one, it's much easier to keep track of what the loop counter is actually doing :)
Now why would we want to see how many times our loop has run?
Well, take a gander at this piece of code:
How cool is that? It tells us what line number it is!
Well that concludes this section of the tutorial… Hopefully I'll get around to writing the other loops up :)
-Pancakes.
Hello all, and welcome to my first tutorial for SCAR :) Be forewarned, I'm not that good at explaining things, but I'll do my best here.
Ah loops. They're the magic that make scripts work. Without them, most scripts wouldn't be here today. They're a very important part of coding in all programming languages. First off lets take a look at a sample piece of code:
program AutoTalker;
begin
Writeln('Hello everybody!');
end.Now what does that do? All it does, is send a message to the debug window, saying 'Hello everybody!' (without the quotation marks). Not very useful at all.
But what if you wanted to make it say 'Hello everybody!' more than once?
program AutoTalker;
begin
Writeln('Hello everybody!');
Writeln('Hello everybody!');
Writeln('Hello everybody!');
Writeln('Hello everybody!');
Writeln('Hello everybody!');
end.Now that looks a little bit… pointless, doesn't it? All you are saying is say 'Hello everybody!' over and over again. Of course, you may actually want to do this. I'll introduce you the the first loop I'm going to teach you…
The 'For' Loop!
This is how it is coded in Scar.
var i : Integer;
begin
for i := 1 to 5 do
begin
Writeln('Hello everybody!');
end;
end;
Lets take this one line at a time.
var i : Integer;All we are doing here is declaring 'i' as a variable. 'i' is the most commonly used variable in coding. You can use anything in the place of 'i', but you have to remember to declare it before you start the loop.for i := 1 to 5 doThis is going to be a learning curve for you. First of all, we tell SCAR that we are using a loop, the for loop. Next off, you tell SCAR which variable you will be using for the for loop, in our case it is 'i'. Then, you make 'i' equal 1 to 5, which basically means, it will perform our 'loop' for every number from 1 to 5. Please note that it includes the first and last numbers to, not just the numbers in between. So 1 to 5 will do our loop 5 times, whereas, say, 0 to 5, will do our loop 6 times, because it counts 0 also. Next we put a 'do' after we have told SCAR how many times we want our loop to run. We are basically saying, "For everytime this loop is run, do…".beginThis is where our loop begins. Everything you put in between this and the 'end;' will run however many times we set our loop to run.Writeln('Hello everybody!');Here, we are telling SCAR to write 'Hello everybody!' to the debug window.end;This is the end of our loop, so anything we put AFTER this will not be executed 5 times.Now if you run this script, you should see 'Hello everybody!' written 5 times in the debug window. Thats great!
There is one more feature of the 'for' loop. Remember we declared that variable 'i' as an Integer? Well, that is our loop counter. You maybe be saying 'WTF is a loop counter????', I'm about to explain it to you. A loop counter is pretty much the amount of times the loop has executed. So if it is up to the 3rd time the loop has run, and you made the for loop as stated up, 'i' would be equal to 3. As a rule of thumb, always make 'for' loops start at one, it's much easier to keep track of what the loop counter is actually doing :)
Now why would we want to see how many times our loop has run?
Well, take a gander at this piece of code:
program ForLoops;
var i : Integer;
begin
for i := 1 to 5 do
begin
Writeln('This is line number: ' + IntToStr(i));
end;
end.
Now don't go and get scared off, it's actually quite simple. All we are doing, is Writing what line number it is, each time. How does it change though? Well, remember before I was talking about loop counters, and how every time the loop executes it updates the loop counter? Well we can use them in our script! So when you run this code, this is what you will see in your debug window:
This is line number: 1
This is line number: 2
This is line number: 3
This is line number: 4
This is line number: 5
How cool is that? It tells us what line number it is!
Well that concludes this section of the tutorial… Hopefully I'll get around to writing the other loops up :)
-Pancakes.