VARIABLES
Variables are storage spaces that hold data.
Variables are given a
specific name (identifiers). For example
String name=” Joshua”;
The variable here is
“name “with the data type string and “Joshua “has been assigned to it.
The string data type
indicates that the variable(name) can only hold data that consists of
combination of characters. There are other data types such as; double, int,
char etc. that we will be talking about later in our next tutorials.
Why the name Variable?
The value of a variable
can be changed, hence the name variables.
In a program, every variable has:
Name (Identifier)
Data Type
Size
Value
TYPES OF VARIABLES
There are two types of
variables;
1. Local variables
2. Global variables
Local Variables
Local variables are
variables that are declared within programming block of subroutines. they can
only be accessed inside the subroutine or code in which they are declared.
GLOBAL VARIABLE
Global variables are
those that are in scope for the duration of the program’s execution. They can
be accessed at any part of the program.
Rules for naming Variables
In naming variables some
rules are applied, these rules include;
1. A variable must not begin with a number and must
have at least one alphabet.
2. A variable should only be made of
alphabet, numbers and underscore (_)
3. A variable name must not be a keyword. Example;
int, string etc.
Example of some valid
variables include;
number3, name,
code_2 etc.
Constants
A constant
is a value that never changes.
![]()
The keyword “const “is used in declaring constants.
Constants are declared as;
Const data
_type constant _name=value;
Example;
Here the keyword "const" has been used to declare the constant pie. If the value of pie is
changed, there will be an error


0 Comments