Code_ The Hidden Language of Computer Hardware and Software - Charles Petzold [152]
If the particular ALGOL compiler we happen to be using supports the IEEE floating-point standard, the three variables in the program each require 4 bytes of storage (for single-precision numbers) or 8 bytes of storage (for double-precision numbers).
The next three statements are assignment statements. In ALGOL, you can always recognize an assignment statement because it's designated by a colon followed by the equal sign. (In most computer languages, only the equal sign is required for an assignment statement.) On the left is a variable. On the right is an expression. The variable is set to the number that results from an evaluation of the expression. The first two assignment statements indicate that both a and b are assigned particular values. The third assignment statement in the program assigns the variable c to the product of variables a and b.
These days, the familiar x multiplication symbol is usually not allowed in programming languages because it's not part of the ASCII and EBCDIC character sets. Most programming languages use an asterisk to indicate multiplication. While ALGOL uses a slash (/) for division, the language also includes a division sign (÷) for integer division, which indicates how many times the divisor is contained in the dividend. ALGOL also defines an arrow (↑), another non-ASCII character, for exponentiation.
Finally the print statement displays everything. It combines text and variables separated by commas. Displaying ASCII characters is probably not a major chore for the print statement, but here the function must also convert the floating-point numbers to ASCII:
The product of 535.43 and 289.771 is 155152.08653
The program then terminates and returns control to the operating system.
If you want to multiply a couple of other numbers, you'll need to edit the program, change the numbers, recompile it, and run it again. You can avoid this frequent recompiling by taking advantage of another built-in function named read.
begin
real a, b, c;
print ('Enter the first number: ');
read (a);
print ('Enter the second number: ');
read (b);
c := a x b;
print ('The product of ', a, ' and ', b, ' is ', c);
end
The read statements read ASCII characters that you type at the keyboard and convert them to floating-point values.
A very important construction in high-level languages is the loop. The loop allows you to write a program that does the same thing for many different values of a variable. Suppose you want to write a program that calculates the cubes of 3, 5, 7, and 9. You can do it like this:
begin
real a, b;
for a := 3, 5, 7, 9 do
begin
b := a x a x a;
print ('The cube of ', a, ' is ', b);
end
end
The for statement sets the variable a first to the value 3 and then executes the statement that follows the do keyword. If there's more than one statement that must be executed (as is the case here), the multiple statements must be included between begin and end statements. These two keywords define a block of statements. The for statement then executes those same statements for the variable a set to 5, 7, and 9.
Here's another version of the for statement. This one calculates the cubes of odd numbers from 3 through 99:
begin
real a, b;
for a := 3 step 2 until 99 do
begin
b := a x a x a;
print ('The cube of ', a, ' is ', b);
end
end
The for statement initially sets the variable a to 3 and executes the block following the for statement. Then a is increased by the number following the step keyword, which is 2. The new value of a, which is 5, is used to execute the block. The variable a will continue to be increased by 2. When it exceeds 99, the for loop is completed.
Programming languages usually have a very strict syntax. In Algol 60, for example, the keyword for can be followed by only one type of thing—a variable name. In English, however, the word for can be followed by all sorts of different words, such as example in the previous