Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pascal Programming Strings, Arithmetic operators and output formatting National Certificate – Unit 4 Carl Smith.

Similar presentations


Presentation on theme: "Pascal Programming Strings, Arithmetic operators and output formatting National Certificate – Unit 4 Carl Smith."— Presentation transcript:

1 Pascal Programming Strings, Arithmetic operators and output formatting National Certificate – Unit 4 Carl Smith

2 Basic Terminology Alphabetic a b c d e f g h i j k l m n o p q r s t u v w x y z Digits 0 1 2 3 4 5 6 7 8 9 Special Characters (Symbols) + - / * ( ),. $ £ # = ‘ @ ~ { } ^ & ! Alpha-Numeric – any character typed from the keyboard

3 What is a variable? A variable is an individual storage space for data during the execution of your program (a reserved part of computer memory – “RAM”) When the program ends the variable’s contents are cleared Therefore, if we need keep the information we need to save it to a permanent store

4 Variable Data Type Definition In Pascal every variable used must be declared with a data type using VAR After declaration, to make a variable take a numeric value we use:- E.g. number_value := 327; Strings or characters are delimited with the ‘’ marks in PASCAL E.g. string_value := ‘fred’;

5 Operators +add - subtract * multiply /DIVdivide =equals (compare) :=equals (assign) <> not equal > greater than < less than >= greater than or equal to <= less than or equal to

6 Pascal functions Pascal has many in-built functions to help the programmer Functions are sub-programs that “return” single values back to the calling program e.g. chr(x) – returns ASCII char of value x The programmer can also write specialised “user-defined” functions

7 Example String Functions Length - returns the length of a string writeln (‘length of the string ‘,length(fred)); Insert - inserts a string into a string at a fixed point insert(‘added’, fred, 5); inserts the word “added” starting at 5th char of string fred

8 Example Math functions MOD – returns the Modulus of two numbers:- Remainder = 22 MOD 7; Remainder would = 1 SQR – Returns the square of a number or calculation Squared=sqr(4); squared would = 16

9 Arithmetic priority Expression or Operation Priority () *, / +, - Evaluate from inside out Evaluate from left to right

10 Real v Integer ‘Real’ variables are used to store numbers to ‘x’ decimal places Integers are whole numbers Integers use less memory to store than real numbers ‘DIV’ is used for division of integer values and ‘/’ is used for real values

11 Read,Readln,Write,Writeln Read and Readln are used to get input, normally from a keyboard but also from an external disk file. Write and Writeln are used to output data to a device – normally the screen but also a printer or disk file. The ‘LN’ part of the command appends a CRLF to input or output

12 Formatting using writeln() Output from your program can be formatted using a modified ‘writeln’ Each character or space is counted as 1 column on an 80 column screen/printer Writeln(‘Carl’:6); --Carl ‘-’=space Writeln(‘Carl’:10); ------Carl Writeln(52.7:12:2); -------52.70 Writeln(1006:9); -----1006 Writeln(100.367:12:2); ------100.37 The last example shows how writeln rounds up or down

13 More on Writeln; More than one value can be output with a single statement e.g. Writeln(‘Carl’,’Smith’:10,’IT’:6); Produces:- Carl-----Smith----IT Each value in the WRITELN statement is separated by a comma

14 Colour and positioning Turbo Pascal contains some functions to add colour to your text screens Gotoxy(1,15); (from top left corner) TextColor(Yellow); TextColor(White); TextBackground(Blue); TextColor & Textbackground also takes numeric input from 0 (black) to 15 (white) NormVideo; - reset all colours to normal

15 Printed output To send output to a printer you must include the following statement:- Uses Printer,Crt; {Use screen and printer as output devices} Then use in your code:- Writeln(lst, ‘Carl’:10); The ‘lst’ is short for list

16 Summary Basic terminology Variables Arithmetic operators and functions Real and Integer numeric values Formatting your output Using read, write, readln & writeln Colour and Positioning


Download ppt "Pascal Programming Strings, Arithmetic operators and output formatting National Certificate – Unit 4 Carl Smith."

Similar presentations


Ads by Google