×
13 Lessons

Model 2

In this module, we continue our journey into programming by introducing one of the earliest and most widely used beginner-friendly programming languages: QBasic. This curriculum is designed to help participants understand the fundamental principles of programming using QBasic as a foundation. By the end of this module, you will be able to write, run, and debug simple programs while also learning the logical thinking skills necessary to advance into modern programming languages.

15 Lessons

Welcome to today’s lesson, The Other Way to Declare a Variable in QBasic.
In our previous class, we learned the first way to declare a variable using type declaration characters such as $, %, &, !, and #.
In this lesson, we are taking a deeper look at another method of declaring variables, using the DIM statement with AS to define variable types more clearly and professionally in QBasic.

By the end of this lesson, participants will be able to

👉 Understand how to declare variables using the DIM statement in QBasic.
👉 Identify and define different data types using AS STRING, AS INTEGER, AS LONG, AS SINGLE, and AS DOUBLE.
👉 Write practical QBasic programs that demonstrate variable declaration and execution.

Lesson Explanation (Tutorial Format)

👉 In QBasic, we can declare variables in two ways:
👉 Using type-declaration symbols (like $, %, &, !, #).
👉 Using the DIM statement with AS to specify the data type.

The second method is more descriptive and is commonly used in professional coding. It makes your program easier to read and understand.

Here’s the general structure

DIM [VariableName] AS [DataType]

For example

DIM name AS STRING

DIM age AS INTEGER

DIM amount AS SINGLE

DIM salary AS DOUBLE

DIM studentID AS LONG

This tells QBasic the type of data each variable will hold.

Practical Example 1

DIM Numn1 AS INTEGER

DIM Numn2 AS LONG

DIM Numn3 AS SINGLE

DIM Numn4 AS DOUBLE

DIM Header AS STRING

Header = "Another way in declaring variables"

Numn1 = 5

Numn2 = 5600

Numn3 = 45.7

Numn4 = 6600.78

PRINT Header

PRINT Numn1 + Numn2 + Numn3 + Numn4

Here, we declared each variable using DIM and assigned them different data types.

When the program runs, it prints the header and then calculates the total of all the numbers.

Practical Example 2

DIM Numn1 AS INTEGER

DIM Numn2 AS LONG

DIM Numn3 AS SINGLE

DIM Numn4 AS DOUBLE

DIM Header AS STRING

Header = "Another way in declaring variables"

Numn1 = 5

Numn2 = 5600

Numn3 = 45.7

Numn4 = 6600.78

PRINT Header

PRINT Numn1; "+"; Numn2; "+"; Numn3; "+"; Numn4; "="; Numn1 + Numn2 + Numn3 + Numn4

This example prints the expression and the result together, helping you visualize both the operation and its output clearly.

Note

Using DIM with AS makes your QBasic programs neat, readable, and more professional. It also helps prevent data type errors and improves program structure.