- Get link
- X
- Other Apps
Recent - Posts
- Get link
- X
- Other Apps
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 INTEGERDIM name AS STRING
Here:
-
agewill store whole numbers. -
namewill store text.
Common Data Types in QBasic
| Data Type | What it stores | Example |
|---|---|---|
| INTEGER | Whole numbers | 10, -5, 2026 |
| SINGLE | Decimal numbers | 3.14, -0.5 |
| STRING | Text | "Hello", "QBasic" |
| DOUBLE | Very large or precise decimal numbers | 123456.789 |
| BOOLEAN | True/False values | TRUE, FALSE |
Assigning Values to Variables
After declaring, you can give a value to a variable using the = sign:
age = 16name = "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:
CLSDIM name AS STRINGDIM age AS INTEGERPRINT "Enter your name: "INPUT namePRINT "Enter your age: "INPUT agePRINT "Hello "; name; "! You are "; age; " years old."
✅ Explanation:
-
CLSclears the screen. -
INPUTlets the user type something and stores it in a variable. -
PRINTshows 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.
- Get link
- X
- Other Apps
Comments