📄 pasl1000.html
字号:
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta http-equiv="Content-Language" content="en-us">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<link href="../style.css" rel="stylesheet" type="text/css">
<TITLE>Pascal Tutorial - Chapter 1</TITLE>
</HEAD>
<BODY BACKGROUND="../tile01.jpg">
<CENTER><H1>"Hello, World !"<br> A Classic Approach to Begin the Lesson</H1></CENTER>
<P>
<P> <H3>Welcome ! </H3> <P>
Start your Borland Pascal 7.0 first. Feel it. Type some words then delete
it. Once you will get used to it. Let's begin our lesson! Oh, by the way,
you may download the entire lesson, chapter 1 to 14, in <a HREF="../download.html">here</a>, just a mere 83KB (compressed size) complete with source codes and examples.
</P><P>
Pascal is a structured programming language. It means that everything you
program, must be structured and in orderly. No more free gotos and jumps.
It has a format. Yeah... right. You don't have to obey it anyway since you
COULD extend it to complex ones.
</P><P>
A format of very simple Pascal programs :</P>
<HR><pre>uses ....
var
...
begin
.... (Your program is here)
end.</pre><HR>
<P>
The word <TT>end</TT>
here should end with stop ('.'). Every Pascal program blocks
begins with the word <TT>begin</TT>
and ends with the word <TT>end</TT>. Some of the
<TT>end</TT> word ends with semicolon ';' or period '.' or just nothing. That will be described
later (not in this lesson). </P>
<P>
For example :<BR>
Type in these words in your BP, and run it by pressing <TT>Ctrl+F9</TT>
</P><HR><pre>begin
Writeln('Hello, World !');
end.</pre><HR>
<P>
In this case we don't need the word <TT>uses</TT> and <TT>var</TT>. Try to run it. It
should display 'Hello, World !' on screen. But perhaps it's too fast. But,
how can I view it anyway ? Press <TT>Alt+F5</TT>.
</P><P>
What is the word <TT>uses</TT> for ? It's a kind of terms to declare that we use
specific library. Some are standard ones. At first, we use the standard
ones that come with every Pascal compiler. The one we will use most is <TT>crt</TT> unit,
since it contains various commands that is suitable for us for this moment.
</P><P>
What is <TT>Writeln</TT> for ? <TT>Writeln</TT> is a command to write something into the
screen. If you want to write a sentence, just wrap it with quote ('). And
don't forget the pair of brackets and the semicolon. Example :
</P><HR><pre>begin
Writeln('I learn Pascal');
Writeln('Hi, there !');
end.</pre><HR><P>
You will see another messages on screen. View it with <TT>Alt+F5</TT>
after running
it by pressing <TT>Ctrl+F9</TT>.
</P><P>
So, you want to clear the screen when the program starts. You need to add
the word <TT>Clrscr; </TT> just after the word begin. But it needs <TT>uses crt</TT>
since <TT>Clrscr</TT> is one of the commands included in <TT>crt</TT>
library. (Pascal terms for
library is unit) So, the above example would become :
</P><HR><pre>uses crt;
begin
Clrscr;
Writeln('I learn Pascal');
Writeln('Hi, there !');
end.</pre><HR><P>
Try and run it !</P>
<P>
Pascal has a command <TT>Write</TT> instead of <TT>Writeln</TT>.
Why don't you just try
that ? Just replace all the <TT>Writeln</TT> into <TT>Write</TT>
(of the program above).
Run it and see what happens. See the difference ?
</P><P>
It'll output <TT>I learn PascalHi, there !</TT>. Stick !
</P><P>
Sometime, we need to leave a line blank. Suppose we want a blank line between
<TT>I learn Pascal</TT> and <TT>Hi, there !</TT>.
Just insert <TT>Writeln;</TT> between,
and bingo ! Try inserting the <TT>Writeln</TT> in correct order so it yields :
</P><HR><pre>I learn Pascal
Hi, there !</pre><HR><P>
If you do have a good grip about how to write something on screen, please
proceed. Otherwise, you should repeat it once again.
</P>
<P>
Now the input parts.</P>
<P>
When we ask computer to receive input from users, we need something to
store the input before it is being processed. That was variable. We need it
to corporate our program. There are various types of variable and with various
range too. Here are some frequently used variable types :
</P><center><TABLE BORDER=2>
<CAPTION><B>Pascal Variable Name, Range, and Type</B></CAPTION>
<TR><TH>Type name</TH><TH>Range</TH><TH>Type</TH></TR>
<TR><TD>Shortint</TD><TD>-128 to +127</TD><TD>integer</TD></TR>
<TR><TD>Byte</TD><TD>0 to 255</TD><TD>integer</TD></TR>
<TR><TD>Integer</TD><TD>-32768 to +32767</TD><TD>integer</TD></TR>
<TR><TD>Word</TD><TD>0 to 65535</TD><TD>integer</TD></TR>
<TR><TD>Longint</TD><TD>-2146473648 to +2146473647</TD><TD>integer</TD></TR>
<TR><TD>Real</TD><TD>-??????? to +???????</TD><TD>fractional</TD></TR>
<TR><TD>String</TD><TD>up to 255 letters</TD><TD>non-numeric</TD></TR>
<TR><TD>Char</TD><TD>1 letter only</TD><TD>non-numeric</TD></TR></TABLE></center>
<P>There are many more, but that's all we need for now.</P>
<P>
How to declare our variables ? As we seen in our format previously that we
have <TT>var</TT> section there. That's where we suppose to declare our variables.
<BR>Here is some example :</P>
<HR><pre>var
MyAge : Byte;
Comments : String;</pre><HR><P>
To assign variables with values, you need to write this syntax :</P>
<PRE> var_name := value;</PRE><P>
This should be written anywhere within <TT>begin ... end</TT> block, in appropriate
location.</P>
<P>Example :</P><HR><pre>
var
MyAge : Byte;
Comments : String;
begin
MyAge:=19;
Comments:='Hi, there ! I'm learning Pascal';
Writeln('I am ',MyAge,' years old');
Writeln(Comments);
end.
</pre><HR><P>Write it down, run it an view it on screen !</P>
<P>
How to get the user's input ? Yeah, that's easy actually. Similar to <TT>Write</TT>
or <TT>Writeln</TT>, Pascal uses <TT>Read</TT> and
<TT>Readln</TT>. The difference is that in
<TT>Read</TT> or <TT>Readln</TT>, we cannot write anything on screen but to get user's input.
</P><P>Example :</P><HR><pre>
var
MyAge : Byte;
Comments : String;
begin
Write('How old are you ? '); Readln(MyAge);
Write('Give us comments: '); Readln(Comments);
Writeln;
Writeln('I am ',MyAge,' years old');
Writeln(Comments);
end.
</pre><HR><P>Write it down, run it an view it on screen !</P>
<P>
Now, you know how to get user's input. But wait ! If I enter 23.5 in 'How
old are you ?' it spouts error ! Yeah, you right ! The value on that question
is actually stored in <TT>MyAge</TT>. <TT>MyAge</TT> is a <TT>Byte</TT>
variable.
So it only accept integer values between 0 to 255. That's the range of
it ! It cannot go further. If you want computer to accept fractional values
in <TT>MyAge</TT>, try using <TT>Real</TT>
instead of <TT>Byte</TT>.
</P><P>
But why when I change it to <TT>Real</TT>, it outputs erratical numbers ? No, it
actually doesn't throw garbage on the screen. It just exponential numbers.
For ordinary people -- non technical -- it would be more convenient to view
fractional number at 3 or 4 place after the decimal point. Try changing :
</P><pre>
Writeln('I am ',MyAge,' years old');</pre>
<P>to :</P>
<pre>Writeln('I am ',MyAge:2:4,' years old');</pre>
<P>
What does it mean ? Why some <TT>:2:4</TT> attached to <TT>MyAge</TT>
? For real numbers,
it means : Show 2 place before decimal point and 4 place after decimal point
So, when we enter 23.5 for <TT>MyAge</TT>, it will output :
</P><TT>
I am 23.5000 years old</TT>
<P>
Try changing <TT>:2:4</TT> with other values. Be creative !</P>
<P>
Can we apply it to the string ? Nope ! But it only accepts just <TT>:x</TT>.
It
means that 'I only grant this variable x place on the screen to express its
value'. Suppose you change :</P>
<PRE>
Writeln(Comments);
</PRE><P>
to</P>
<PRE>
Writeln(Comments:5);
</PRE><P>
Try writing long comments when the program is running. See what happens. If
you enter <TT>Programming</TT> as the Comment, you will see output :</P>
<TT>
Progr
</TT><P>
Ok ! I'm tired of pressing <TT>Alt+F5</TT> to view this tiresome tutorial result !
No problem. Just add <TT>Readln;</TT> just before <TT>end</TT>.
Try it yourself !
</P><P>
That's all for this time. Refer to <A HREF="pasq1000.html">the quiz</A>
to evaluate your understanding. Any questions ? Write to <a HREF="../faq.html">me</a>.</P>
<hr><P><B>Now, select your way</B><br>
<a HREF="../menu.html">Back to main page</a><BR>
<A HREF="pasles01.html">Back to Pascal Tutorial Lesson 1 contents</A><BR>
<A HREF="pasq1000.html">To the quiz</A><BR>
<A HREF="pasl1001.html">To Chapter 2</A>, about exploring <TT>crt</TT> units<BR>
<a HREF="../mylink.html">My page of programming link</a><BR>
</P><hr><P class="cpy">By : Roby Joehanes, © 1996, 2000</P>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -