Colorful Pascal Programming
Colorful Pascal Programming
Video Games
Perform Calculations
Make Spacecrafts
Which of these have Computer Program?
Smart TV
Playstation Gaming Console
Mars Rover
Digital Car Radio
Saw
Which of these have Computer Program?
Smart TV
Playstation Gaming Console
Mars Rover
Digital Car Radio
Saw
If it has a
CPU
Then it uses a software program
What is needed to write a program?
Code
Compiler
Computer
How to write a program?
Code Algorithm Instructions / Programming Language
Compiler Translates the code
Computer Performs the instructions in its language
CODE Programming Language
Low Level Languages High Level Languages
Machines friendly Human Friendly
Machine dependent Computer independent
Hard to write Easy to write
Fast execution Longer to execute
Converted into machine level language
Low Level Languages
Machine Friendly
Machine Dependent
Hard to write
Fast execution
High Level Languages
Human Friendly
Computer independent
Converted into machine level language
Easy to write
Longer to execute
Programming Building Blocks Simple Sequence
PROGRAM NAME
VARIABLES
START
PRINT
READ
STOP
Programming Building Blocks Simple Sequence
PROGRAM NAME Labels the name of the program
VARIABLES declare inputs and outputs types
START identifies the beginning of program
PRINT Display outputs
READ Receive inputs
STOP end of program
Programming Example Pseudocode
START
name=’ABC’
age= 0
PRINT “What is your name?”
READ name
PRINT “How old are you?”
READ age
PRINT “Hello”, name , “you are ”, age , “years old”
STOP
Programming FLOWCHART
(Input/Output symbol)
Program Nameage(output);
var
name: string;
age: integer;
begin
name:= 'ABC';
age:=0;
writeln('What is your name?');
readln(name);
writeln('What is your age');
readln(age);
writeln('Hello! ', name, ' Your age is ', age );
end.
Programming Pascal Code
Programming Pascal Code
Program Name, no spaces
Programming Pascal Code
Declaring variables
variables. Name is String
variables. Age is Integer, no letters
Programming Pascal Code
Start
Programming Pascal Code
Start
Initialize Variable
Initialize Variable
Output
Input and store
Output
Input
Output
Stop
Programming Pascal Code error
Can you spot the Error?
Can you spot the Error?
Semicolon ; is missing
Can you spot the Error?
Can you spot the Error?
Semicolons ;
End. no fullstop at the end
Space in names read ln , program name
Programming Pascal Code Comments
{
Comments in multi lines
}
Programming 2nd Example Enter name and calculate age
Program Nameage(output);
var
name: string;
age , newage: integer;
begin
name:= 'ABC';
age:=0;
newage:=0;
writeln('What is your name?');
readln(name);
writeln('What year were you born?');
readln(age);
newage:= 2024 - age;
writeln('Hello! ', name, ' Your age is ', newage );
end.
Programming 2nd Example Enter name and calculate age
Data Types or Variables or Var
Visit : https://www.tutorialspoint.com/pascal/pascal_variable_types.htm
Character
Integer
Real
String
IF condition THEN
action(s)
ELSE
action(s)
ENDIF
Programming FLOWCHART
If Then ELSE Statements ConditionalDiamond
branches Decisions
(Decision symbol)
THEN
IF
Condition
ELSE
Programming FLOWCHART
If Then ELSE Statements Conditional branches Decisions
Example
Write a program to calculate if a student passes his
exam. If his score is above 95 then he passes
Note you must do:
Pseudo code
Flowchart
Pascal code
Example
Write a program to calculate if a student passes his exam. If his score is above 95 then he passes
Pseudo code
START
score= 0
Print “Enter your Score”
READ score
FLOWCHART
Example
Write a program to calculate if a student passes his exam. If his score is above 95 then he passes
Version 1
Program StudentPass(output);
var
score: integer;
begin
writeln('Please enter your score');
readln(score);
if score >90 then
writeln(' You have passed')
else
writeln(' you did no pass');
end.
Version 2
Program StudentPass(output);
var
score: integer;
begin
writeln('Please enter your score');
readln(score);
if score >90 then begin
writeln(' You have passed');
end
else begin
writeln(' you did no pass');
end
end.
Loops
FOR
WHILE
Loops
FOR
Finite
Executes a sequence of statements multiple times and abbreviates the code that
manages the loop variable.
WHILE
infinite
Repeats a statement or group of statements while a given condition is true.
It tests the condition before executing the loop body.
Loops
FOR
Program StudentPass(output);
var
i, passed, score: integer;
begin
score:=0;
i:=0;
passed:=0;
for i:=1 to 4 DO
begin
writeln('Please enter your score');
readln(score);
if score >90 then
writeln(' You have passed')
else
writeln(' you did not pass')