Table of Contents

Introduction

Basics of Programming

Variables

Input and Output

Boolean Expressions and Branching

Loops

Functions and Procedures

Files

Arrays

HOME

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

CHAPTER 2

BASICS OF PROGRAMMING

All programs consist of three things:

  • Commands
  • Data
  • Comments

 

Commands

Commands are words that tell the computer what to do. These words are called "Reserved Words" or "Keywords." The following table is a list of Pascal commands. Don't worry about what each word means. We will cover the functions of the most common keywords in later chapters.

 

ABS ARCTAN  AND ARRAY BEGIN BOOLEAN CASE
CHAR CHR  CONST COS DISPOSE DIV DO
DOWNTO ELSE END EOF EOLN EXP FALSE
FILE FOR FORWARD FUNCTION GOTO IF IN
INPUT INTEGER LABEL LN MAXINT MOD NEW
NIL NOT ODD OF OR ORD OUTPUT
PACK PACKED PAGE PROCEDURE PROGRAM PRED PUT
REAL READ READLN RECORD REPEAT RESET REWRITE
ROUND SET SIN SQR SQRT SUCC TEXT
THEN TO TRUE TRUNC TYPE UNPACK UNTIL
VAR WHILE WITH WRITE WRITELN    

 

Data

While commands are very important, they are useless without data to perform actions on. If someone instructed you to run you would want to know in which direction and how fast. These are examples of data.

There are two types of data:

  • Constants
  • Variables

Constants

Constants are types of data that never change. For example, the number 2 is a constant. It will always equal 2 no matter what. In Pascal, constants are values that don't change while the program is executing.

 

Variables

As their name implies, variables are values that can change. Your age, for instance, is a variable it constantly changes. In Pascal, variables function much like variables in algebra. They have a letter or symbol that represents the variable. In Pascal, these letters or symbols are called "Identifiers." Unlike variables in algebra, identifiers have several naming rules. They are:

 1. An Identifier can be made up of multiple characters, numbers and the "_" character
 2. The identifier name must start with a letter
3. Reserved words cannot be identifiers

The following are examples of valid identifier names:

 

 

 x
years
weeks_in_a_year
MyProgram

 

The following are examples of invalid identifiers

 

 Invalid Identifier

Reason For being invalid
 1year Cannot start with a number
Program Is a reserved word
annual%Rate Cannot use special characters such as the "%" sign.
miles-per-hour Can only use "_" not "-"

Comments

Sometimes, after writing a program, you must go back and modify it. When you look at it again you may not be sure about what each statement in the source code is doing. Comments allow you to put remarks and notes to yourself inside the program's source code. Although comments don't affect the way your program executes, it is very important that you put comments inside your program to make it clearer.

 

There are two ways to put comments in Pascal source code:

  • Type the comment inside curly braces { }. Any text inside the braces will be considered a comment and will be ignored by the compiler

 

  • Type the comment between the "(*" and "*)" characters. Anything inside these will also be considered a comment

 

Your First Program

The following program is a very simple, but functional example of a Pascal program. It contains examples of many of the topics discussed above. To run this program type follow these steps.

1. Start the Turbo Pascal Compiler.

2. Type the program exactly as you see it

3. Click on COMPILE on the menu bar and choose the RUN option from the menu.

 


Program SimpleProgram; Begin { This program displays } {"HELLO WORLD" on the screen } Writeln('Hello World'); End.


Each section of the program has an important function.

Program Heading

The first line in this example is called the program heading. This line is optional in Turbo Pascal, but it is required in all other types of Pascal. It contains the keyword PROGRAM; and the program name, which must be a valid identifier.

 

Executable section

After the heading comes the executable section. The executable section is where the main program is written. The start of the executable section is marked by a "BEGIN" statement. The end is marked with the word "END" followed by a period.

 

The executable section in this example contains several statements. The first statement after the BEGIN is a comment. The comment describes what the program is doing.

 

The second statement contains two important elements. It contains the "Writeln" command. This command instructs the computer to print output on the screen. The "Writeln" command is followed by a set of parentheses which contain data. In this case the data is the phrase "Hello World." This is considered a constant.

Notice that the first statement is followed by a semicolon. In Pascal all statements must be terminated by a semicolon.

 

Now that you have completed this chapter. You may take the end-of-chapter quiz and continue on to chapter 3.