Code_ The Hidden Language of Computer Hardware and Software - Charles Petzold [153]
Another important feature of most programming languages is the conditional. This is a statement that causes another statement to execute only if a particular condition is true. Here's an example that uses the ALGOL built-in function sqrt, which calculates a square root. The sqrt function doesn't work for negative numbers, so this program avoids that occurrence:
begin
real a, b;
print ('Enter a number: ');
read (a);
if a < 0 then
print ('Sorry, the number was negative.');
else
begin
b = sqrt(a);
print ('The square root of ', a, ' is ', b);
end
end
The left angle bracket (<) is a less than sign. If the user of this program types in a number that is less than 0, the first print statement is executed. If not—that is, if the number is greater than or equal to 0—the block containing the other print statement is executed.
So far, the variables shown in the programs in this chapter store only one value each. Often it's convenient for the same variable to store many values. This is known as an array. An array is declared in an ALGOL program like this:
real array a[1:100];
In this case, we've indicated that we want to use this variable to store 100 different floating-point values, called elements of the array. The first one is referenced by a[1], the second by a[2], and the last by a[100]. The number in brackets is called the index of the array.
This program calculates all the square roots of 1 through 100 and stores them in an array. Then it prints them out:
begin
real array a[1:100];
integer i;
for i := 1 step 1 until 100 do
a[i] := sqrt(i);
for i := 1 step 1 until 100 do
print ('The square root of ', i, ' is ', a[i]);
end
This program also shows an integer variable named i (which is a traditional name for an integer variable because it's the first letter of the word). In the first for loop, each element of the array is assigned the square root of its index. In the second for loop, these are printed out.
In addition to real and integer, variables can also be declared as Boolean. (Remember George Boole from Chapter 10?) A Boolean variable has only two possible values, which are true and false. I make use of a Boolean array (and almost every other feature we've learned about so far) in the final program of this chapter—a program that implements a famous algorithm for finding prime numbers called the Sieve of Eratosthenes. Eratosthenes (circa 276–196 BCE) was the librarian of the legendary library at Alexandria and is best remembered today for accurately calculating the circumference of the earth.
Prime numbers are those whole numbers that are divisible without a remainder only by themselves and 1. The first prime number is 2 (the only even prime number), and the primes continue with 3, 5, 7, 11, 13, 17, and so forth.
Eratosthenes' technique begins with a list of the positive whole numbers beginning with 2. Because 2 is a prime number, cross out all the numbers that are multiples of 2. (That's all the even numbers except 2.) Those numbers aren't primes. Because 3 is a prime number, cross out all the numbers that are multiples of 3. We already know 4 isn't a prime number because it has been crossed out. The next prime is 5, so cross out all the multiples of 5. Continue in this way. What you have left are the prime numbers.
An ALGOL program to determine all the prime numbers through 10,000 can implement this algorithm by declaring a Boolean array with indices from 2 through 10,000:
begin
Boolean array a[2:10000];
integer i, j;
for i := 2 step 1 until 10000 do
a[i] := true;
for i := 2 step 1 until 100 do
if a[i] then
for j := 2 step 1 until 10000 ÷ i do
a[i x j] := false;
for i := 2 step 1 until 10000 do
if a[i] then
print (i);
end
The first for loop sets all the array elements to the Boolean value true. Thus, the program starts by assuming that all the numbers are prime. The second for loop goes from 1 through 100 (the square root