You are on page 1of 23

Comparison of Prog’g

Languages
PASCAL
{1} program tutorial1; uses crt;
{2} var
{3} world_stmt: string;
{4} begin
{5} world_stmt := 'Hello world!';
{6} writeln(world_stmt);
{7} end.

{1} this is the programID, and the uses clause.


program <identifer>; uses <library>; is the syntax.<identifier> may be
anything that we may decide to call the program. <library> is a
specification of additional libraries, or *units* of commands that we may
want to use.
crt is the name of one library we will use a lot in our programming. The
commands that can be accessed in this library will be detailed later on in
the tutorial. If we want to use any of the commands in a library, we must
tell the compiler to use the library, and the uses statement does this. I
will specify if any commands we use will come out of a unit.
{2} var is a signal we use at the beginning of a program block to tell the
compiler that we want to start defining variables.
{3} This is a definition of a string, or sequence of text. Variable definitions
will be covered later in this part. We are defining a variable named
world_stmt to be a string.
{4} begin says we want to begin a block of code.
{5} & {6} are some program commands. We will explain them later.
{7} end. ends a program block.
Variable Types:
string : a section of text. "Hello world!"
would be a string.
integer : a number which does not have
a decimal part. 12 is an integer.
char : one part of a string. "G" is a
character, while "GG" is not.
real : a number which has a decimal
part. 3.25 would be a real.
boolean : a variable with only 2 values,
True or False
Comments

• { } for single-line comments


• (* *) for multi- line comments
Declaring Variables
• The keyword var signals the compiler that we want to
start defining variables
• The syntax is:
Var variable_name : Variable_type;
Ex.
Var num1 : integer;
Var num1 : real;
Var name : string;
Var letter : char;

• You can declare variables of the same type in a single


line.
Ex. Var num1, num2 : integer;
The assignment operator

• We use the := to assign a value to a variable. Examples of that


would be such as:

world_stmt := 'Hello world.';


{ a definition of a string. The (‘)s must be there on each side }

choice_char := 'a';
{ a definition of a character . The (‘)s must be there on each side }

money := 3.25; { a definition of a real }

coins := 10; { a definition of an integer }


Arithmetic Operators
Operator Description Example Result

a= 6; b=5;

+ addition Sum:=a+b; 11

- Subtraction Diff:=a-b; 1

* multiplication Prod:=a*b; 30

/ Integer division Div:=a/b; 1.2

Div division Whole:=a div b; 1

mod modulus Remndr:=a mod b; 1


A program to point up the use of arithmetic operators:

program tutorial2;
var first_number, second_number,sum,product,difference: integer;
quotient : real;
begin
first_number := 3; {assign first number the value of 3}
second_number := first_number * 2; {assign 2nd number-multiply first number by 2}
first_number := second_number - 5; {assign 1st number-old value of 1st number - 5}
sum := first_number + second_number;
difference := first_number - second_number;
product:= first_number * second_number;
quotient := first_number / second_number;
writeln(sum);
writeln(difference);
writeln(product);
writeln(quotient);
end

The output:
7
-5
6
0.16667
Relational Operators –returns True or False

Operator Description Example Result

a= 6; b=5;

> Greater than a>b True

>= Greater than or a>=b True


equal to
< Lesser than a<b False

<= Lesser than or a<=b False


equal to
<> Not equal to a<>b True

= Equal to / equality a=b False


operator

You might also like