Introduction to Variables |
|
|
Variable Declaration |
A variable is an area of computer memory used to store data. Before using a variable, you must first let the compiler know. Letting the compiler know about a variable is referred to as “Declaring” the variable. The compiler needs two pieces of information concerning each variable: a name for the variable and the amount of memory space the variable needs. var VariableName : DataType;
A variable is declared outside of (and before) the begin and end block in which the variable would be used. The
var keyword is required; it lets the computer know that you are declaring a variable. The VariableName represents the name of the variable and must abide to certain rules. var OneName : ADataType; var AnotherVariablee : ItsDataType; var OneMoreVariableName : AnotherDataType; On the other hand, if you are planning to use various variables of the same type, you can declare more than one after the var keyword but before the colon. The variables must be separated with a semi-colon. After the colon, specify the data type that those variables are using. Therefore, the syntax to use would be: var Variable1, Variable2 : DataType; or var Variable1, Variable2, Variable_n : DataType; Instead of typing the var keyword for each declaration, Pascal allows you to create a declaration section. Such a section starts with the var keyword followed by the desired declarations. By convention, the var stands on its line and the formal declarations follow on the subsequent line. By tradition also, the declarations are indented by 4 characters. The declarations above can be resumed as: var OneName : ADataType; AnotherVariable : ItsDataType; OneMoreVariableName : AnotherDataType; The declaration can also be done as follows: var AVariable : ADataType; Variable1, Variable2 : DataType; Variable3 : ItsDataType Variable4, Variable5, Variablen : DataType; |
User Input: The read and readln Procedures |
Besides the write and writeln procedures, Pascal is equipped with two procedures to perform opposite assignments. While the write and writeln procedures are used to display something on the screen, Pascal uses the read and the readln procedures to get values from the outside. This is usually done by the user typing values using the keyboard. The read procedure requests a value from the user and stores it in the computer memory. The syntax of the read procedure is: read(VariableName) The readln procedure also requests a value from the user but sends the cursor to the next line. Its syntax is: readln(VariableName) So far, we have learned to use the writeln procedures to display things on the screen. Besides the strings we were displaying, you can pass the name of a variable. For example, if you have a variable called HourlySalary ant want to display its value on the screen, you can type its name between the parentheses of the write or writeln procedures. The compiler would take care of the rest. This is done as follows: write(HourlySalary); If you wish to append a new line after displaying the variable name, you can use the writeln procedure instead of write. |
|
||
Previous | Copyright © 2004 FunctionX | Next |
|