📄 pasl1004.html
字号:
<HTML><HEAD>
<LINK href="../style.css" rel="stylesheet" type="text/css">
<TITLE>Pascal Tutorial - Chapter 5</Title></head>
<body background="../tile01.jpg">
<H1><center>Loop A Loop</center></h1><p> <br><br>
Hi ! Nice to meet you ! We meet again in Loop a loop. This course suppose
to be long and you feel quite bored, don't you ? Well, let's make it quite
straight forward.</P><p>
In many cases in programming, we need to repeat processes. In that case, we
need some loops. Pascal understands it, therefore it provides three types
of loops :</p><p><ol>
<li><tt>For ... to ... do</tt></li>
<li><tt>While ... do</tt></li>
<li><tt>Repeat ... until</tt></li>
</ol><p>Well, let's look inside them :</p>
<h3>1. For ... to ... do</h3>
<p>Syntax :
<pre>
For variable := fromwhat to what do
begin
:
:
statements / commands
:
:
end;
</pre><p>Example : Suppose i is an integer</p>
<pre>
For i:=1 to 5 do
begin
writeln(i);
if i=5 then writeln('Finished !');
end;
</pre><p>
It yields 1, 2, 3, 4, 5, then the message 'Finished !'. You see that inside
the begin-end block, you could add any commands you want. If there is only
one command inside it, you may omit the <tt>begin..end</tt>, just like <tt>if</tt> we have
studied before. Suppose you omit the <tt>if i=5 ...</tt>, so inside the <tt>begin..end</tt>
we have only <tt>writeln(i);</tt>. We can omit the <tt>begin..end</tt>, then it becomes :</P>
<pre>
For i:=1 to 5 do
writeln(i);
</pre><p>
If we want to count backwards, i.e. from 5 to 1, we just change <tt>to</tt> into
<tt>downto</tt>, so check this out :</p>
<pre>
For i:=5 downto 1 do
writeln(i);
</pre><p>
How about if we want to step more than one, i.e. 1, 3, 5, 7, 9, ...? BASIC
provides the <tt>STEP</tt> how about Pascal ? Well, in this case, Pascal <b>DOES NOT</b>
provides such, but if you are creative, you are able to do it. This applies
to fractional step as well. <b>BUT</b>, Pascal does include special feature. Check
this :</P>
<pre>
{ Suppose c is a char variable }
For c:='A' to 'Z' do write(c,' ');
</pre><p>
You may also write <tt>For c:='Z' downto 'A' do write(c,' ');</tt> as well. You may even
write this : (Suppose b is a boolean)</p>
<pre>
For b:=false to true do writeln(b);
</pre><p>
And many even <i>stranger cases</i> than this, if you have studied <tt>type</tt>
statement.</p>
<h3>2. While...do</h3><p>
Well, if you have learned BASIC, you will find that <tt>While...do</tt> is quite the
same as <tt>WHILE ... WEND</tt>. The syntax is :</p>
<pre>
While conditions do
begin
:
(statements / commands)
:
end;
</pre><p>
Before we enter the <tt>begin..end</tt> block, FIRSTLY it checks whether the condition(s)
are satisfied. If not, it just skips it out, or else it does any
commands inside <tt>begin..end</tt>. When it reaches the end, it checks the condition
again. If it is satisfied, then loop again. If not, go out. It goes on and
on. The logic is like this :</p>
<center><img src="whiledo.png" border=0 alt="While..do logic" width="134" height="183"></center>
<p>For example :</p>
<pre>
i:=1;
While i<6 do
begin
writeln(i);
i:=i+1;
end;
</pre><p>
You may also combine conditions with <tt>AND</tt>, <tt>OR</tt>, or <tt>XOR</tt>.
If Pascal does not provide <tt>STEP</tt> like BASIC, you can change the
<tt>for</tt> into <tt>while</tt> that has a lot more flexibilities.</p>
<h3>3. Repeat ... Until.</h3>
<p>The syntax is :</P>
<pre>
Repeat
:
statements / commands
:
Until condition;
</pre><p>
Unlike <tt>While...do</tt>, <tt>Repeat...Until</tt> does not check for conditions at first.
It just enters the block and do anything inside. Beware that <tt>Repeat...Until</tt>
does not require begin...end. AT THE END, it then checks whether the conditions
are satisfied or not. If SO, it QUITs. That is the third difference
from the <tt>While...do</tt>. If it does not, it simply loops back. So, the logic is
like this :</p>
<center><img src="repeat.png" border=0 alt="While..do logic" width="115" height="172"></center>
<p>For example :</p>
<pre>
(uses crt first)
Repeat Until Keypressed;
</pre><p>OR :</p><pre>
i:=1;
Repeat
writeln(i);
i:=i+1;
Until i>5;
</pre><p>
In <tt>While...do</tt> and <tt>Repeat...until</tt> there is a chance to get an infinite
loop. Simply give an impossible conditions. But remember, that this also a
drawback if we don't use it carefully. Example :
</p><pre>
While 2<4 do OR Repeat
begin (bla bla bla ...)
(bla bla bla ...) Until 3>5;
end;
(2 is always less than 4, so (3 is never be able to exceed 5, so
While...do get an infinite loop) Repeat...until goes forever).
</pre><p>Or you might just:</p>
<pre>
While true do OR Repeat
begin (bla bla bla ...)
(bla bla bla ...) Until false;
end;
</pre><p>(They yield the same effect as previous examples : infinite loops).</p>
<p>You may also nest the loop, i.e. a <tt>for</tt> inside another <tt>for</tt>, a <tt>While</tt> in
another <tt>While</tt>, etc. And even, you may nest one form of loop inside
another form, like a <tt>for</tt> inside <tt>Repeat...Until</tt>. Example :</p>
<pre>
Repeat
:
For ... do
begin
:
end;
:
Until ...;
</pre><p>It is perfectly legal.</p><p>
How about if I want to bail out in a middle of loops ? Easy, use <tt>Break</tt>.
Example :</p><pre>
begin
for i:=1 to 10 do
begin
writeln(i);
if i=5 then break;
end;
writeln('Finished !');
end.
</pre><p>It yields :</p><pre>
1
2
3
4
5
Finished
</pre><p>
Got it ? <tt>Break</tt> means go out from current loop. It can also be applied on
<tt>while...do</tt> and <tt>repeat...until</tt>. How about nested loop ? <tt>Break</tt> only applies
on its <tt>begin...end</tt> block. Check out the program below :</p><hr><pre>
begin
for i:=1 to 4 do
begin
for j:=1 to 5 do
begin
writeln(i,' ',j);
if j=3 then break;
end;
writeln('Next i loop');
end;
writeln('Finished !');
end.
</pre><hr><p>It yields :</p><pre>
1 1
1 2
1 3
Next i loop
2 1
2 2
2 3
Next i loop
3 1
3 2
3 3
Next i loop
4 1
4 2
4 3
Next i loop
Finished !
</pre><p>Another example :</p><hr><pre>
begin
for i:=1 to 5 do
begin
for j:=1 to 3 do
begin
writeln(i,' ',j);
end;
if i=2 then break;
end;
writeln('Finished !');
end.
</pre><hr><p>It yields :</p><pre>
1 1
1 2
1 3
2 1
2 2
2 3
Finished !
</pre><p>
<tt>Break</tt> only does its function in its current scope (<tt>begin...end</tt> block)
How about if I want to skip instead of quitting current loop ? Well, use
<tt>Continue</tt> instead of <tt>Break</tt>. Replace all <tt>Break</tt> in previous examples with
<tt>Continue</tt> and see what happens.</p>
<H3>Extra !!!</h3>
<p>Here we are, have additional lessons :</p><ol>
<li><tt>Random</tt> and <tt>randomize</tt></li>
<li><tt>Halt</tt></li>
<li>ASCII characters</li></ol>
<h4>Random and randomize</h4><p>
All of them are used to generate random numbers. Random numbers are always
used to simulate the reality, where certainty is hardly guaranteed. We are
here not to discuss how to make it, but how to use these two commands.</p>
<p><tt>
Randomize</tt> is used to start the random generator, so that it is only used at
the first initialization, placed after the <tt>begin</tt> in the main block. It is
enough to call it once, eventhough it may be called several times.<tt>
Random</tt> is used to get the random number. Its parameter is the highest
possible number that can be generated. Suppose : <tt>num:=random(50);</tt> It means
that <tt>num</tt> will contain any integer number between 0 to 49.
Look at next example :</p>
<hr><pre>
var
i : byte;
begin
randomize;
for i:=1 to 100 do
write(random(30),' ');
end.
</pre><hr><p>
Let's remove the <tt>randomize</tt>. Run it for several times and note that when
it runs, if you don't use <tt>randomize</tt>, the computer will result the same
number. So, always use the <tt>randomize</tt> so that the computer generate quite
a good random numbers.</p>
<h4>Halt</h4><p>
Just stop and return to system. Usage : <tt>Halt;</tt> You may place it anywhere you
want. If computer encounters the <tt>halt</tt>, it will stop the program execution
and back to the operating system.</p>
<h4>ASCII characters</h4><p>
There are 128 basic ASCII characters and 128 extended ASCII characters.
Writing ASCII characters is as simple as using <tt>writeln;</tt> Example :</p>
<pre>
writeln(#30); { Writing ASCII code 30 }
</pre>
<p>Try changing 30 with other numbers between 0 to 255.</p>
<p>That's all. We'll have a <a href="pasq1004.html">quiz</a>.
Well, it's not hard, actually, but it is harder than before. Bye for now !</p>
<hr><p><B><H3>Where to go ?</H3></B><p>
<A HREF="../news.html">Back to main page</A><BR>
<A HREF="pasles01.html">Back to Pascal Tutorial Lesson 1 contents</A><BR>
<A HREF="pasq1004.html">To the quiz</A><BR>
<A HREF="pasl1003.html">Back to Chapter 4</A> about constants<BR>
<A HREF="pasl1005.html">To Chapter 6</A> about procedures and functions<BR>
<A HREF="../mylink.html">My page of programming link</A><BR>
<A HREF="../faq.html">Contact me here</A>
<hr><P class="cpy">By : Roby Joehanes, © 1996, 2000</P>
</BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -