Welcome to the world of QBASIC programming! If you’re new to coding or just starting out, QBASIC is a great language for beginners. In this guide, we’ll introduce you to important QBASIC commands and show you examples of how to use them.
Your First QBASIC Program
Let’s begin by making a simple “Hello, World!” program. This classic practice is a great way to get comfortable with QBASIC’s rules and arrangement. Here’s the piece of code you need:
PRINT "Hello, World!"
In this code, the PRINT
command is used to display the text “Hello, World!” on the screen.
Variables and User Input
In QBASIC, you can use variables to store and work with data. They’re important for keeping and changing information. Here’s an example that shows how to use variables and get input from the user:
INPUT "What's your name? "; name$
PRINT "Hello, "; name$; "!"
In this code, the INPUT
command prompts the user to enter their name. The variable name$
is used to store the input, and then the program prints a personalized greeting that includes the user’s name.
Basic Arithmetic Operations
QBASIC enables you to perform various arithmetic operations such as addition, subtraction, multiplication, and division. Let’s explore these operations through code:
LET a = 5
LET b = 3
PRINT "Sum: "; a + b
PRINT "Difference: "; a - b
PRINT "Product: "; a * b
PRINT "Quotient: "; a / b
Here, the LET
command assigns values to the variables a
and b
. The +
, -
, *
, and /
operators are used for addition, subtraction, multiplication, and division respectively.
Conditional Statements
Conditional statements allow your program to make decisions based on specific conditions. Let’s take a look at a basic example using the IF...THEN
statement:
INPUT "Enter your age: "; age
IF age >= 18 THEN
PRINT "You are an adult."
ELSE
PRINT "You are a minor."
END IF
In this code, the IF...THEN
statement checks whether the user’s age is 18 or older. If the condition is met, the program prints “You are an adult.”; otherwise, it prints “You are a minor.”
Loops: FOR…NEXT
Loops are a powerful concept in programming, allowing you to repeat a set of instructions multiple times. The FOR...NEXT
loop is commonly used for this purpose:
FOR i = 1 TO 5
PRINT "Count: "; i
NEXT i
Within this code, the FOR...NEXT
loop iterates the variable i
from 1 to 5. During each iteration, it prints “Count: ” followed by the current value of i
.
Conclusion
In this beginner’s guide to QBASIC programming, we’ve looked at some basic commands that are like the building blocks of your coding adventure. QBASIC helps you start strong by letting you show text, do calculations, make choices, and repeat actions. As you keep exploring, feel free to play around with these commands, change the examples, and dive into more advanced QBASIC stuff.