Input

A question never asked is never answered

So far our programs have only produced output. With INPUT, a program can ask the user for a value and use it.

Using INPUT

INPUT takes no arguments, so we call it with empty parentheses. It pauses the program, asks the user to enter a value, and then returns what they entered. Like any other returned value, we can store it in a variable.

csp
You may also type <- !=>= <= for auto complete.

When the program reaches INPUT(), a dialog box appears and waits. Once the user submits it, the call is replaced by the entered value, just like the call to square(4) was replaced by 16 in the last chapter.

We do not have to store the result. The returned value can be used directly anywhere a value is allowed.

csp
You may also type <- !=>= <= for auto complete.

This asks the user for something and immediately displays it back.

Numbers and Text

The user types everything the same way, but INPUT does not return everything as the same type. If what the user enters is a number, INPUT returns a number. Otherwise, it returns a string.

user typesreturned valuetype
4242number
2.52.5number
hello"hello"string
42 apples"42 apples"string
true"true"string

Notice the last row. INPUT never returns a boolean. If the user types true, we get the string "true", not the boolean true.

Because a number comes back as a real number, we can do arithmetic with it right away.

csp
You may also type <- !=>= <= for auto complete.

If the user enters 6, this displays 36.

The catch is that we cannot know ahead of time which type we will get. If the user types six instead, n holds a string, and n * n does not make sense. A program that expects a number has to be ready for text.

We can only tell the two apart by what the program does with the value, so a good habit is to ask the user clearly for what we expect, like “Enter a number” above.

Why a Dialog Box?

In many languages, input is read from the terminal. CSP instead uses the prompt dialog built into your browser. There are two reasons for this.

Terminal (noun)
A text interface that allows users to interface with a computer without graphics

First, it makes it clear that input is expected. A dialog box that asks you to type something is hard to miss. A program that sits silently in a terminal, waiting for you to press enter, looks exactly like a program that has frozen.

Second, DISPLAY does not print a newline. With terminal input, the text the user types would end up on the same line as the program’s output, which looks messy. The dialog box keeps the user’s typing separate from the output.

Since DISPLAY does not add a newline, remember to end your messages with \n when you want the next output to start on a new line.

Input Inside Programs

Because INPUT is a value like any other, it works with everything we have learned. We can compare it in a conditional, or collect several answers in a list.

csp
You may also type <- !=>= <= for auto complete.

Remember that CSP is case sensitive, so "Swordfish" would not match "swordfish".

Comparing works with numbers as well. Since INPUT returns a number when the user types one, we can compare it with a number directly.

csp
You may also type <- !=>= <= for auto complete.

Lists can collect several answers.

csp
You may also type <- !=>= <= for auto complete.

Each call to INPUT() opens a new dialog, so this program asks three times. Since lists can mix types, answers could hold numbers and strings at once if the user does not follow instructions, and the addition would fail on the strings.


Next: More Loops