Commands


Commands

So far you have used expressions and functions to perform computation and Let-statements to allow yourself to refer to a value by a name. A Let-statement is a kind of command. With this chapter we will begin to explore what you can do with commands.

In programming language terminology, a command is called a statement, and we will use that terminology.


Writing information on the console

One way for a program to tell the user information is to write it on a terminal window (often called the console). In Cinnameg, you write a value using a Display statement. To write the value of expression e, use statement

  Display e.
You can display any of the kinds of things that we have seen so far: real numbers, integers, boolean values, characters or strings. For example,
  Display "Hello".
writes Hello onto the console. A Display statement does not end the line unless you explicitly tell it to end the line. Use the newline character, '\n', to end a line. For example,
  Display "Hello\n".
writes what you would get by typing Hello and then the enter key. You can also use Displayln, which is the same as Display except that it adds a newline to the end. So
  Displayln "Hello".
is equivalent to
  Display "Hello".
  Display "\n".


Sequencing statements

A step-by-step algorithm is a sequence of any kinds of statements. They are performed one after the other. For example,

  Let age: Integer = 20.
  Display "I am ".
  Display age.
  Display " years old\n".
Shows
I am 20 years old
on the console.


An example

Let's take our short quadratic equation solver and make it show its answer. The goal is to find two values of x that make ax2 + bx + c = 0, where a, b and c are given numbers.

  Let a: Real = 20.0.
  Let b: Real = -50.0.
  Let c: Real = 1.0.
  Let d: Real = sqrt(b*b - 4*a*c).
  Let twoA: Real = 2*a.
  Let x1: Real = (-b + d)/twoA.
  Let x2: Real = (-b - d)/twoA.
  Display "The first solution is ".
  Display x1.
  Display "\n".
  Display "The second solution is ".
  Display x2.
  Display "\n".

Shortening displays

Instead of doing several displays you can just concatenate strings together and show the concatenated strings. But you will need to use $(x) for values that are not strings, to convert them to strings. Using Displayln can further shorten the program. For example, sequence of statements

  Display "The first solution is ".
  Display x1.
  Display "\n".
  Display "The second solution is ".
  Display x2.
  Display "\n".
can be shortened to two statements.
  Displayln "The first solution is " ++ $(x1).
  Displayln "The second solution is " ++ $(x2).


Problems

  1. [solve] Write a statement that will display You are wise. on the console, followed by an end-of-line.

  2. [solve] Write a statement that defines name to be your name (a string). Follow it by statements that display your name is wise. on the console, followed by an end-of-line, where your name is the value of name. For example, if your name is Doc, then it should display

    Doc is wise.
    
    Do not write your name more than once in the program.


Summary

A command is called a statement.

One kind of statement displays information on the console. Use

  Display e.
to display the value of expression e.


Review

Thinking about types helps you understand what you are doing. You can write type information in your programs and function definitions. Doing that tends to make programs easier to understand.

A name can contains letters and digits, provided it starts with a letter. A name cannot have any spaces in it. The case of letters matters. Names Q and q are not the same, and cannot be used interchangeably. In Cinnameg, it is important to choose names that start with lower case letters.