1. Trang chủ
  2. » Công Nghệ Thông Tin

The pascal programming language

18 236 1

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 18
Dung lượng 880,11 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

The output screen: this is where you will see the results when you compile run your program – in other words, where a program works.. Writing a simple program – the use of write and writ

Trang 1

The Pascal programming language was developed, around the 1970s, by Nicholas Wirth It was developed as a first language for programming students and serves the purpose well Pascal is one of the easiest languages to learn, and it encourages beginners to develop good programming skills

Easy to learn

It is easy to learn because the language has a relatively small set of words, and these are close enough to ordinary English to be easy to understand and remember Even before you start to learn the language, you should be able to read a Pascal program’s code and be able to make some sense of it

Example

program first;

begin

write(‘Hello World’);

end

The basic structure

At the simplest, a program takes this shape:

program title;

begin

statement;

statement;

statement;

end

The important things to notice here are:

Programs always start with the word program followed by the name The name must be

a single word – no spaces or punctuation – but can normally be of any length

The start of the active part of the program is marked by begin

The end of the active part is marked by end

program, begin and end are all reserved words, ones with a special meaning They can

be written in lower case or capitals

The full stop at the end is essential

A program can contain any number of statements

It starts here…

This is the code of a Pascal

program, and it is called first

write must put the text on the

screen

… and it ends here

Trang 2

A statement will usually be on a line by itself, but can be spread over several

Each statement is separated from the next by a semi-colon This is normally written at the end of the line

To make th eprogram easier to read, lines may be indented This is more useful in more complex programs, where different levels of indents can help to bring out the structure Whether and how far you indent is entirely up to you

Let’s get it started…

When using Turbo Pascal, you will working with two different types of screens,

The text-editor: this is a blue screen

where you will be typing your

programs

The output screen: this is where you

will see the results when you compile

(run) your program – in other words, where a program works

Writing a simple program – the use of write and writeln statements

Try this program to see how text, integers and decimal values are displayed on the screen

program writing;

uses crt;

begin

clrscr;

writeln(‘This is a text item.’,‘Here is another’);

writeln(‘One more ’,‘and the last’);

writeln(12345.678, 0.123); {number with decimal fraction}

readln;

end

To see the program work, click on RUN in the menu bar and click on

RUN in the menu

Write down the results you see on the screen in the box below

Instead of writeln, type the command write What happens to the output? Write down the results you see on the screen in the box below

Comments written inside {curly brackets}

are ignored by the compiler – i.e when the program is run

Trang 3

So what’s the difference between write and writeln?

write _ writeln _

The clrscr command

This command is used to clear the output screen from any characters and put the cursor in the top-left corner of the screen

Why not add a few colours? – the use of textcolor and textbackground

The output screen can be very dull It usually displays grey text and a black

background Type the following program and see and then write down what you

think textcolor and textbackground are used for

program colourful;

uses crt;

begin

clrscr;

textbackground(14);

textcolor(13);

writeln(‘Testing colours’);

readln;

textcolor(12);

writeln(‘Still testing colours’);

readln;

end

So what do the command textcolor and textbackground do?

textcolor _ _ textbackground _ _

Trang 4

But why doesn’t my program work?

Have you every asked this question yet? If you do not use the correct syntax

(i.e grammar) when typing a Pascal program, it simply won’t work Your

program must be error-free in order to work

When the compiler meets an error in a program, the cursor stops in the line

after the error An error message is displayed at the top of the screen If you press F1 the error message is further explained for you

The following program has errors Correct the errors and make the program work on a computer

program colourful text;

uses crt;

begin

clrcsr;

textcolour(green);

writeln(‘I am programming);

textcolor(12)

writeln(Using Turbo Pascal’);

readln;

end

In the box below write down the corrected version of the above program

_ _ _ _ _ _ _ _ _ _ _

Trang 5

The four basic arithmetic operations

The four basic arithmetic operations that one can use in Pascal are :

plus + minus - multiply * divide DIV, MOD, / You will now be looking at a few exercises to see how each operation can work

Addition, Subtraction and Multiplication

Adding, subtracting and multiplying numbers in Pascal couldn’t be easier Copy and run the following program on a computer to see how these arithmetic operations work

program arithmetic_operations;

{adding, subtracting and multiplying}

uses crt;

begin

clrscr;

textcolor(green);

writeln(‘Addition: 5 + 2 = ‘,5 + 2);

writeln(‘Subtraction: 15 - 7 = ‘,15 - 7);

writeln(‘Multiplication: 8 x 3 = ‘,8 * 3);

readln;

end

Write down the results that you see on the screen in the box below

Division

Using Pascal, there are three ways in which you can divide two numbers Try out each method Division using ‘/’

program division;

{division using /}

uses crt;

begin

clrscr;

writeln(‘Division: 17 / 3 = ‘,17/3);

readln;

end

Write down the output that you see on the screen in the box below

Trang 6

Counting the decimal places

Look at the result from the previous program Can you see too many zeros? How about we format the number to two decimal places instead?

Go back to the previous program Change the line to the one that is displayed here writeln(‘Division: 17 / 3 = ‘,17/3:0:2);

Now write down the output that you see on the screen in the box below

Division using ‘DIV’ and ‘MOD’

DIV and MOD are two other ways of performing division But with a difference Copy and run the following program on a computer Look at the results – what do you think is the difference between DIV and MOD?

program division;

{division using DIV and MOD}

uses crt;

begin

clrscr;

writeln(‘Division: 17 DIV 3 = ‘,17 DIV 3);

writeln(‘Division: 17 MOD 3 = ‘,17 MOD 3);

readln;

end

Write down the output that you see on the screen in the box below

So what’s the difference between DIV and MOD?

DIV

_ MOD _

So what have you been doin’…?

Up till now you have been writing out and running simple programs on a computer

using Pascal Very straight-forward programs They perform simple

tasks that are set by YOU, the programmer But how about a little

This means that the number will be displayed

to 2 decimal places

Trang 7

input from the user while the program is running?

The next part of these notes will show you how to accept inputs while a program is running

Variables and data types

If you want to handle any data while the program is running, you must set up variables These are areas of memory, idenified by a name written into the program When setting up a variable, you must say what type of data is to be stored there, as different types require different amounts

of memory

Declaring a variable

To accept input while a program is running, you have to declare a variable name and its datatype

A variable name:

must be a single word;

can contain letters and numbers;

cannot start with a number

You cannot use a reserved word (words used by Pascal) as a variable name, e.g begin

A datatype is used to specify the type of input The following are the datatypes you will be using:

integer A whole number, in the range -32,768 to

+32,767 real Any number, with or without a decimal

fraction char A single character, which can be a letter,

symbol or digit – in fact, any character from the ASCII set

boolean These can only store the values ‘TRUE’ or

‘FALSE’ Boolean variables are generally used to store the results of logical tests

string Can be used to store a set of zero or more

characters

INTEGER and REAL data types

You’ve typed programs that add numbers – the only problem is that they only add the same two numbers! The following is a program that adds two numbers that the user can enter while the program is running Copy and run the following program

Trang 8

program input_numbers;

{adding numbers using variables}

uses crt;

{this is the part where you declare a variable}

var

num1, num2: integer; {an integer is a whole number}

begin

clrscr;

writeln(‘Enter the FIRST number: ‘);

readln(num1);

writeln(‘Enter the SECOND number: ‘);

readln(num2);

writeln(‘Result after adding the numbers is ‘,num1 + num2);

readln;

end

Write down the output that you see on the screen in the box below

You have just written a program that adds any two numbers Well done!

Modify the program above so that it will output the subtraction, multiplication and

division for the two numbers entered by the user

In the box below write down the new version of the above program

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _

Declaring variables

Notice the use of var

when declaring variables

Trang 9

The assignment statement

There are two ways of storing variables: they can come in from the keyboard (the user’s input),

or values can be assigned from within the program You have seen how to input numbers and

store them in variables when you typed and compiled the previous program (on page 8)

Variable := value

Values are assigned to variables with the operator “ := ” – a colon followed by an equals sign The variable name sits to the left, and on the right is an actual value, another variable, a function,

or an expression that produces a value Here are some examples:

number_1 := 99;

number_2 := 103;

answer := number_1 + number_2;

total := total + next;

name := ‘SAM’;

Copy and run the following program

program values_variables;

{assigning values to variables}

uses crt;

var

a, b, c :integer;

decimal :real;

letter :char;

YesNo :boolean;

begin

clrscr;

a := 2;

b := 3;

c := a + b;

decimal := 9.99;

letter := ‘X’;

YesNo := TRUE;

writeln(‘a = ‘,a ,’b = ‘,b ,’c = ‘,c);

writeln(‘Decimal holds ‘,decimal:0:2);

writeln(‘The letter is ‘,letter);

writeln(‘The value of YesNo is ‘,YesNo);

readln;

end

Write down the output that you see on the screen in the box below

Trang 10

Exercises

Read the following problems and then try to solve them by writing a program Once the

programs work successfully on a computer, write them out on a foolscape and pass them on to your teacher

1 Write a program where the user will be asked to input his/her name and the year of birth The program will calculate how old the person is and then output the person’s name together with his/her age

Write down the output that you see on the screen in the box below

2 Write a Fahrenheit to Centrigrade conversion program The progra will take a Fahrenheit temperature as input and ouputs the corresponding temperature in Centigrade Use the formula C = ( F – 32 ) x 5/9 where C is the temperature in Centirgrade and F is the

temperature in Fahrenheit

Write down the output that you see on the screen in the box below

3 Write a program that accepts three numbers as input, calculates their total and average, and outputs the results on screen

Write down the output that you see on the screen in the box below

Trang 11

Conditional statements

The programs you have seen up till now are sequential – i.e the program goes through all the

instructions, one after another It is often necessary to take different routes through a program depending on some condition This is known as branching Branches make a program flexible, allowing a program to vary its actions in response to incoming data This can be achieved in Pascal by the if-then statement

if-then statement

This is the basic syntax:

if test then statement;

… or where there are several statements:

if test

then

begin

statements;

end;

The test checks the value held by a variable if the condition proves true, then the program

performs the following statement(s) If the condition does not prove true, the statements are ignored

Conditional expressions

The tests use these operators:

<= is less than or equal to >= is greater than or equal to

Some examples of valid tests:

if x > 99 true if the value in x is greater than 99

if letter <= ‘Z’ true if a capital letter or other character lower down in the ASCII set

if ans <> correct true if th evariables do not match Copy and run the following program

program calculator;

uses crt;

var

num1, num2, answer :real;

op :char;

Ngày đăng: 23/10/2014, 11:47

TỪ KHÓA LIÊN QUAN