Skip to main content

Variables, constants and datatypes

variables, constants and datatypes

Defining and declaring variables:

Data type is an important component of every programming language, which tells the compiler, the type of data that can be stored in the variables. What is a variable then? It is a container that holds the particular type of data.

Consider a container, that is made only to hold water with maximum capacity of 10 liters. So, now we can pour water in to that container till the maximum limit. Depends on the requirement, up to 10 liters of water can be stored.

Similarly, datatype is like a container that is made only to hold water i.e. only a specific type of data can be stored. The popular data types, used by programming languages are int, float, char, double etc.

Then what is a variable, it is name of the container that holds the water, let’s name that as Water Container. In programming, it is defined as below,

int water_container;

in the above expression int is a datatype and water_container is a variable. Int can hold only integers.
Consider we are storing 2 liters of water in the container, how can we do that,
int water_container = 2; int can store only some particular bits of data which of integer type.

So, to store extra amount of amount, we need a larger container, similar to that, to store high volumes of data, we need to use other data types. This is the basic for all the programming languages.

In INFO-BASIC, the datatype need not to be explicitly mentioned as like in other programming languages.

You can just say, water_container =2;

This means, BASIC is not a strongly typed language. So, you need to care about the data type of a variable. What will you do if you want store some characters in water_container variable.

water-container = “2 liters”

Even a string can be stored in the same variable as shown above. That’s all about data type. Give a name to the variable and store some value in it, no need to specific what type of data you are storing in BASIC.

What is a constant?

Let’s consider the value of Pi. Everyone knows it is 3.14. So, these values are constants and never be changed. But a data stored in a variable can be changed.

If you are doing some mathematical calculations, that has Pi constant, how to ensure that the value of Pi remains unchanged till the end of the program. To achieve this EQU should be used. What does this EQU mean, it is nothing but EQUATE. We are equating a value to a variable to make it constant.
Below is code representation,

EQU pi TO 3.14

This statement stores 3.14 in a memory located named as pi.
Interesting! Have you noticed, EQU and TO in the above statements are capitalized. Yes, because these are keywords.

What is a keyword? Does it is something special to do with my code.

Keywords are reserved words for special purposes. We used EQU to equate the value 3.14 to pi. “TO” is also capitalized. Because it is keyword too. There many keywords in BASIC and we will come across in the forth coming chapters. Very important, these keywords should not be used as a variable.


Do you have questions, please comment below? We will respond to you at the earliest.

Comments