Page 99 - Computer Genius Class 06
P. 99
Create/Declare a Variable
Syntax is a rule which must be followed while performing a task.
In most of the programming languages, you can declare a variable by using the following syntax:
Datatype VariableName=Value;
Example: Declaring a variable.
int x=20
int y=30
int x, int y will create two variables ‘x’ and ‘y’ and it reserve two memory locations with names ‘x’ and
‘y’. Variables are created by using the ‘int’ keyword. ‘int’ specify the data type which means that the
declared variable will store integer values ‘20’ and ‘30’ in these two variables.
Once the variable is created, you need to assign it a value before using it. This process of assigning
a value to a variable is called initialization.
aria le hich is declared ut is not initiali ed e er has an undefined alue ut you can assign it
a value in later steps by using assignment operators.
Consider example 1, in which two variable ‘x’ and ‘y’ are declared. Variable ‘x’ stores a value ‘20’ and
variable ‘y’ stores a value ‘30’.
Now, when the program is executed, the memory location named as ‘x’ will hold a value ‘20’ and
memory location named as ‘y’ will hold a value ‘30’.
As you already know that a variable cannot be used unless a value is assigned to it. The value of a
aria le can e initiali ed to ull hich is also a alue. aria le hich is defined ith the ull
value is a variable which is not initialized and also it does not hold any value, not even ‘Null’. In
some programming languages like Python, there is no command to declare the variables. A variable
is created at the moment hen you first assign a alue to it.
Example: Declaring a variable in Python.
x=7
y=“Orange Education”
Data Types in Programming
ata type identifies the type o data hich the declared aria le can hold. t helps the computer to
understand what operations need to be performed on the declared variables.
To declare the variable, you need to define:
name of the variable
its type
More on MakeCode Arcade 97

