QBasic for Beginners – Post 3: Understanding Variables and Data Types

Hello, young programmers! 👋

Welcome to the 3rd lesson in our QBasic journey. So far, we have learned how to start QBasic and write simple programs. Today, we will dive into variables and data types – the building blocks of every program. 🏗️

What is a Variable?

A variable is like a storage box in your computer’s memory. It holds information that your program can use and change while it runs.

Think of it this way:

  • If your program is a classroom, a variable is like a student’s desk. You can put things on it (like numbers or text), check what’s there, or even replace it with something else.

How to Declare a Variable in QBasic

In QBasic, you can simply write a name for your variable and start using it. You don’t always need to declare it first, but for clarity and best practice, it’s a good idea.

Example:

DIM age AS INTEGER
DIM name AS STRING

Here:

  • age will store whole numbers.

  • name will store text.

Common Data Types in QBasic

Data TypeWhat it storesExample
INTEGERWhole numbers10, -5, 2026
SINGLEDecimal numbers3.14, -0.5
STRINGText"Hello", "QBasic"
DOUBLEVery large or precise decimal numbers123456.789
BOOLEANTrue/False valuesTRUE, FALSE

Assigning Values to Variables

After declaring, you can give a value to a variable using the = sign:

age = 16
name = "Aayush"

You can also change the value anytime:

age = 17 ' updated age

Using Variables in a Program

Here’s a simple program combining what we learned:

CLS
DIM name AS STRING
DIM age AS INTEGER
PRINT "Enter your name: "
INPUT name
PRINT "Enter your age: "
INPUT age
PRINT "Hello "; name; "! You are "; age; " years old."

Explanation:

  1. CLS clears the screen.

  2. INPUT lets the user type something and stores it in a variable.

  3. PRINT shows the result using your variables.

Try changing the values and see how the program behaves differently. This is the power of variables! 💡


Key Points to Remember

  • Variables are storage boxes in your program.

  • Always choose meaningful names for your variables.

  • Know the data type you need – numbers or text.

  • Variables can change value during program execution.

Comments