variables

Computer programs manipulate (or process) data. A variable is used to store a piece of data for processing. It is called variable because you can change the value stored.

More precisely, a variable is a named storage location, that stores a value of a particular data type. In other words, a variable has a name, a type and stores a value.

A variable has a name (or identifier), e.g., radius, area, age, height. The name is needed to uniquely identify each variable, so as to assign a value to the variable (e.g., radius=1.2), and retrieve the value stored (e.g., area = radius*radius*3.1416).
➤A variable has a type. Examples of type are,
int: for integers (whole numbers) such as 123 and -456;
double: for floating-point or real numbers such as 3.1416, -55.66, having a decimal point and fractional part.
➤A variable can store a value of that particular type. It is important to take note that a variable in most programming languages is associated with a type, and can only store value of the particular type. For example, a int variable can store an integer value such as 123, but NOT real number such as 12.34, nor texts such as "Hello".
The concept of type was introduced into the early programming languages to simplify interpretation of data made up of 0s and 1s. The type determines the size and layout of the data, the range of its values, and the set of operations that can be applied.

The following diagram illustrates two types of variables: int and double. An int variable stores an integer (whole number). A double variable stores a real number.

Variable Declaration
To use a variable in your program, you need to first "introduce" it by declaring its name and type, in one of the following syntaxes:

Syntax
// Declare a variable of a specified type
type identifier;
// Declare multiple variables of the same type, separated by commas
type identifier-1, identifier-2, ..., identifier-n;
// Declare a variable and assign an initial value
type identifier = value;
// Declare multiple variables with initial values
type identifier-1 = value-1, ..., identifier-n = value-n;

Comments

Popular posts from this blog

Covid-19 Analysis and Visualization using Plotly Express

Artificial Intelligence (Problem Solving)

Linear Regression