Showing the Value of a Variable
We have learned how to calculate and how to store results in variables. How do we show these results on the screen? Suppose we perform this calculation:
How do we later show the value of y? Assume we have defined y. Perhaps we could use this:
No, that won't work; that will just put the letter βyβ on the screen. The -write- command is basically a device for displaying non-varying text, not for showing the value contained in a variable. We need another command:
This will show the value of y in an appropriate format (-show- picks an appropriate number of significant figures and will use a scientific format such as 6.7Γ1013, if the number is large enough to require it). By using -show- instead of -write-, you tell TUTOR that you want the stored value to be shown rather than just the characters in the tag.
The -show- command will normally choose 4 significant figures, so that a typical display might be β-23.47β. You can specify a different value by giving a second βargumentβ (arguments are the individual pieces of the tag of a statement):
The arguments of the -show- command can, of course, be complicated expressions:
In fact, it is a general rule that you can use complicated expressions anywhere in TUTOR statements. For example, ''draw 5rad +225,34L;123-L2,28Lβ!
Here is a short program which uses -show- to display a table (see Figure 4-6) of square roots of the integers from 1 to 15:
The last statement could also be written as βshow NWβ. 'This technique of making tables, including the use of the -do- index (N) to position the displays (as in βat 425Β±100Nβ) is an important and powerful tool.
There are other commands for displaying variables: -showe- (exponential), -showt- (tabular), -showa- (alphanumeric), -showo- (octal), and -showz- (show trailing zeroes). These are described in detail in the reference material mentioned in Appendix A.
Although -write- is basically designed for non-variable text, combinations of text and variables occur so often that TUTOR makes it easy to βembedβ a -show- command within a -write-:
The embedded βsβ indicates a -show- command and the remainder β13.7w,6β is its tag. Other permissible abbreviations include βoβ (showo), βaβ (showa), βeβ (showe), βtβ (showt) and βzβ (showz). The above -write- statement is equivalent to: