📄 pasl1002.html
字号:
<HTML>
<head>
<LINK href="../style.css" rel="stylesheet" type="text/css">
<TITLE>Pascal Tutorial - Chapter 3</Title></head>
<body background="../tile01.jpg">
<H1><center>If I...</center></h1>
<P>
</P>
<P>
Hi ! Nice to meet you again. And now, we're getting closer, right ? We will
begin our lesson with conditional branching.
What is conditional branching anyway ? Well, it's a if not this, do this,
else do that ... ummm similar things like that, you know. We have the conditions,
and if the conditions are satisfied, do some statements, otherwise do something else.
</P><P>
Pascal provides two ways to implement conditional branching. One uses <TT>if</TT>,
the other uses <TT>case ... of</TT>. Well, let's discuss the first part :
<TT>if</TT>
The structure of <TT>if</TT> in Pascal is like this :
</P><PRE>
if condition then
begin
:
:
end;
</PRE><P>
OR, if you add the <TT>else</TT> part :</P><PRE>
if condition then
begin
:
:
end <-- notice that the semicolon disappeared now !
else
begin
:
:
end;
</PRE><P>
Now, you ask : How could Pascal determine the process ?
If the condition(s) beside the word <TT>if</TT> is met, then the block <TT>begin...end</TT>
-- just below the <TT>if</TT> -- will be executed, otherwise, then the block <TT>begin..end</TT>
after the <TT>else</TT> will be executed. If we have the <TT>if</TT>
structure as the
first one -- that is, no <TT>else</TT> block -- the program execution will continue
after the word <TT>end</TT>. Let us examine the excerpt of the code below:
</P><HR><PRE>
if counter<10 then
begin
counter:=counter+1;
writeln(counter);
end;
writeln('Done');
</PRE><HR><P>
If the variable <TT>counter</TT> has the value less than 10, the commands between
<TT>begin</TT> and <TT>end</TT> will be executed. If it has the value 10 or more, it skips
the process just after the <TT>end</TT>. It means that it does the
<TT>writeln('Done');</TT>.
But, don't be mistaken. Even the <TT>counter</TT> has the value less than 10, after
it has done the commands between <TT>begin</TT> and <TT>end</TT>, it continues the process to
the next statement -- that is <TT>writeln('Done');</TT>.
How about adding else block ? It just as the same as above, but of course
with some differences. See the examples, and run them ! Input some numbers
and see how it works.
To clarify how the if works, refer to these programs</P>
<HR>
<pre>program if1;
var
i : byte;
begin
writeln('Enter a number = '); readln(i);
if i <= 10 then
begin
writeln('It is done if i <= 10 only');
writeln('Or you just can omit the begin..end block');
writeln('if there is only ONE command inside (See IF2.PAS)');
end;
writeln('We always do this');
end.
</pre>
<HR>
<pre>program if2;
var
i : byte;
begin
writeln('Enter a number = '); readln(i);
if i <= 10 then writeln('It is done if i <= 10 only');
writeln('We always do this');
end.
</pre>
<HR>
<pre>program if3;
var
i : byte;
begin
writeln('Enter a number = '); readln(i);
if i <= 10 then
writeln('It is done if i <= 10 only') { Omit the semicolon ; }
else
writeln('It is done if i > 10 only'); { There is a semicolon }
writeln('We always do this');
end.
</pre>
<HR>
<pre>program if4;
var
i : byte;
begin
writeln('Enter a number = '); readln(i);
if i <= 10 then
begin
writeln('It is done if i <= 10 only');
writeln('Yeah, you can add begin..end block to else part too !');
end { Omit the semicolon BEFORE the end keyword }
else writeln('It is done if i > 10 only.');
writeln('We always do this');
end.
</pre>
<HR>
<pre>program if5;
var
i : byte;
begin
writeln('Enter a number = '); readln(i);
if i <= 10 then
begin
writeln('It is done if i <= 10 only');
writeln('Yeah, you can add begin..end block to else part too !');
end { Omit the semicolon BEFORE the end keyword }
else
begin
writeln('It is done if i > 10 only.');
writeln('Look !');
end;
writeln('We always do this');
end.
</pre>
<HR>
<H3>Nested if</H3>
<P>
Now how to have some <TT>if</TT>s inside an <TT>if</TT> ? Yeah, right ! That's perfectly
legal provided if you don't forget to wrap the <TT>begin..end</TT> (if there are two
or more statements inside). Here is one example:
</P><HR><PRE>
if i<10 then
begin
writeln('i is more than 10.');
if j>3 then writeln('j is more than 3.');
end;
</PRE><HR><P>
Yes, yes ! You may do <TT>if</TT> inside the <TT>begin..end</TT> block after the
<TT>else</TT> too.
Why don't you do some experiments ?</P>
<H3>Combining the conditions</H3>
<P>
Pascal lets us combine the conditions. Say that if a salesman is qualified
to get the project if he has sold 7000 pieces of products and he has at
least 3 years of experience. This is the example :</P><HR><PRE>if (productsold>=7000) and (experienceyear>=3) then qualified;</PRE><HR><P>
Yes, this cannot be run of course. In this example I would like to introduce the
<TT>and</TT>, <TT>or</TT>, and <TT>xor</TT> keywords.
The keyword <TT>and</TT> means that it will return <TT>true</TT> if both conditions are
met. It means that the first <TT>begin..end</TT> block will be executed only if both
conditions are true.</P>
<P>
The keyword <TT>or</TT> means that if there is at least one of the conditions is
met (or both), then the first <TT>begin..end</TT> block will be executed.
The keyword <TT>xor</TT> means that if there is ONLY one condition is met (not
both), the first <TT>begin..end</TT> block will be executed.
The keyword <TT>not</TT> means negating all conditions. Then if the condition is
<TT>true</TT>, it will be considered <TT>false</TT> and vice versa.
Now, take a look at these codes :</P><HR><PRE>program if6;
var
i, j : byte;
begin
write('Enter a value for i = '); readln(i);
write('Enter a value for j = '); readln(j);
if (i>3) and (j>4) then
begin
writeln('This will be done if i>3 and j>4');
writeln('Now change the "and" with "or" and "xor"');
end;
end.</pre>
<HR>
<pre>program if7;
var
i : byte;
begin
write('Enter a value for i = '); readln(i);
if not (i>3) then
begin
writeln('This will be done if i is NOT more than 3');
writeln('So, do you understand ?');
end;
end.</pre>
<HR>
<P>
How will it be if the conditions are more than 2 ? Simply wraps them with
brackets, two by two. Note : The conditions inside the brackets will be
done first. Example, look at this :</P><HR><PRE>
program if8;
var
i, j, k : byte;
begin
write('Enter a value for i = '); readln(i);
write('Enter a value for j = '); readln(j);
write('Enter a value for k = '); readln(k);
if ((i>3) and (j>4)) or (k>5) then
begin
writeln('Yeah !!');
writeln('Now change the bracket orders and run it again !');
end;
end.
</PRE><HR>
<H3>Case..of</H3>
<P>
Sometimes, you really need to select conditions according some criteria.
You're clever ! Use some <TT>if</TT>s and let it be done ! But how if the criterium
was quite long and complex ? I'm sure that it would be a daunting task to
have an <TT>if</TT> for each. How could we categorize the criteria with some similarities
and simplify the problem.</P>
<P>
Suppose we have to do the grading system for our campus. Say, if the student
has 80 or more deserves an A. 70 to 79 is for B, 60 to 69 is C, 50 to
59 is D, and 49 or below is E. The if example would be like this :
(Note that <TT>mark</TT>
is a <TT>byte</TT>)</P><HR><PRE>if mark>=80 then
grade:='A'
else { 79 or below goes here }
if mark>=70 then
grade:='B'
else { 69 or below goes here }
if mark>=60 then
grade:='C'
else { 59 or below goes here }
if mark>=50 then
grade:='D'
else { 49 or below goes here }
grade:='E';
</PRE><HR><P>
Wow, that's pretty long. Now see this :</P><HR><PRE>
case mark of
80..100: grade:='A';
70..79: grade:='B';
60..69: grade:='C';
50..59: grade:='D';
else grade:='E';
end;
</PRE><HR><P>
Simple and elegant ! Now, let's learn about <TT>readkey</TT> statement. It is one of
the commands included in <TT>crt</TT> unit, like <TT>clrscr</TT>. What it does ? It receives
the ASCII code of pressed keyboard button. Well, what is ASCII code anyway?
It is some sort of codes that is defined for computers. If we pressed the
keyboard, it produces some sort of code, and then the computer translates
it into the ASCII code, the code we commonly (programmers) know.</P>
<BLOCKQUOTE>
Note for advanced programmers :<BR>
Don't laugh at this, I have to explain what ASCII code exactly to the non-programmer
folks in a practical and easy way without going into the complicated talks !
Yes, we know the "origin" keyboard codes. I do too.)
</BLOCKQUOTE><P><TT>
Readkey</TT> is a function, returning a <TT>char</TT> value. OK, let's see how it works !
</P><HR><PRE>
program test_readkey;
uses crt;
var
c : char;
begin
c:=readkey;
case c of
#8 : writeln('You presses backspace');
#9 : writeln('You presses tab');
#13: writeln('You presses enter');
#27: writeln('You presses escape');
#32: writeln('You presses space');
else writeln('You presses other key');
end;
end.
</PRE><HR><P>
The program simply waits for a keypress then detects it and quits.
Now you asked : Why do we use the # sign ? Because it denotes a character
with a code. Suppose #8 means a character with a code of 8. Why don't we
use the .. like the previous example ? Because in this case we don't specify
ranges of values while the first example did. Like 80..100 means from 80
to 100.</P>
<P>
Let us detect the arrow keys. Arrow keys, just like function keys, are extended.
It means it generates a special codes. Now, here is the code to
trap them :</P><HR><PRE>program trap_arrow;
uses crt;
var
c : char;
begin
c:=readkey;
if c=#0 then { If extended codes, }
begin
c:=readkey; { read the code once more }
case c of
#72: writeln('Up arrow');
#75: writeln('Left arrow');
#77: writeln('Right arrow');
#80: writeln('Down arrow');
end;
end;
end.</PRE><HR><P>
How to detect the ASCII number ? How do you know that ? Easy, with <TT>ord</TT>. Suppose
you want to know the ASCII codes for each keyboard keys. Do this :
</P><HR><PRE>program trap_key;
uses crt;
var
c : char;
begin
c:=#0;
while c<>#27 do
begin
c:=readkey;
if c=#0 then { If extended codes, }
begin
c:=readkey; { read the code once more }
writeln('Extended : ',ord(c));
end
else writeln(ord(c));
end;
end.</PRE><HR><P>
Now, press keyboard keys, and see the codes. Combine it with <TT>Ctrl</TT>,
<TT>Alt</TT>, and
<TT>Shift</TT> and see what happens. To understand the program above, you need to
know what while means. The statements inside the while will be repeated on
and on as long as the condition (<TT>c<>#27</TT>) is met. It means the
<TT>c:= readkey;</TT>
if <TT>c=#0</TT> ... will be repeated as long as c is not equal to character 27.
Since character 27 is escape, then the program simply runs until user presses
<TT>Esc</TT>. More about while and other repetitional commands will be explained
thoroughly in <A HREF="pasl1004.html">chapter 5</A>.</P>
<P>
Well, that's all for now. Practice hard to get to the top !
If there are some questions, direct it to <a HREF="../faq.html">me</a></P>
<hr><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="pasq1002.html">To the quiz</A><BR>
<A HREF="pasl1001.html">Back to Chapter 2</A> about extending first program<BR>
<A HREF="pasl1003.html">To Chapter 4</A> about constants in Pascal<BR>
<a HREF="../mylink.html">My page of programming link</a><BR>
<a HREF="../faq.html">Contact me</a></p>
<hr><P class="cpy">By : Roby Joehanes, © 1996, 2000</P>
</BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -