Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 18 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
18
Dung lượng
880,11 KB
Nội dung
Computer Studies Programming using Turbo Pascal Notes - 1 - The Pascal programming language was developed, around the 1970s, by Nicholas Wirth. It was developed as a first language for programming students and serves the purpose well. Pascal is one of the easiest languages to learn, and it encourages beginners to develop good programming skills. Easy to learn It is easy to learn because the language has a relatively small set of words, and these are close enough to ordinary English to be easy to understand and remember. Even before you start to learn the language, you should be able to read a Pascal program’s code and be able to make some sense of it. Example program first; begin write(‘Hello World’); end. The basic structure At the simplest, a program takes this shape: program title; begin statement; statement; statement; end. The important things to notice here are: Programs always start with the word program followed by the name. The name must be a single word – no spaces or punctuation – but can normally be of any length. The start of the active part of the program is marked by begin. The end of the active part is marked by end. program, begin and end are all reserved words, ones with a special meaning. They can be written in lower case or capitals. The full stop at the end is essential. A program can contain any number of statements. It starts here… This is the code of a Pascal program, and it is called first . write must put the text on the screen. … and it ends here Computer Studies Programming using Turbo Pascal Notes - 2 - A statement will usually be on a line by itself, but can be spread over several. Each statement is separated from the next by a semi-colon. This is normally written at the end of the line. To make th eprogram easier to read, lines may be indented. This is more useful in more complex programs, where different levels of indents can help to bring out the structure. Whether and how far you indent is entirely up to you. Let’s get it started… When using Turbo Pascal, you will working with two different types of screens, The text-editor: this is a blue screen where you will be typing your programs. The output screen: this is where you will see the results when you compile (run) your program – in other words, where a program works. Writing a simple program – the use of write and writeln statements Try this program to see how text, integers and decimal values are displayed on the screen. program writing; uses crt; begin clrscr; writeln(‘This is a text item.’,‘Here is another’); writeln(‘One more ’,‘and the last’); writeln(1,2,3,4,5); {integers or whole numbers} writeln(12345.678, 0.123); {number with decimal fraction} readln; end. To see the program work, click on RUN in the menu bar and click on RUN in the menu. Write down the results you see on the screen in the box below. Instead of writeln, type the command write. What happens to the output? Write down the results you see on the screen in the box below. Comments written inside {curly brackets} are ignored by the compiler – i.e. when the program is run. Computer Studies Programming using Turbo Pascal Notes - 3 - So what’s the difference between write and writeln? write ______________________________________________________________ _______________________________________________________________________ writeln ____________________________________________________________ _______________________________________________________________________ The clrscr command This command is used to clear the output screen from any characters and put the cursor in the top-left corner of the screen. Why not add a few colours? – the use of textcolor and textbackground The output screen can be very dull. It usually displays grey text and a black background. Type the following program and see and then write down what you think textcolor and textbackground are used for. program colourful; uses crt; begin clrscr; textbackground(14); textcolor(13); writeln(‘Testing colours’); readln; textcolor(12); writeln(‘Still testing colours’); readln; end. So what do the command textcolor and textbackground do? textcolor _________________________________________________________ _______________________________________________________________________ textbackground ___________________________________________________ _______________________________________________________________________ Computer Studies Programming using Turbo Pascal Notes - 4 - But why doesn’t my program work? Have you every asked this question yet? If you do not use the correct syntax (i.e. grammar) when typing a Pascal program, it simply won’t work. Your program must be error-free in order to work. When the compiler meets an error in a program, the cursor stops in the line after the error. An error message is displayed at the top of the screen. If you press F1 the error message is further explained for you. The error message The help window The following program has errors. Correct the errors and make the program work on a computer. program colourful text; uses crt; begin clrcsr; textcolour(green); writeln(‘I am programming); textcolor(12) writeln(Using Turbo Pascal’); readln; end In the box below write down the corrected version of the above program. _____________________________________________________________________________ _____________________________________________________________________________ _____________________________________________________________________________ _____________________________________________________________________________ _____________________________________________________________________________ _____________________________________________________________________________ _____________________________________________________________________________ _____________________________________________________________________________ _____________________________________________________________________________ _____________________________________________________________________________ _____________________________________________________________________________ Computer Studies Programming using Turbo Pascal Notes - 5 - The four basic arithmetic operations The four basic arithmetic operations that one can use in Pascal are : plus + minus - multiply * divide DIV, MOD, / You will now be looking at a few exercises to see how each operation can work. Addition, Subtraction and Multiplication Adding, subtracting and multiplying numbers in Pascal couldn’t be easier. Copy and run the following program on a computer to see how these arithmetic operations work. program arithmetic_operations; {adding, subtracting and multiplying} uses crt; begin clrscr; textcolor(green); writeln(‘Addition: 5 + 2 = ‘,5 + 2); writeln(‘Subtraction: 15 - 7 = ‘,15 - 7); writeln(‘Multiplication: 8 x 3 = ‘,8 * 3); readln; end. Write down the results that you see on the screen in the box below. Division Using Pascal, there are three ways in which you can divide two numbers. Try out each method. Division using ‘/’ program division; {division using /} uses crt; begin clrscr; writeln(‘Division: 17 / 3 = ‘,17/3); readln; end. Write down the output that you see on the screen in the box below. Computer Studies Programming using Turbo Pascal Notes - 6 - Counting the decimal places Look at the result from the previous program. Can you see too many zeros? How about we format the number to two decimal places instead? Go back to the previous program. Change the line to the one that is displayed here. writeln(‘Division: 17 / 3 = ‘,17/3:0:2); Now write down the output that you see on the screen in the box below. Division using ‘DIV’ and ‘MOD’ DIV and MOD are two other ways of performing division. But with a difference. Copy and run the following program on a computer. Look at the results – what do you think is the difference between DIV and MOD? program division; {division using DIV and MOD} uses crt; begin clrscr; writeln(‘Division: 17 DIV 3 = ‘,17 DIV 3); writeln(‘Division: 17 MOD 3 = ‘,17 MOD 3); readln; end. Write down the output that you see on the screen in the box below. So what’s the difference between DIV and MOD? DIV ________________________________________________ _______________________________________________________________________ MOD ________________________________________________________________ _______________________________________________________________________ So what have you been doin’…? Up till now you have been writing out and running simple programs on a computer using Pascal. Very straight-forward programs. They perform simple tasks that are set by YOU, the programmer. But how about a little This means that the number will be displayed to 2 decimal places. Computer Studies Programming using Turbo Pascal Notes - 7 - input from the user while the program is running? The next part of these notes will show you how to accept inputs while a program is running. Variables and data types If you want to handle any data while the program is running, you must set up variables. These are areas of memory, idenified by a name written into the program. When setting up a variable, you must say what type of data is to be stored there, as different types require different amounts of memory. Declaring a variable To accept input while a program is running, you have to declare a variable name and its datatype. A variable name: must be a single word; can contain letters and numbers; cannot start with a number. You cannot use a reserved word (words used by Pascal) as a variable name, e.g. begin. A datatype is used to specify the type of input. The following are the datatypes you will be using: integer A whole number, in the range -32,768 to +32,767 real Any number, with or without a decimal fraction char A single character, which can be a letter, symbol or digit – in fact, any character from the ASCII set. boolean These can only store the values ‘TRUE’ or ‘FALSE’. Boolean variables are generally used to store the results of logical tests. string Can be used to store a set of zero or more characters. INTEGER and REAL data types You’ve typed programs that add numbers – the only problem is that they only add the same two numbers! The following is a program that adds two numbers that the user can enter while the program is running. Copy and run the following program. Computer Studies Programming using Turbo Pascal Notes - 8 - program input_numbers; {adding numbers using variables} uses crt; {this is the part where you declare a variable} var num1, num2: integer; {an integer is a whole number} begin clrscr; writeln(‘Enter the FIRST number: ‘); readln(num1); writeln(‘Enter the SECOND number: ‘); readln(num2); writeln(‘Result after adding the numbers is ‘,num1 + num2); readln; end. Write down the output that you see on the screen in the box below. You have just written a program that adds any two numbers. Well done! Modify the program above so that it will output the subtraction, multiplication and division for the two numbers entered by the user. In the box below write down the new version of the above program. _____________________________________________________________________________ _____________________________________________________________________________ _____________________________________________________________________________ _____________________________________________________________________________ _____________________________________________________________________________ _____________________________________________________________________________ _____________________________________________________________________________ _____________________________________________________________________________ _____________________________________________________________________________ _____________________________________________________________________________ _____________________________________________________________________________ _____________________________________________________________________________ _____________________________________________________________________________ _____________________________________________________________________________ _____________________________________________________________________________ Declaring variables. Notice the use of var when declaring variables. Computer Studies Programming using Turbo Pascal Notes - 9 - The assignment statement There are two ways of storing variables: they can come in from the keyboard (the user’s input), or values can be assigned from within the program. You have seen how to input numbers and store them in variables when you typed and compiled the previous program (on page 8). Variable := value Values are assigned to variables with the operator “ := ” – a colon followed by an equals sign. The variable name sits to the left, and on the right is an actual value, another variable, a function, or an expression that produces a value. Here are some examples: number_1 := 99; number_2 := 103; answer := number_1 + number_2; total := total + next; name := ‘SAM’; Copy and run the following program. program values_variables; {assigning values to variables} uses crt; var a, b, c :integer; decimal :real; letter :char; YesNo :boolean; begin clrscr; a := 2; b := 3; c := a + b; decimal := 9.99; letter := ‘X’; YesNo := TRUE; writeln(‘a = ‘,a ,’b = ‘,b ,’c = ‘,c); writeln(‘Decimal holds ‘,decimal:0:2); writeln(‘The letter is ‘,letter); writeln(‘The value of YesNo is ‘,YesNo); readln; end. Write down the output that you see on the screen in the box below. Computer Studies Programming using Turbo Pascal Notes - 10 - Exercises Read the following problems and then try to solve them by writing a program. Once the programs work successfully on a computer, write them out on a foolscape and pass them on to your teacher. 1. Write a program where the user will be asked to input his/her name and the year of birth. The program will calculate how old the person is and then output the person’s name together with his/her age. Write down the output that you see on the screen in the box below. 2. Write a Fahrenheit to Centrigrade conversion program. The progra will take a Fahrenheit temperature as input and ouputs the corresponding temperature in Centigrade. Use the formula C = ( F – 32 ) x 5 / 9 where C is the temperature in Centirgrade and F is the temperature in Fahrenheit. Write down the output that you see on the screen in the box below. 3. Write a program that accepts three numbers as input, calculates their total and average, and outputs the results on screen. Write down the output that you see on the screen in the box below. [...]... in Pascal by the if-then statement if-then statement This is the basic syntax: if test then statement; … or where there are several statements: if test then begin statements; end; The test checks the value held by a variable if the condition proves true, then the program performs the following statement(s) If the condition does not prove true, the statements are ignored Conditional expressions The. .. is the purpose of the variable POS? 6 Make the necessary changes to the program so that it will also find the lowest mark Exercise Write a program (using the Pascal programming language) that allows a user to enter the names and marks for eight students The program should prompt the user to enter a name and a mark, check that the mark is within the range 0 . Computer Studies Programming using Turbo Pascal Notes - 1 - The Pascal programming language was developed, around the 1970s, by Nicholas Wirth. It was developed as a first language for programming. When the compiler meets an error in a program, the cursor stops in the line after the error. An error message is displayed at the top of the screen. If you press F1 the error message is further. Write down the output that you see on the screen in the box below. Computer Studies Programming using Turbo Pascal Notes - 6 - Counting the decimal places Look at the result from the previous