




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
The Pascal programming language was created by Niklaus Wirth in 1970. ... TITLE is the name the programmer gives to the Pascal program being written.
Typology: Study notes
1 / 154
This page cannot be seen from the preview
Don't miss anything!
The Pascal programming language was created by Niklaus Wirth in 1970. It was named after Blaise Pascal, a famous French Mathematician. The Pascal language provides a teaching language that highlights concepts common to all computer languages standardizes the language in such a way that it makes programs easy to write Strict rules make it difficult for the programmer to write bad code! A program is a sequence of instructions which inform a computer of a required task. BASIC FORMAT OF EVERY PASCAL PROGRAM Every Pascal program has the same essential format, which is illustrated below, program TITLE ( input, output ); begin program statements; program statement end. program is the first word of all Pascal programs. It is a keyword (Keywords are reserved, ie, you cannot use keywords to describe variables). Keywords are used to implement the language. TITLE is the name the programmer gives to the Pascal program being written. It is an identifier. Identifiers begin with a letter, then followed by any digit, letter or the underscore character ( _ ). Identifiers are used to give names to user defined variables and methods used to perform operations. (input, output) states what the program will do, ie, input and/or output data. Data is inputted from the keyboard, and outputted to the console screen. begin defines the starting point of the program, and provides a means of grouping statements together (i.e. all statements between a begin and end are considered part of the same group or block). Program statements are commands or instructions to the computer which perform various tasks. end. This must always be the final statement of a Pascal program.
Next we will type begin and end. We are going to type the main body of the program between these 2 keywords. Remember to put the full stop after the end. program Hello; begin end. The Write command prints words on the screen. program Hello; begin Write('Hello world'); end. You will see that the "Hello world" is between single quotes. This is because it is what is called a string. All strings must be like this. The semi-colon at the end of the line is a statement separator. You must always remember to put it at the end of the line. MORE COMMANDS Displaying information/data on the Screen Writeln is just like Write except that it moves the cursor onto the next line after it has printed the words. Here is a program that will print "Hello" and then "world" on the next line: program Hello; begin Writeln('Hello'); Write('world'); Readln; end. If you want to skip a line then just use Writeln by itself without any brackets. A simple Pascal Program Write a program to print the words 'Hello. How are you?' on the console screen. program MYFIRST (output); begin writeln('Hello. How are you?') end. Sample Program Output _Hello. How are you? __
The above shows both the program, and its sample output which is printed as a result of running the program. The keyword writeln writes text to the console screen. The text to be displayed is written inside single quotes. After printing the text inside the single quotes, the cursor is positioned to the beginning of the next line. To print a single quote as part of the text, then use two quotes, eg, program TWOQUOTES (output); begin writeln('Hello there. I''m fine.') end. Sample program output is; Hello there. I'm fine. _ Note the underscore character represents the position of the cursor write versus writeln The write statement leaves the cursor at the end of the current output, rather than going to a new line. By replacing the above program with a write statement, the result is, program TWOQUOTES (output); begin write('Hello there. I''m fine.') end. Sample program output is; Hello there. I'm fine._ Note the underscore character represents the position of the cursor Exercise 1.1: Write a program to print the following words on the console screen. Hello. How are you? I'm just fine. The program can be implemented with writeln statements for each of the lines which need to be printed. The text is enclosed in single quotes. The sample program below illustrates how this is done. To display the single quote, two single quotes are used. This is due to Pascal using a single quote to begin and end text strings, when two are encountered one after the other, Pascal interprets this as a literal single quote.
ClrScr; Write('Hello world'); Readln; end. Comments Comments are things that are used to explain what a program does. Comments are inserted into Pascal programs by enclosing the comment within { and } braces. Comments are ignored by the computer, but are helpful to explain how the program works to other programmers or people who use the source code. You should always have a comment at the top of your program to say what it does as well as comments for any code that is difficult to understand. Here is an example of how to comment the program we just made: {This program will clear the screen, print "Hello world" and wait for the user to press enter.} program Hello; uses crt; begin ClrScr; {Clears the screen} Write('Hello world'); {Prints "Hello world"} Readln; {Waits for the user to press enter} end. program DEMOPROG (output); begin write('Hello there.'); {the write statement does not set the cursor to the beginning of the next line. } writeln('This is getting boring.') { This is printed on the same line as Hello there, but now the cursor moves to the beginning of the next line, because this time we used writeln instead of write } end. Sample Program Output Hello there. This is getting boring. Indentation You will notice that I have put 3 spaces in front of some of the commands. This is called indentation and it is used to make a program easier to read. A lot of beginners do not understand the reason for indentation and don't use it but when we start making longer, more complex programs, you will understand.
To change the color of the text printed on the screen we use the TextColor command. program Colors; uses crt; begin TextColor(Red); Writeln('Hello'); TextColor(White); Writeln('world'); end. The TextBackground command changes the color of the background of text. If you want to change the whole screen to a certain color then you must use ClrScr. program Colors; uses crt; begin TextBackground(Red); Writeln('Hello'); TextColor(White); ClrScr; end. SCREEN COORDINATES You can put the cursor anywhere on the screen using the GoToXY command. In DOS, the screen is 80 characters wide and 25 characters high. The height and width varies on other platforms. You may remember graphs from Maths which have a X and a Y axis. Screen coordinates work in a similar way. Here is an example of how to move the cursor to the 10th column in the 5th row. program Coordinates; uses crt; begin GoToXY(10,5); Writeln('Hello'); end.
Variables are names given to blocks of the computer's memory. The names are used to store values in these blocks of memory. Variables can hold values which are either numbers, strings or Boolean. We already know what numbers are. Strings are made up of letters. Boolean variables can have one of two values, either True or False. PASCAL VARIABLES AND DATA TYPES Variables store values and information. They allow programs to perform calculations and store data for later retrieval. Variables store numbers, names, text messages, etc. Pascal supports FOUR standard variable types, which are integer char boolean real integer Integer variables store whole numbers, ie, no decimal places. Examples of integer variables are, 34 6458 - 90 0 1112 char Character variables hold any valid character which is typed from the keyboard, ie digits, letters, punctuation, special symbols etc. Examples of characters are, XYZ 0ABC SAM_SAID.GET;LOST [ ] { } = + \ | % & ( ) * $ boolean Boolean variables, also called logical variables, can only have one of two possible states, true or false. real Real variables are positive or negative numbers which include decimal places. Examples are, 34.265 - 3.55 0.0 35.997E+ Here, the symbol E stands for 'times 10 to the power of'
Types integer, char and boolean are called ORDINAL types. This is because they have a limited, specified range of values. VARIABLE NAMES Variable names are a maximum of 32 alphanumeric characters. Some Pascal versions only recognize the first eight characters. The first letter of the data name must be ALPHABETIC (ie A to Z ). Lowercase characters ( a to z ) are treated as uppercase. Examples of variable names are, RATE_OF_PAY HOURS_WORKED B X y Home_score Give variables meaningful names, which will help to make the program easier to read and follow. This simplifies the task of error correction. Assigning values to variables Having declared a variable, you often want to make it equal to some value. In Pascal, the special operator := provides a means of assigning a value to a variable. The following portion of code, which appeared earlier, illustrates this. var number1, number2, number3 : integer; begin number1 := 43; { make number1 equal to 43 decimal } number2 := 34; { make number2 equal to 34 decimal } number3 := number1 + number2; { number3 equals 77 } When assigning values to char variables, only one character is assigned, and it is enclosed inside single quotes, eg, var letter : char; begin letter := 'W'; { this is correct } letter := 'WXY'; { this is wrong, only one character allowed } When assigning values to real variables, if the value is less than one, use a leading zero, eg, var money : real; begin money := 0.34; { this is correct } money := .34; { this is wrong, must use leading zero } money := 34.5; { this is correct }
Some examples of variable declarations program VARIABLESINTRO2 (output); var number1: integer; letter : char; money : real; begin number1 := 34; letter := 'Z'; money := 32.345; writeln( 'number1 is ', number1 ); writeln( 'letter is ', letter ); writeln( 'money is ', money ) end. Sample Program Output _number1 is 34 letter is Z money is 32. __ SELF TEST 3. Are the following valid variable declarations? var day, month : integer; time : real; var time : real; day : integer; month: integer; Answer: They are both identical and also valid! Classify each of the following according to the four basic data types. 34.276 ____________ - 37 __________________ H ____________ < __________________ dd ____________ 5.09E+27 _______________ 0 ____________ 0.0 __________________ Answer: 34.276 Real - 37 Integer
H Character < Character dd Character 5.09E+27 Real 0 Integer 0.0 Real Calculations with variables Variables can be used in calculations. For example you could assign the value to a variable and then add the number 1 to it. Here is a table of the operators that can be used:
number1 := 10; number2 := 20; result := number1 + number2; writeln(number1, ' plus ', number2, ' is ', result ) end. Sample Program Output 10 plus 20 is 30 Subtraction Example program Subtract (output); var number1, number2, result : integer; begin number1 := 15; number2 := 2; result := number1 - number2; writeln(number1, ' minus ', number2, ' is ', result ) end. Sample Program Output 15 minus 2 is 13 Multiplication Example program Multiply (output); var number1, number2, result : integer; begin number1 := 10; number2 := 20; result := number1 * number2; writeln(number1, ' multiplied by ', number2, ' is ', result ) end. Sample Program Output 10 multiplied by 20 is 200 Division Example program Divide (output); var number1, number2, result : integer; begin number1 := 20; number2 := 10; result := number1 / number2; writeln(number1, ' divided by ', number2, ' is ', result ) end.
Sample Program Output 20 divided by 10 is 2 SELF TEST 3. The following program contains a few errors. Identify each error (there are seven), and show the correct version on the right. progam TEST (output) var number1, number2; integer; begin; number1 = 24; number2 := number1 * 4; writeln('Help ) end Solution: prog r am TEST (output) ; var number1, number2 : integer; begin number1 : = 24; number2 := number1 * 4; writeln('Help ' ) end. Displaying the value or contents of variables The write or writeln statement displays the value of variables on the console screen. To print text, enclose inside single quotes. To display the value of a variable, do NOT enclose using single quotes, eg, the following program displays the content of each of the variables declared. program DISPLAYVARIABLES (output); var number1 : integer; letter : char; money : real; begin number1 := 23; letter := 'W'; money := 23.73; writeln('number1 = ', number1 ); writeln('letter = ', letter ); writeln('money = ', money ) end. The display output from the above program will be,
You are to write a program which calculates and prints on the screen, the time required to travel 3000 miles at a speed of 500 mph. program PROG1 (output); var Time, Distance, Speed : real; begin Distance := 3000; Speed := 500; Time := Distance / Speed; writeln('It takes ',Time,' hours.') end. PROGRAM TWO Write a program to calculate the gross pay for a worker named FRED given that FRED worked 40 hours at $2.90 per hour. program PROG2 (output); var grosspay, hoursworked, hourlyrate : real; begin hoursworked := 40; hourlyrate := 2.90; grosspay := hoursworked * hourlyrate; writeln('FRED''s gross pay is $', grosspay ) end. GETTING INFORMATION/DATA FROM THE KEYBOARD INTO A PROGRAM It is convenient to accept data whilst a program is running. The read and readln statements allow you to read values and characters from the keyboard, placing them directly into specified variables. The program that follows reads two numbers from the keyboard, assigns them to the specified variables, then prints them to the console screen. program READDEMO (input, output); var numb1, numb2 : integer; begin writeln('Please enter two numbers separated by a space'); read( numb1 ); read( numb2 ); writeln; writeln('Numb1 is ', numb1 , ' Numb2 is ', numb2 ) end.
When run, the program will display the message, Please enter two numbers separated by a space waiting for you to enter in the two numbers. If you typed the two numbers, then pressed the return key, e.g., 237 64