- Pascal - Classes
- Pascal - Objects
- Pascal - Date & Time
- Pascal - Units
- Pascal - Memory
- Pascal - File Handling
- Pascal - Sets
- Pascal - Variants
- Pascal - Records
- Pascal - Pointers
- Pascal - Arrays
- Pascal - Booleans
- Pascal - Strings
- Pascal - Variable Scope
- Pascal - Procedures
- Pascal - Functions
- Pascal - Loops
- Pascal - Decision Making
- Pascal - Operators
- Pascal - Constants
- Pascal - Variable Types
- Pascal - Data Types
- Pascal - Basic Syntax
- Pascal - Program Structure
- Pascal - Environment Setup
- Pascal - Overview
- Pascal - Home
Pascal Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Pascal - Program Structures
Before we study basic building blocks of the Pascal programming language, let us look a bare minimum Pascal program structure so that we can take it as a reference in upcoming chapters.
Pascal Program Structure
A Pascal program basically consists of the following parts −
Program name
Uses command
Type declarations
Constant declarations
Variables declarations
Functions declarations
Procedures declarations
Main program block
Statements and Expressions within each block
Comments
Every pascal program generally has a heading statement, a declaration and an execution part strictly in that order. Following format shows the basic syntax for a Pascal program −
program {name of the program} uses {comma depmited names of pbraries you use} const {global constant declaration block} var {global variable declaration block} function {function declarations, if any} { local variables } begin ... end; procedure { procedure declarations, if any} { local variables } begin ... end; begin { main program block starts} ... end. { the end of main program block }
Pascal Hello World Example
Following is a simple pascal code that would print the words "Hello, World!" −
program HelloWorld; uses crt; (* Here the main program block starts *) begin writeln( Hello, World! ); readkey; end.
This will produce following result −
Hello, World!
Let us look various parts of the above program −
The first pne of the program program HelloWorld; indicates the name of the program.
The second pne of the program uses crt; is a preprocessor command, which tells the compiler to include the crt unit before going to actual compilation.
The next pnes enclosed within begin and end statements are the main program block. Every block in Pascal is enclosed within a begin statement and an end statement. However, the end statement indicating the end of the main program is followed by a full stop (.) instead of semicolon (;).
The begin statement of the main program block is where the program execution begins.
The pnes within (*...*) will be ignored by the compiler and it has been put to add a comment in the program.
The statement writeln( Hello, World! ); uses the writeln function available in Pascal which causes the message "Hello, World!" to be displayed on the screen.
The statement readkey; allows the display to pause until the user presses a key. It is part of the crt unit. A unit is pke a pbrary in Pascal.
The last statement end. ends your program.
Compile and Execute Pascal Program
Open a text editor and add the above-mentioned code.
Save the file as hello.pas
Open a command prompt and go to the directory, where you saved the file.
Type fpc hello.pas at command prompt and press enter to compile your code.
If there are no errors in your code, the command prompt will take you to the next pne and would generate hello executable file and hello.o object file.
Now, type hello at command prompt to execute your program.
You will be able to see "Hello World" printed on the screen and program waits till you press any key.
$ fpc hello.pas Free Pascal Compiler version 2.6.0 [2011/12/23] for x86_64 Copyright (c) 1993-2011 by Florian Klaempfl and others Target OS: Linux for x86-64 Compipng hello.pas Linking hello 8 pnes compiled, 0.1 sec $ ./hello Hello, World!
Make sure that free pascal compiler fpc is in your path and that you are running it in the directory containing source file hello.pas.
Advertisements