7.2.2 Loops, Variables and Arithmetic Expressions

The following, a bit more complex program will introduce some new ideas like assignments, loops, the concept of variables and arithmetic expressions. By variables at this point we mean crisp data types, fuzzy data types will be treated in Chapter 7.5. In CSI(C&F), crisp data types like strings and floating point numbers must not be declared before they are used. The next program uses the formula °C = (5/9)*(F-32) to print the following table of Fahrenheit temperatures and their centigrade or Celsius equivalents:

Fahrenheit : 0

Celsius : -17.77777778

Fahrenheit : 20

Celsius : -6.666666667

...

Fahrenheit : 300

Celsius : 148.88888888

The program itself consists only of the main program and for this we have not to state an EXIT statement behind ENDLOOP:

/* Print Fahrenheit - Celsius table using LOOP */

LET Fahreinheit$ = "Fahrenheit : "

LET Celsius$ = "Celsius : "

LET lower = 0

LET upper = 300

LET step = 20

LET fahr = lower

LOOP

EXITLOOP (fahr>upper)

LET celsius = (5/9)*(fahr-32)

PRINT Fahrenheit$ PRINT fahr PRINT CR

PRINT Celsius$ PRINT celsius PRINT CR

LET fahr = fahr+step

ENDLOOP

The computation in the temperature conversion program begins with the assignment statements

LET lower = 0

LET upper = 300

LET step = 20

LET fahr = lower

which set the variables to their initial values. Individual statements have not to be terminated by any termination characters like semicolons etc. As each line in the temperature table is computed in the same way we use a loop that repeats once per output line.

LOOP

EXITLOOP (fahr>upper)

...

ENDLOOP

The loop operates as follows: the condition in parenthesis is tested. If it is true, we leave the loop statement, otherwise the body of the loop is executed. Then the condition is re-tested, and if false, the body is executed again. The body of a LOOP can be one or more statements, where the statement block within the body of the loop is terminated by the ENDLOOP statement.

The computation and assignment of the Celsius temperature gets done in the body of the loop by the statement

LET celsius = (5/9)*(fahr-32)

String assignments work in much the same way as numerical assignments do. Although variables holding string values have the same name space and structure as variables that hold numerical values, we suggest to mark strings with a '$' at the end of the name. So confusion about the "type" of variables can be avoided:

LET Celsius$ = "Celsius : "

LET Celsius = (5/9)*(fahr-32)

This does not mean that Celsius$ and Celsius are incompatible types of variables. As mentioned in the introduction, CSI(C&F) does only differentiate between fuzzy data types and crisp data types. So you may assign values to string variables and vice versa:

LET Celsius$ = 3.141592654

LET Celsius = "Pi"

Although CSI(C&F) does not care about how a program looks, well-named variables, proper indentation and spacing are critical in making programs easy for people to read.