User Input


What is a User Input?

Simply put, User Input is anything that the user types at the keyboard into the program. For example, when you are browsing the Internet you can type in an address in the address bar (such as www.libertybasic.com). After you type it in and press enter you are taken to that site. It is the user who decides what page is opened (not the computer).

Up until now we've been hard-coding information into our Liberty BASIC source code. For example, we've placed people's names, ages, year of birth, and other data right into the code of our programs. Every time we run our program it runs the exact same way with the same names and numbers. The user gets no say in what happens.

A better way to code our programs is with User Input. Now we can ask the user what their name is, what their age is, what year they were born in, etc. By doing this the program runs a bit different every time.

The Input Statement

Liberty BASIC lets you store user input into a variable by using the INPUT statement. When the computer runs the INPUT statement used in your code it waits for the user to enter something at the keyboard. After the user has typed that information at the keyboard and presses the Enter key it is then stored in the variable assigned to the INPUT statement. There are two ways to use an INPUT statement. Let us take a look at the simplest way:

print "What is your name"
input Name$
print "Welcome to the program "; Name$


This will produce the following run:


Notice that the first line of our code printed out the message "What is your name" to the screen. The INPUT statement when used in this fashion displays a question mark (?) and waits for the user to type something at the keyboard. In this example, after the user types in their name and presses the Enter key, the user's name is stored in the variable Name$. After the INPUT statement is completed the rest of the program continues on as shown in the run screen below.


The INPUT statement, like the PRINT also lets you display information to the run screen. In the above example our first two lines consisted of a print statement displaying information to the run screen followed by the input statement asking the user for input. We can do away with this first print statement altogether by using the following code:

input "What is your name? "; Name$
print "Welcome to the program "; Name$


Notice that we have added a question mark and a space to the end of our input string. This is because when we use the INPUT statement to display information to the run screen we are not forced to use the question mark. Therefore if we want to use the question mark we need to include it, if we want to use something else (such as a colon) we are free to use that or nothing at all. Our completed run (after the user has entered their name) will now look like this:


While these two options basically do the same thing, the last option is more flexible, shortens your code, and usually makes your run look a bit nicer.