This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: Chapter 4 BASIC CONCEPTS OF PROGRAMMING In order to read numerical information into the computer, to perform arithmetic calculations on the numbers you read in, and to output the answers we must look at the concepts of: variable , data type , declaration , and assignment statement . We will, as well, introduce the statement for input and the idea of program tracing . VARIABLES We have said that a computer has a memory and that in the memory there are locations where information can be stored. Each location has its own unique address. In a high-level language like Fortran we do not ever refer to an actual machine address. Instead we use a name to identify a particular location. It is like referring to a house by the name of the owner rather than by its street address. We use the word variable to stand for the memory location. It is named by an identifier . The identifier for a variable must begin with a letter and may be followed by more letters, digits and certain special symbols such as the underscore ( _ ). If you think of the variable as the memory location and its name as the identifier then you will realize that the value of the variable will be the actual information that is stored in the memory location. Locations are arranged to hold only one type of information or data. We speak of the data type of a variable. A variable may hold integers, in which case we say it is an integer variable . It could also be a real variable or a string variable . If a variable is an integer variable its value can be any integer. The value may be changed from time to time in the program but its type can never change; once an integer variable, always an integer variable. Examples of variable identifiers are 2 | BASIC CONCEPTS OF COMPUTER PROGRAMMING factor , tax , total , mark Identifiers for variables can be up to 49 characters long. It is very important to choose identifiers that relate to the kind of information that is stored in the corresponding locations. Well chosen identifiers make a program easier to understand. DECLARATIONS We must make the words we want to use as variable identifiers known to the compiler and associate them with memory locations suitable for the particular data type they will hold. This is accomplished by means of declarations that are placed at the beginning of the program. We will not, at the moment, show how character variables can be declared but look only at integer and real variables. To declare that sum is to be an integer variable we write integer sum The identifier is placed after the keyword integer . This establishes sum as having the type integer . To declare width to be a real variable use real width If a number of integer variables are required they can all be listed separated by commas, for example integer sum, mark, number Putting declarations in a program is like phoning ahead for hotel reservations; when you need it, the space is there with the right name on it....
View Full Document
- Winter '12
- Computer Programming, Madrid Metro, Stockholm Metro