0% found this document useful (0 votes)
2 views

A Crash Course in Pascal - 2024

The document provides a crash course in Pascal programming, outlining key concepts such as naming and declaring programs, using keywords like BEGIN and END, and the importance of semicolons. It explains how to print messages, capture user input, and utilize loops and comparative statements. Additionally, it includes practice questions for writing programs that handle utility bill information.

Uploaded by

amanolamey1221
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

A Crash Course in Pascal - 2024

The document provides a crash course in Pascal programming, outlining key concepts such as naming and declaring programs, using keywords like BEGIN and END, and the importance of semicolons. It explains how to print messages, capture user input, and utilize loops and comparative statements. Additionally, it includes practice questions for writing programs that handle utility bill information.

Uploaded by

amanolamey1221
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

A Crash

Course in
Pascal
By: GT
Things to KNOW

When programming in
Pascal, There are certain
KEY things to remember in
order to avoid errors.

1. Name & Declare


2. Begin
3. Body
4. Brackets & Semicolons
5. End

Before we begin though, lets recap.


Most statements in Pascal need to be terminated
by a ; semicolon.

Remember how to use your functions

Remember to properly terminate your program


Type the above code as is and run to execute.
Now lets begin this crash course.
1. NAME & DECLARE

Always name your Pascal Program.


Program MyProgram ;

Name all variables you will use. A variable name


must not start with a number or contain a space.

When declaring variables always begin with the


Keyword VAR

Declare variable names and variable datatypes.


VAR EmployeeName: String ;
2. BEGIN

After naming your program and declaring your


variables and their datatypes, you are now ready
to BEGIN the body of your program.

The body of your program begins after the BEGIN


keyword is found between the BEGIN and END
keywords
3. BODY

Whatever it is that your program is supposed to


do, the code to achieve that should be placed
between the BEGIN and END keywords. At the
end of the end of the program, the END keyword
is followed by a full stop. Eg END.
The body of your program will contain one or
more of the following:

 Comparative statements
 Blocks including / * + - = operators
 Loops & iterations
 Nested comparative statements
 Nested loops and iterations

Lets recap some basics for a second.


How do we print a message to the screen
??
To print a message to the screen we use the writeln() function.
Writeln is short for Write Line. The message we want to print
to the screen needs to be enclosed in single quotes.
Eg.

How do we capture and store input from the user ??


To capture and store a user’s response, we use the readln()
function. Readln is short for Read Line, we do need a variable
of corresponding datatype to store it though.
Eg.

Have you noticed that all statements are followed by


semicolon ?

Have you also noticed that the quotation marks are not
used with readln ?
How do we print the value that a variable has ??
To print a variable to the screen you can use one of 2 ways. Eg

When you would like to add a message before the value, use the top
option.
To print just the value to the screen you can use the last option.

How do we assign a value to a variable ??


We use the assignment operator :=
Eg
How do we deal with loops and
iterations ??

Loops & Iterations are the same thing. There


are 3 basic loop types used in Pascal. For
Loop, Repeat Until and While Loop. They
can do the same things depending on how
you write them. There are key things to
remember about each loop though.
For Loops

FOR loops are counter controlled loops. In the snippet


above, notice the opening line for a FOR loop. FOR
X:=0 to 7 DO. This simply means that X is a counter;
and the loop will execute as many times as possible
until X reaches 6, yes 6. Please count 0,1,2,3,4,5,6
…. In total that is 7 iterations.

The body of a FOR loop starts with a BEGIN and ends


with an END;
FOR loops are counter controlled loops. In the
previous snippet, notice the opening line for a
FOR loop. FOR X:=0 to 7 DO. This simply means
that X is a counter; and the loop will execute as
many times as possible until X reaches 6, yes 6.
Please count 0,1,2,3,4,5,6 …. In total that is 7
iterations.

The body of a FOR loop starts with a BEGIN.

Whatever is within the body of the FOR loop,


between BEGIN & END, will happen as many
times as the loop execute.

The body of a FOR ends with an END;


Repeat Until Loops

REPEAT UNTIL loops are sentinel controlled


loops. In the snippet above, notice the opening
line for the loop. Repeat. This simply means
that whatever follows the keyword Repeat, or
whatever is within the body of the loop, will
execute as many times as the loop repeats.

The body of the loop starts with a REPEAT and


ends with an UNTIL, the sentinel;
REPEAT UNTIL loops are sentinel controlled loops. In the
snippet above, notice the opening line for the loop. Repeat.
This simply means that whatever follows the keyword
Repeat, or whatever is within the body of the loop, will
execute as many times as the loop repeats.

The sentinel is this case is X, and the loop will execute until X
= 10.

Notice within the loop:


1. A writeln() statement that prints the value of X
AND
2. A statement that increases the value of X each time the
loop executes

The body of the loop starts with a REPEAT and ends with an
UNTIL the sentinel, X=10; Please notice that in the line
before the loop starts, X was assigned the value of 0, X:=0;
Comparative Statements

Comparative statements usually are IF-THEN-ELSE situations.

If you are not familiar with IF-THEN-ELSE, then you should try and
remember this. IF this condition THEN do this ELSE do that. Its
really that simple.

Please note, the IF THEN ELSE is set up as follows


IF followed by the condition that is being tested.
THEN, the body of which is found between a BEGIN and an END
ELSE, the body of which is found between a BEGIN and an END
After which is an end; which ends the entire IF THEN ELSE block.
Nested comparative statements are possible. So
are nested loops and iterations.

Comparative statements within loops and loops


within comparative statements are also possible
depending on the nature of the program you are
writing.

In the next session we will look at more


complex statements in Pascal.
End.

After the body of your program is complete, you will


need to terminate your program properly.

A readln; before your END. Will ensure that the


program waits for input from the user before it
closes.

NOTE that the END at the end of the program is END


followed by a full stop, not a semicolon. . NOT ;
Practice Questions
Write a program that will take information for 3 utility bills.
Bill Name, Bill Amount is to be accepted by the program, it
should then calculate and display the total paid as well as
individual bill information at the end.

Write a program that will take information for 3 utility bills.


Bill Name, Bill Amount is to be accepted by the program, it
should then calculate and display the total paid, average
bill amount as well as individual bill information at the end.

Write a program that will take information for 3 utility bills,


Bill Name, Bill Amount. The program will also need to
display all bill information at the end as well as the total
amount paid for the bills and the average bill amount. You
are also required to show the information for the highest bill
amount.

You might also like