Friday, January 20, 2017

Lecture 07 - Pascal Programming (Part I)



Dr. Niklaus Wirth of the Swiss Federal institute of Technology, invented specification for a highly-structured language which resembled ALGOL in many ways. He named it Pascal after the'l7th-century French philosopher and mathematician who built a working mechanical digital computer.

  • Pascal is very data-oriented, giving the programmer the ability to define custom data types.
  • Pascal was intended as a teaching language.
  • Pascal is free-flowing, and reads very much like a natural language, making it very easy to understand code written in it.
  • Pascal was a programming language intended for scientific computing but with the introduction of file handling features it became multipurpose.
  • The elements of a program must be in the correct order, though some may be
  • Pascal is a procedural language

Basic Pascal Syntax
Comments in Pascal
Comments are portions of the code which do not compile or execute. Pascal comments start with a (* and end with a *). You cannot nest comments: (* (* *) ")

(* This is a test program *)

Turbo Pascal and most other modern compilers support brace comments, such as {Comment}. The opening brace signifies the beginning of a block of comments, and the ending brace signifies the end of a block of comments.

{ This is also a comment in Turbo Pascal }

The Basic Structure of a Pascal Program

PROGRAM Program Name (File List);

CONST
(* Constant declarations *)

TYPE
(* Type declarations *)

VAR
(* Variable declarations *)

(* Sub program definitions *)

BEGIN
(* Executable statements *)
END.

The elements of a program must be in the correct order, though some may be omitted if not needed.

SAMPLE PROGRAM:

PROGRAM HelloWorld;
BEGIN
Writeln ("Welcome to Pascal World...");
END

Rules for Identifiers
Identifiers are names that allow you to reference stored values, such as variables and constants. Also, every program and unit must be named by an identifier.

  • Must begin with a letter from the English alphabet or underscore
  • Can be followed by alphanumeric characters (alphabetic characters and numerals) and possibly the underscore (_).
  • Spaces are not allowed
  • Not case sensitive
  • May not contain certain special characters, many of which have special meanings in Pascal. (- ! @ #$%^&*()+' -={}[] :";'<>?,./|)
  • Several identifiers are reserved in Pascal as syntactical elements. You are not allowed to use these for your identifiers. Example BEGIN, WHILE , IF END etc.,
  • Identifiers can be any length, but some Pascal compilers will only look at the first several characters.

Constants
Constants are referenced by identifiers, and can be assigned one value at the beginning of the program. The value stored in a constant cannot be changed. Constants are defined in the constant section of the program:

CONST
Identifierl = value;

For example, let's define some constants of various data types: strings, characters, integers, reals, and Booleans.

CONST
Name ='BCS';
Year = 2017;


Note that in Pascal, characters are enclosed in single quotes, or apostrophes (')

The Basic Data Types in Pascal 

  • Integer
  • Real
  • Char
  • String
  • Boolean
Integer 

  • Numerical
  • Positive or Negative whole numbers
  • Two bytes allocated
  • Range -32768 to +32767
  • Stored in pure binary form
  • Cannot store vary large numbers and very small numbers

Real
 
  • Numeric data
  • Positive or Negative numbers with decimal places
  • Six bytes allocated
  • Stored as binary fractions
  • Can store vary large numbers and very small numbers

Char
 
  • Character data
  • Single character at a time
  • One byte allocated
  • Enclose them in single quotes

String
 
  • Character data
  • A chain of Characters
  • 255 bytes allocated
  • Enclose them in single quotes

Boolean
 
  • Enumerated data
  • Can have only two values
  • 1 byte allocated
  • True (1) or false (0)

Declaring Variables
Variables are similar to constants, but their values can be changed as the program runs. Variables must first be declared in Pascal before they can be used: An example of declaring several variables is:

VAR
age, year, grade : Integer;
circumference : Real;
LetterGrade: Char;
DidYouFail : Boolean;

No comments:

Post a Comment

Important Notice!

Dear students and friends. When you commenting please do not mention your email address. Because your email address will be publicly available and visible to all. Soon, it will start sending tons of spams because email crawlers can extract your email from feed text.

To contact me directly regarding any inquiry you may send an email to info@bcslectures.website and I will reply accordingly.