+ All Categories
Home > Documents > Programming, an introduction to Pascal The Basics of Program writing.

Programming, an introduction to Pascal The Basics of Program writing.

Date post: 05-Jan-2016
Category:
Upload: ethel-martin
View: 223 times
Download: 1 times
Share this document with a friend
34
Programming, an introduction to Pascal The Basics of Program writing.
Transcript
Page 1: Programming, an introduction to Pascal The Basics of Program writing.

Programming, an introduction to PascalThe Basics of Program writing.

Page 2: Programming, an introduction to Pascal The Basics of Program writing.

Generations of Languages

•Classification of programming languages▫1st Generation Machine Code▫2nd Generation Assembly Language▫3rd Generation Pascal▫4th Generation programming languages▫5th Generation programming languages

LowLevel

HighLevel

Page 3: Programming, an introduction to Pascal The Basics of Program writing.

Machine language vs Assembly Language•Machine Language

▫Consists of strings made of 1’s and 0’s. It is the only programming language as computer can understand.

•Assembly Language▫Uses special codes called MNEMONIC to

represent the language instructions instead of using 1’s and 0’s.AD, SUB, JMP, MUL

Page 4: Programming, an introduction to Pascal The Basics of Program writing.

Low-Level languages

•They are machine dependent (The code can only be understood by a machine).

•FIRST GENERATION LANGUAGES▫Made of Machine code. Eg. 1001011011

•SECOND GENERATION LANGUAGES▫Assembly Language with simple commands

like “Add A,B” (Add the contents of A to B). Also called mnemonics.

Page 5: Programming, an introduction to Pascal The Basics of Program writing.

High Level Languages▫Not Machine dependent and use words

similar to English making them easier to write.

▫THIRD GENERATION LANGUAGES These languages are converted from simple

English to machine code. Eg. FORTRAN, BASIC, Pascal, C

▫FOURTH GENERATION LANGUAGES Easier to write by just giving simple

commands and no long lines of code. Eg. COBOL

Page 6: Programming, an introduction to Pascal The Basics of Program writing.

High Level Languages

•FIFTH GENERATION LANGUAGES▫These languages perform tasks based on

the goal to be achieved. In theory it should be able to translate the words a person says. Eg. PROLOG

Page 7: Programming, an introduction to Pascal The Basics of Program writing.

History of PascalPascal, named in honor of the French mathematician and philosopher Blaise Pascal, was developed by Niklaus Wirth.

Page 8: Programming, an introduction to Pascal The Basics of Program writing.

Pascal FormatProgram <program name>;

const<constant values in the program>;

var<variables used in the program>

begin<the actual program code>end.

HEADER

VARIABLE DECLARATION

PROGRAM BODY

CONSTANT DECLARATION

Page 9: Programming, an introduction to Pascal The Basics of Program writing.

Constants and Variables

•Identifier – The name that identifies a variable or constant

•Constant – a value that is stored in memory and cannot be changed. Eg. Pi = 3.14

•Variable – A value that is stored in memory and can be changed. Eg. x,a,b,y…….

Page 10: Programming, an introduction to Pascal The Basics of Program writing.

Variable Types

•Integer – positive and negative numbers which do not have a decimal point.

•Real – positive and negative numbers that may have a decimal point

•Char – a single character (letter, digit or symbol)

•String – a group of characters•Boolean – either true or false

Page 11: Programming, an introduction to Pascal The Basics of Program writing.

Declaring Variables

var

gender: char;num1, num2, num3: integer;product: real;

THREE VARIABLES OF THE SAME TYPE CAN BE

SEPARATED BY A COMMA

Page 12: Programming, an introduction to Pascal The Basics of Program writing.

Declaring Constants

const

pi := 3.142;VAT := 0.15;Gravity := 9.81;

Page 13: Programming, an introduction to Pascal The Basics of Program writing.

Basic Commands

•Write – writes the command on the same line

•Writeln – writes the command and goes to the next line

•Read – reads the input•Readln – reads the input and then goes to

the next line.

Page 14: Programming, an introduction to Pascal The Basics of Program writing.

Read Commands

Read(num1)This will read the input from the user of the program and store the value in num1

Readln(num1)This will read the input from the user and store the value in num1 and go to the next line.It is also used to pause at the end of a program until the user presses enter.

Page 15: Programming, an introduction to Pascal The Basics of Program writing.

Write commands

Write(‘ hello world’);

Writeln (‘hello world’);

Hello World DISPLAYS ‘HELLO WORLD’ IN THE SAME

LINE

Hello World_

DISPLAYS ‘HELLO WORLD’ AND GOES TO

THE NEXT LINE

Page 16: Programming, an introduction to Pascal The Basics of Program writing.

Assignments

The value of a variable can be changed by doing an assignment statement.It is done by using a colon and equal sign.:=

eg.Age := 15cost := 65Count := count + 1

Page 17: Programming, an introduction to Pascal The Basics of Program writing.

Simple Programs

Program HelloWorld; Begin

Write(‘Hello World’);Readln;

End.

This program writes Hello World and

then waits for the user to

press enter to exit the

program.

Page 18: Programming, an introduction to Pascal The Basics of Program writing.

Simple Programs

program Hello; begin   Writeln('Hello');   Write('world');   Readln;end.

This program writes ‘Hello’

in one line then goes to

the other, then it prints ‘World’ and waits for the user to press

enter.

Page 19: Programming, an introduction to Pascal The Basics of Program writing.

Simple ProgramsProgram Addition; Var

a:integer; Begin

Writeln('The sum of 6 and 4 is:');a := 6 + 4;write(a);readln;

End.

This declares ‘a’ as an integer.

It then adds two numbers and assigns the result to

‘a’.

Page 20: Programming, an introduction to Pascal The Basics of Program writing.

Simple ProgramsProgram Multiplication; Var

a: integer; Begin

Writeln ('This will find the product of 6 and 4');a := 6*4 ;writeln ('Press enter to view the result');readln;write(a);readln;

End.

This declares ‘a’ as an integer.

It then multiplies two numbers and assigns the result

to ‘a’. Then displays ‘a’

Page 21: Programming, an introduction to Pascal The Basics of Program writing.

Simple ProgramsProgram addition; Var

a:integer;b:integer;c: integer;

 Begin

writeln(‘Enter first number’);read(a);writeln(‘Enter second number’);read(b);writeln(‘Your numbers are:’ ,a ,b);c:= a + b;writeln(‘the sum of your numbers are/is:’);writeln(c);readln;readln;

End.

This declares ‘a’ ‘b’ and ‘c’ as

integers.It then adds ‘a’ to

‘b’ and assigns the result to ‘c’. Then displays ‘c’

Page 22: Programming, an introduction to Pascal The Basics of Program writing.

Comments•Comments are used to give descriptions

to sections of code so that other programmers can understand what is being done.

•A comment is activated by putting to forward slashes then typing the comment.▫Eg. Readln (x); //get value for xReadln (y); //get value for yA = x * y; //multiply x and yWriteln (a); //write value of a on screen

Page 23: Programming, an introduction to Pascal The Basics of Program writing.

Practice Questions

1. Write a pascal program to read two numbers, find the product. The program should display the two numbers entered and the product.

2. Write a pascal program to read the costs of 5 grocery items. Find the sum of the items, display it. Multiply the sum by 15% to get the VAT and add the VAT to the sum of the items and display the total.

Page 24: Programming, an introduction to Pascal The Basics of Program writing.

Practice Questions

•Write a program to get the scores of 11 players on a cricket team and find the average and display it.

•Write a program to get the dimensions of a square and a rectangle and find the area of the combined area of the two.

Page 25: Programming, an introduction to Pascal The Basics of Program writing.

Selection Control StructureIF <condition true> THEN<perform action>ELSE<perform action>

IF age > 18 THENYou can get your drivers permitELSE You cannot legally drive

Page 26: Programming, an introduction to Pascal The Basics of Program writing.

Selection Pseudocode Example

get xget ysum = x + yif sum < 20 thenprint “ the score is small”else print “the score is large”

Page 27: Programming, an introduction to Pascal The Basics of Program writing.

Selection Pascal Examplebegin

writeln('Please enter your mark');readln(a);if a>50 thenwriteln ('You have passed')elsewriteln('You have failed');readln;

end.

Page 28: Programming, an introduction to Pascal The Basics of Program writing.

Selection Practice Questions

1. Write a pascal program to read three test scores and find the average. If the average is greater than 50 display pass, else display fail.

2. Write a pascal program to read the price of two dresses. Display which dress costs more.

3. Write a pascal program to read the shoe size of a student. If their shoe size is less than 7 print your foot is small else print your foot is big.

Page 29: Programming, an introduction to Pascal The Basics of Program writing.

Practice questions 2

•Write a pascal program to read three numbers a, b and c.Find the sum of a and b and the product of b and c. Display both the product and sum.If the sum is greater than the product display “The sum is bigger”. If not then display “the product is bigger”

Page 30: Programming, an introduction to Pascal The Basics of Program writing.

Types of Errors

•Syntax Error•Logic Error•Run Time Error

Page 31: Programming, an introduction to Pascal The Basics of Program writing.

Syntax errors

•They happen when the programing language used is not formatted according to the set standard.

•Eg 1.▫Readline (a) is a syntax error it should be:▫Readln (a)

•Eg 2.▫Writeln (‘Hello World’) is a syntax error it

shouldbe:▫Writeln (‘Hello World’);

Page 32: Programming, an introduction to Pascal The Basics of Program writing.

Logic Errors

•These occur when the calculations do make sense mathematically or logically.

•Eg1.▫C := a + b /2 should be▫C := (a+b) / 2

•Eg2.▫Avg := (a + b + c +d ) / 2 should be▫Avg := (a + b + c + d) / 4

Page 33: Programming, an introduction to Pascal The Basics of Program writing.

Runtime Errors

•These cause the program to crash or freeze while running preventing it from giving an output usually caused by bad calculations.

•Eg1.▫A = C / 0▫It is a runtime error because you cannot

divide by zero

Page 34: Programming, an introduction to Pascal The Basics of Program writing.

Selection Practice Questions

•Write a pascal program to read three test scores and find the average. If the average is greater than 50 display pass, else display fail.

•Write a pascal program to read the price of two dresses. Display which dress costs more.

•Write a pascal program to read the shoe size of a student. If their shoe size is less than 7 print your foot is small else print your foot is big.


Recommended