TURBO PASCAL PROGRAMMING

Pascal language named after the Mathematician Blaise Pascal is one of the Procedural language we are going to learn in a programming lesson.
To be practical Pascal programming course we need to have a Pascal compiler for practice purpose. I prefer using DEVPAS and if you don't have it then CLICK HERE for free Download. 

START
Pascal Program is divided into two major parts. 
Program Header which is one line long having the statement 'Program Programname;', Program name can be any name without first Character being a number, spacing between characters or two words and should not be a reserved word(words that have predefined meaning) and  

Block which comprises of Declaration part(part of the program holding all the identifiers(name given to some program element such as variables, constants etc) and there corresponding data types that are going to be used in a program) and Statement part(having the statement to be executed during Program execution.)
Statements are enclosed between the word 'BEGIN' and 'END.'(the word AND has a period '.' at the end),
and each statement is terminated with Semicolon ';' lets begin programming.(Enjoy).

Program with output only.
The statement 'write(); or writeln();' is used when output is needed to the console.
write(); places the cursor on the same line hence the next output will follow the previous one but on the same line.
writeln(); places the cursor on the next line hence the next output will follow the previous one but on the next line.
write('  '); in addition to the write(); statement above the write('  '); statement will display exactly the same words as written in the apostrophes which are in blackets. e.g. write('Hallow'); will display Hallow to the console. but taking an example the value of x=4 and you say write(x); will display 4. same applies to writeln('  '); statement except that the cursor will be moved to the next line.

Note:comments in Pascal are written in the curl Brakets {}, comments are never displayed in the console.
Program that explains the above lesson, write the following on your DevPas Compiler.

Program with Input and Output from the keyboard.
In order to input something from the keyboard then readln(); or read(); statement is used, readln(); and read(); statement work similar except that we don have read('  '); or readln(' ');

Variables.
Is an identifier whose value is allowed to change during program execution. variable is always declared in the declaration part of the program, when the variable is declared the compiler associates it with a particular memory location so the value of the variable will always be the value stored in that memory.
Variable declaration syntax.
var
variablename:datatype;{data type can be integer, real, character etc. you will learn this later}
Program that explains the above lesson, write the following in your Devpas compiler.











Data types
Data types are divided into three categories named Simple data types, Structured data types and Pointers.
Simple data types
These are single item that are associated with a single Identifier on a one-to-one basis. Divided into two groups.
Standard data types which are Integers, Real numbers, Characters, Strings and Boolean.
Integers.
Number contain neither a decimal nor an exponent.
Integer syntax
Declaration
var 
identifier:integer;{identifiers refers to any name you wish to give to your integer e.g, a,b,c:integer;}
Adding Integers
c:=a+b;{where a,b and c are declared identifiers}
Subtracting integers
c:=a+b;
Multiplying Integers
c:=a*b;
Dividing Integers
c:=a DIV b;

Real numbers
Declaration
var
a,b,c:real;
Addition
c:=a+b;
Subtraction
c:=a-b;
Multiplication
c:=a*b;
Division
c:=a/b;

String-Is a collection of characters, string is normally enclosed within the apostrophes.
Declaration
var
name:string;{check a simple program below explaining how a string is, don't forget a program header}
begin
name:='sourcecode';
writeln(name);
readln;
end.
Program will display sourcecode as the output on a console.

String addition
name3:=name1+name2;
if name1:='source'; and name2:='code'; then name3 will be sourcecode.

String Functions.
concat function- this joins two or more strings{an alternative of  '+' sing}
syntax, concat(s1,s2,s3...sn); where 's' is a string
e.g
name1:='source'; name2:='code';
name3:=concat(name1,name2);{name3 will be sourcecode}

copy function-return the copied substring of a string.
syntax, copy(sourcestring,starting position, number of characters);
e.g
name1:='sourcecode';
name3:=copy(name1,7,4); {name3 will be code}

Pos function-return the starting position of the substring being searched.
syntax, pos(search model,source string);
e.g.
name1:='sourcecode';
position:=pos('code',name1); {position will be =7}

length function-returns the total number of characters in a given string
syntax, length(stringname);
e.g
name1:='code';
l:=length(name1);{l will b =4}

Characters-hold a single byte in memory.
Declaration
var
letter:char;

Boolean-has two values only, either TRUE or FALSE, and has to be initialized always.
Declaration
var
decide:boolean;
initializations can either be
decide:=true; or decide:=false; depending on your need.

Selection Structure.
Selection structure make possible the selection of one of a number of alternative actions, depending on the value of boolean expression.
IF..THEN structure- allows a group of statements to be executed if a given logical condition ha a value TRUE.
Has the form, IF boolean expression is true THEN statement

IF..THEN..ELSE structure-used to select one of two or more alternatives.
Has the form, IF condition1 THEN statement1
           ELSE IF condition2 THEN statement2
           ELSE IF condition3 THEN statement3
           ELSE statementn;

CASE structure-allows particular group of statements to be executed from several available group.
Has the form, CASE selector OF
                      Value1:Statement1;
                      Value2:Statement2;
                      Valuen:Statementn
                      ELSE action for none of the above
                      End;
 The following program explains much on the lesson above, please read t carefully and run it in your compiler








No comments:

Post a Comment

Followers