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 Low Level High Level
Machine language vs Assembly 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
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. Write out a sample machine code program.
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
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
History of Pascal Pascal, named in honor of the French mathematician and philosopher Blaise Pascal, was developed by Niklaus Wirth.
Pascal Format HEADER Program <program name>; const <constant values in the program>; var <variables used in the program> begin <the actual program code> end. CONSTANT DECLARATION VARIABLE DECLARATION PROGRAM BODY
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…….
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
THREE VARIABLES OF THE SAME TYPE CAN BE SEPARATED BY A COMMA Declaring Variables var gender: char; num1, num2, num3: integer; product: real; THREE VARIABLES OF THE SAME TYPE CAN BE SEPARATED BY A COMMA
Declaring Constants const pi := 3.142; VAT := 0.15; Gravity := 9.81;
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.
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.
Write commands Write(‘ hello world’); Writeln (‘hello world’); DISPLAYS ‘HELLO WORLD’ IN THE SAME LINE Hello World DISPLAYS ‘HELLO WORLD’ AND GOES TO THE NEXT LINE Hello World _
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 := 15 cost := 65 Count := count + 1
Simple Programs This program writes Hello World and then waits for the user to press enter to exit the program. Program HelloWorld; Begin Write(‘Hello World’); Readln; End. Explain Program line by line
Simple Programs This program writes ‘Hello’ in one line then goes to the other, then it prints ‘World’ and waits for the user to press enter. program Hello; begin Writeln('Hello'); Write('world'); Readln; end. Explain Program line by line
Simple Programs Program Addition; Var a:integer; Begin This declares ‘a’ as an integer. It then adds two numbers and assigns the result to ‘a’. Simple Programs Program Addition; Var a:integer; Begin Writeln('The sum of 6 and 4 is:'); a := 6 + 4; write(a); readln; End. Explain Program line by line
This declares ‘a’ as an integer. It then multiplies two numbers and assigns the result to ‘a’. Then displays ‘a’ Simple Programs Program 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); End. Explain Program line by line
This declares ‘a’ ‘b’ and ‘c’ as integers. Simple Programs This declares ‘a’ ‘b’ and ‘c’ as integers. It then adds ‘a’ to ‘b’ and assigns the result to ‘c’. Then displays ‘c’ Program 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; End. Explain Program line by line
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 x Readln (y); //get value for y A = x * y; //multiply x and y Writeln (a); //write value of a on screen
Practice Questions Write a pascal program to read two numbers, find the product. The program should display the two numbers entered and the product. 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.
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.
Selection Control Structure IF <condition true> THEN <perform action> ELSE IF age > 18 THEN You can get your drivers permit ELSE You cannot legally drive
Selection Pseudocode Example get x get y sum = x + y if sum < 20 then print “ the score is small” else print “the score is large”
Selection Pascal Example begin writeln('Please enter your mark'); readln(a); if a>50 then writeln ('You have passed') else writeln('You have failed'); readln; end.
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.
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”
Types of Errors Syntax Error Logic Error Run Time Error
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’);
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
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
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.