This time we'll cover the FOR loop in Pascal. As the name says, a loop means repetition, and its job is to run a statement over and over according to the condition at hand.
Pascal actually has several ways to build a loop: the FOR loop, the WHILE DO loop, and the REPEAT UNTIL loop. Here we'll only look at the FOR loop.
Where Loops Get Used
It's called a loop, so of course it gets used to do work over and over. For now, type in the code below first.
program perulanganFor;
uses crt;
begin
clrscr;
writeln('saya berjanji tidak akan terlambat lagi');
readln;
end.
No need to run it, the output is already obvious.
Familiar? That's the sentence kids usually have to write when they show up late to class, hehe...
Usually the sentence gets written out many times, as a punishment so they don't do it again. Now we're going to do the same thing. Just 5 times. The sentence changes to "saya akan belajar pascal" so it doesn't feel like a punishment :D
program perulanganFor;
uses crt;
begin
clrscr;
writeln('saya akan belajar pascal');
writeln('saya akan belajar pascal');
writeln('saya akan belajar pascal');
writeln('saya akan belajar pascal');
writeln('saya akan belajar pascal');
readln;
end.
Yayy, problem solved, right?
If you run it, that sentence comes out 5 times. Okay, next I dare you to produce 10 lines of output. Ah, easy, just type a bit more, right...
What if you want 20 lines? 40? Or even 1000? Sure, but your fingers will cramp if you type that by hand. That is what a loop is for: doing this effectively and efficiently.
How to Write a FOR Loop in Pascal
In a FOR loop we tell the computer to run a statement a specific number of times, explicitly (the condition is not a boolean expression).
The format for writing it is as follows.
for {a} to {n} do
begin
{command to be executed}
end;
or
for {a} downto {n} do
begin
{command to be executed}
end;
where:
- a : the starting bound of the loop, an integer variable or a variable assignment
- n : the ending bound of the loop, an integer (whole number)
The first form counts upward, while the second form counts downward.
A FOR Loop Example in Pascal
To build a loop with for, you need a variable and the loop bounds. The variables people usually reach for are i, j, and k, but you are free to decide that yourself.
Okay, now try changing the Pascal code we wrote earlier.
program perulanganFor;
uses crt;
var
i: integer;
begin
clrscr;
for i:=1 to 5 do
writeln('saya akan belajar pascal');
readln;
end.
Now run it, and the output will look roughly like this.
saya akan belajar pascal
saya akan belajar pascal
saya akan belajar pascal
saya akan belajar pascal
saya akan belajar pascal
The output comes out the same as when you typed it by hand. That is what a FOR loop does for you: it is very efficient and saves time. The statement after the "do" keyword runs with a starting bound of variable i, which holds 1, and an ending bound of the number 5.
On every pass, the value of variable i goes up by 1. To check that, we can add variable i to the output.
....
for i := 1 to 3 do
writeln(i, '. saya akan belajar pascal');
readln;
....
If you run it, the output will be as follows.
1. saya akan belajar pascal
2. saya akan belajar pascal
3. saya akan belajar pascal
As an experiment, try changing the starting and ending bounds. You can try any number you like, say 1000 or even 1000000. The more times the loop runs, the longer it takes.
....
for i := 1 to 1000 do
writeln(i, '. saya akan belajar pascal');
readln;
....
You can also try it with downto, keeping in mind that the starting bound has to be greater than the ending bound.
Wrapping Up
That's enough for this tutorial, hope it helps :D Next time we'll stay on loops but use a different method, namely loops with the while do loop.