Page 69 - Digicode_AI_class_7
P. 69
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.
A variable which is declared but is not initialized ever has an undefined value, but 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
variable can be initialized to ‘Null’, which is also a value. Variable which is defined with the ‘Null’
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, when you first assign a value to it.
Example: Declaring a variable in Python.
x=7
y=“Orange Education”
Data Types in Programming
Data type identifies the type of data which the declared variable can hold. It 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 67

