Subsections


Printing multiples

We have already used print to convert plain values to characters displayed on your screen. In fact, print can be supplied with a row of values to be converted, so it is quite valid to write

   []INT i1 = (2,3,5,7,11,13);  print(i1)

You can also present an actual row-display. Instead of using

   print(2); print(blank); print(3)

you can write print((2,blank,3)). The doubled parentheses are necessary: the outer pair are needed by print anyway, and the inner pair are part of the row-display. Notice that the modes of the elements of the row-display are quite different. We shall learn in chapter 8 how that can be so.

Here is a program which will print the answers to the last exercise.

   PROGRAM test CONTEXT VOID
   USE standard
   BEGIN
    [,]INT i = ((1,-2,3,4),(-5,6,7,8));
    []REAL r= (1.4,0,-5.4,3.6);
    []CHAR s= "abcdefghijklmnopqrstuvwxyz"[@ ABS"a"];
    print(("i=",i,newline,
           "r=",r,newline,
           "s=[",s,"]",newline,
           "2 UPB i + UPB s[@1]=",
           2 UPB i+UPB s[@1],newline,
           "r[2:3]=",r[2:3],newline,
           "i[2,2] - r[3]=",
           i[2,2] - r[3],newline,
           "i[2,2:]=",i[2,2:],newline,
           "s[ABS""p"":ABS""t""]=",
           s[ABS"p":ABS"t"],
           newline))
   END
   FINISH

As you can see, print will quite happily take values of modes []CHAR, [,]INT, []REAL and so on4.3. Notice also that in order to get quote symbols in the last line to be printed, they are doubled. A common mistake is to omit a quote symbol or a closing comment symbol. If your editor provides lexical highlighting (usually called “syntax” highlighting), an omitted quote or comment symbol will cause a large part of your program to be highlighted as though it were a []CHAR or a comment. The mistake will be very clear. If your editor does not support lexical highlighting, you will get an odd message from the compiler (usually to the effect that it has run out of program!).


Exercises

3.9
Write short programs to print the answers to all the exercises in this chapter from 3.2.1. You should insert multiples of CHAR at suitable points, as in the example above, so that you can identify the printed answers. Ans[*]


Sian Mountbatten 2012-01-19