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

Welcome to Pascal Programming

This document provides an overview of Pascal programming, including the declaration of data types, variables, and constants. It outlines the structure of a Pascal program, types of statements, and the differences between input, output, and assignment statements. Additionally, it explains the use of comments for internal documentation and provides an example program segment demonstrating these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Welcome to Pascal Programming

This document provides an overview of Pascal programming, including the declaration of data types, variables, and constants. It outlines the structure of a Pascal program, types of statements, and the differences between input, output, and assignment statements. Additionally, it explains the use of comments for internal documentation and provides an example program segment demonstrating these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Welcome to Pascal Programming

At the end of this class you should be


able to:
1. Declare data types in Pascal
2. Use data type for variables and constants
3. Manipulation of Data
i. Writing to variables
ii. Reading from variables
iii. arithmetic operations
Pascal Rules
1. A program name is compulsory
2. Every statement must end with a semicolon (;)
3. All variables and constants used in the
program must be declared.
4. Every program must end with end.
Data Types used in Pascal
These are the common data type of Pascal program.
Additional
Type Explanation Example
explanation

Whole
integer 3, 104, 0, -9
numbers

(Floating point
real Real numbers 4.0, -0.08, 48.6, 2.0E4
numbers)

char Character (One character) 'b', 'A', '7'

string String variable (Text) 'Hello World', ‘Jamhi’


Boolean Can only be True or
boolean True, False
variable False
Pascal Program Structure
program programname;

declaration of constants;
declaration of variables;
begin
program statements;
readln;
end.
Declaration and Initialization of
Constants and Variables
Recall: Variables and Constants MUST have
meaningful names.
We declare and initialize constants and variables in
Pascal as using these rules:
1. Constants are always declared before variables.
2. Constants are initialize upon declaration using
equal sign (=)
3. Variables are initialized after begin using colon
equal (:=)
Declaration and Initialization of
Constants and Variables
Structure Example
program programname; program areaofcircle;
const const
constant= value; pi= 3.14;
var var
variable1, variable2: data type; radius, area: real;
variable3, variable4: data type; count, sum: integer;
begin begin
variable1:= value; radius:= 0.0;
Program Statements
Pascal Programs have four (4) main types of
statement. They are:
1. Input statements
2. Assignment statements
3. Output statements.
4. Comments
Input Statements
• In Pascal we use readln(variable); for single
input and readln(variable, variable ...); for
multiple. When the computer see readln, it
pause and waits for a user inputted value.
• After the value is typed and the ENTER key is
pressed, the value will be stored in the
variable following readln.
• If multiple input is required a space is used to
separate the values followed by ENTER.
Assignment Statements
• An assignment statement is used to store a
value in a variable.
• An assignment statement is written as
follows:
left value := right value
The right value is a literal or an expression that
evaluates to a value that will be stored in the
left hand value which is a variable.
Output Statements
• In Pascal we use write and writeln to achieve
output. They are used to output strings,
variable values or a combination of both
separated by a comma (,).
Pascal Code User sees
writeln(‘I am ok’); I am ok
write(‘My name is ’, name); My name is Jamhi
write(‘My name is ’, name,‘ . I am ’, age); My name is Jamhi. I am 30
writeln;
Difference between write and writeln
• A write statement outputs information to the screen
and positions the cursor at the end of the output.
• A writeln statement outputs information to the
screen and positions the cursor on a new line below
the output.
Pascal code User sees
write(‘I am great’); I am great_
writeln(‘I am great’); I am great
_
Comments
• Comments are used for internal documentation.
That is instruction written to explain what is being
done in a program.
• In Pascal comments, statements are enclosed in
curly brackets or curve brackets and stars eg.
{comment statement}
(* This is also a comment*)
//double solidus – used for single line comments
• Anything appearing between {} or (**) or follow //
is ignored by the compiler.
Example program segment
Code
write ('Enter the radius: '); //Prompts user for radius
readln (radius);
area:=pi*radius*radius; {finds the area}
writeln('The area of the circle with radius ', radius:3:2, ‘ is ’ , area:3:2);
(*Statement outputting original value of radius and area with decimal
controls*)

Assuming radius is 7 User sees


Enter the radius: 7
The area of the circle with radius 7.00 is 154.00

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy