The Fundamentals of Computer ProgrammingIn this lesson, you will
What is a computer program?
|
| ||
| Most likely you will have a little difficulty in driving there using these instructions. Natural languages such as English are adequate when you want to talk with another person. There is a "background of obviousness", a shared common experience that forms the basis for human communication. This does not exist for human-computer communication. Natural languages are far too imprecise when you want to instruct a machine. For that, you need to use a programming language such as Perl. The instructions in a programming language will cause the computer to interpret them the same way every time. |
Creating your first Perl program | |
| For step-by-step illustration of how to create the following Perl program, click Instructions here. | |
|
Program 1.1. The hello.pl program.
| |||
|
The first two lines are comment lines and they are not executed. This means that the
computer will ignore these lines of text. They are there for the benefit of programmers.
The purpose of comments is to add documentation to a program.
The first line reminds the programmer what the program is about. The second line is a blank comment line which separates the header comment and the code body of the program. It just spreads out the lines so that they are easier for you, the programmer, to read. |
| ||
|
This program has only one line to be executed. That is the print instruction. The print
command must be followed by what is to be printed. In this case, it is the phrase
"Hello World!". The semicolon terminates the instruction.
Save the program on the diskette in A: drive and call it hello.pl where the file extension .pl implies that it is a Perl program. |
|
|
The above program displays a string literal " I wish today was " followed by a comma (,).
The comma means there is more to print. It is followed by the variable $weekday.
What is being printed is the value of the variable, not the variable name.
The last semicolon (;) terminates the print instruction.
If you would like to see a period printed after the sentence, you must change the print instruction to: print " I wish today was ", $weekday, "."; Put a comma (,) after the variable $weekday and add another string literal "." before terminating the instruction. |
|
| You may mix as many literals and variables as you like in one print instruction provided that you separate them with commas (,) and terminate the instruction with a semicolon (;). |
|
The PRINT command | |||||
We speak and write instructions to each other in sentences. In Perl, we use instructions that are very much like sentences, for example:
The above statement is a print instruction. It displays on the display screen as follows:
The print command displays the rest of the statement on the display screen, namely, what is enclosed in the pair of quotes. Note that the quotation marks enclosing the phrase are not displayed. The semicolon following the last quote is not displayed either. The semicolon is used to end the instruction. |
| ||||
|
The quotation marks and the semi-colon are examples of proper syntax that must be used as part of the instructions for the computer. |
|
Constants and variables | |||
|
In Perl, a string of characters enclosed in a pair of quotation marks is called a string constant. The word string is a short name for a string of characters. String constants keep their contents unchanged throughout the running of the entire program. |
| ||
|
We often give names to things whose value can change. Examples here are time, date, current month, and so on. These are called variables. A variable has a name and a value. As a programmer you will decide what name to give to a variable and what value to assign to it. You should select the variable name that describes the type of values it is going to have. If the value is about a distance in miles, call the variable $distance or $miles. If it is going to store quantity of goods sold, call the variable $quantity or $goodsSold, etc. All single value variable names start with the dollar sign ($). |
|
The Assignment command | ||
Perl uses the assignment command to assign a value to a variable:
| ||
|
The assignment command appears as an equal sign (=). The left side of the equal sign must be a variable. The right side of the equal sign can be either a constant or a variable. The above assignment statement causes the variable $weekday to be given the value "Friday". The following assignment instruction:
|
|
|
assigns the value of the variable $weekday to the variable $holiday. The variable on the left side of the equal sign has its former value replaced with the value of whatever is on the right side. There can only be one variable on the left side of the equal sign. The value at the right side of the equal sign is not changed. It is possible to have either a constant or a variable at the right side of the equal sign since its value remains unchanged. A variable retains its assigned value for the life of the program, or until it is assigned a new value. Now both variables $weekday and $holiday are string variables as they both contain a string value. For the above example, the variable $weekday has the value "Friday". Therefore, the variable: $holiday is now assigned the value "Friday". Another type of value is the numeric value. The following assignment:
|
|
|
assigns the variable $miles a numeric value of 50. The 50 is a numeric constant. A numeric constant does not require to be enclosed in a pair of quotes like the string constant. The variable $miles is called a numeric variable because it contains a numeric value.
The above assignment assigns the value of the variable $miles, which is 50, to the variable $speedLimit. |
|
|
A variable name must start with the dollar sign and be followed by an alphabetic letter. The rest of the name can be alphabetic (a-z), digits (0-9), or underscore (_). It can be as long as you want. Variable names are case sensitive. Miles, miles, and MILES are three different variables. |
|
Displaying more than one line using one print instruction | |||
|
Program 1.3. Use the text editor to compose the following program: The result of the above program displays the sentence in two lines:
| |||
|
The symbol backslash followed by a letter n (\n) represents the newline character. It serves the same function as the hard return code in WordPerfect. It causes the print instruction to start a new line of output on the display screen. Try to delete \n and run the program again. These two sentences will then be displayed on the same line. Program 1.3 also demonstrates that a print instruction can span more than one line, as long as the items to be displayed are separated by commas (,). The print instruction can be rewritten in the following fashion and still produces the same result as that from Program 1.3
|
|
|
Note also, all of the string literals contain a blank space on both ends. Take the blank off before the last kilometers, run the program, and check out the results. They become hard to read. It is a good practice always to put a blank between two items in a print instruction. The result is a better presented sentence. |
|
Variables can save typing | |||
|
Program 1.4. Use the text editor to compose the following program: The result of above program after execution is:
| |||
|
The advantages of using variables is that once you have a variable with a certain value, such as " wash our face, ", it can be used again and again in the program. In a small program, this advantage is less obvious. In a large program, variables can save programmers a lot of time and typing mistakes. The punctuation in the above example is not perfect. However, it serves to demonstrate how to reuse a variable. As a part of the exercises, try to find a way to improve the punctuation. |
|
Lession 1 Exercises | |
| |
| M. Mark, 2002.03.18 | | Introduction | Lesson 1 | Lesson 2 | Lesson 3 | Lesson 4 | Appendix | |