BASIC Programming/Beginning BASIC/User Input

From Wikibooks, open books for an open world
Jump to: navigation, search

Introduction[edit]

Read the Variables and Data Types article before reading this one.

User Input is one of the most important aspects of programming concepts. Every program should have some sort of user interaction, from getting a character's name for a game to asking for a password to log into a database. This article will teach the basics of user input in the BASIC Programming Language. Please note that the following code may vary from compiler to compiler. (FreeBASIC Users: Do not use line numbers).

Code[edit]

Example 1 (qBasic)

CLS
10 PRINT "what is your name?"
20 INPUT "...(Enter Your Name)...", a$
30 PRINT
40 PRINT "hello, "; a$; ", I am your computer, nice to meet you."
60 END

Example 2 (freeBasic)

10 Dim userInput As String
20 Input "What is your name?", userInput
30 Print
40 Print "Hello, " ; userInput ; "! I am your computer, nice to meet you."
50 Sleep
60 End

Explanation[edit]

Output

What is your name?
...(Enter Your Name)...

Hello, yourName, I am your computer, nice to meet you.

10: Dim userInput declares the variable userInput. As String tells the compiler that userInput is a string (A collection of pure text, can include numbers and symbols, but is considered text).
20: Input "What is your name?" prompts the user for their name. userInput OR a$ tells the compiler to store the answer in the variable in userInput or a$.
30: Makes a blank line
40: A normal Print statement, ; tells the compiler not to skip to the next line.
50: Sleep makes the program wait for the user to press a key.
60: The ending to all BASIC programs, signifies a termination of the program.


Previous: Variables and Data Types Main: BASIC Programming Next: Documentation