Programming Knowledge for Software Testers
Tutorial 5: Variables in Programming

Variables are the names we give to computer memory locations which are used to store values in a computer program.
Or
Variable is a named memory location to store the temporary data within a program

Creating variables is also called declaring variables, different programming languages have different ways of creating variables inside a program.

Important topics in this variables Chapter are:
1. What is Variable?
2. Declaration / Creating of variables
3. Storing Values
4. Rules for naming variables:
5. Types of variables

I am taking three different programming languages to explain this concept,

1. Java
2. Python
3. VBScript

1) Java

i. Declaring/Creating Variables

Syntax for declaring variables

dataType variableName;
dataType variableName=value;
dataType variable1Name, dataType variable2Name, dataType variable3Name;
dataType variable1Name=value, dataType variable2Name=value, dataType variable3Name=value;

Example:

int a;
a=100;

int b=200;

int c, d, e;
c=200; d=300; e=400;

int f=500, g=600, h=700;

System.out.println(b+g);//800

//Declare different type of Variables

int a=123456;
long b= 9787878987l;
float c=12.34f;
double d= 123456.6789;
char e='T';
boolean f=true;
String g="India123";
---------------------------------
ii. Storing Vaues
Initialization
Reading
------------------------------
iii. Rules for naming variables:

a) Java variables are case sensitive
b) Java Variable names should start with a letter, or _ or $
c) You cannot use a java keyword (reserved word) for a variable name.
d) Java variables must be unique in the scope of declaration
----------------------------------------
iv. Types of variables
a) Local variables
b) Instance variabls/Non static variables
c) Static variables / Class variables

2) Python

In Python, you do not need to declare variables before using them, or declare their type.

A variable is created the moment you first assign a value to it.

Example:

a = 5
b = "John"
print(a)
print(b)

Note: Variables do not need to be declared with any particular type and can even change type after they have been set. String variables can be declared either by using single or double quotes.

Example:

val=100
print (val)
val="India is my Country"
print (val)
val=12.34
print (val)

ii. Assign values/Strore values
a) Initialization
b) Reading

Assign Value to Multiple Variables

x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)

And you can assign the same value to multiple variables in one line:

x = y = z = "Orange"

iii) Rules for Python variables:

a. A variable name must start with a letter or the underscore character
b. A variable name cannot start with a number
c. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
d. Variable names are case-sensitive (age, Age and AGE are three different variables)

Note: A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).

iv. Global and Local Variables

Variables that are created outside of a function (as in all of the examples above) are known as global variables.

When we create a variable inside a function, that variable is local, and can only be used inside that function.

Note: If you use the global keyword, the variable belongs to the global scope:

x=100

def myfun():
y=200
print (x+y)

myfun()

print(x)
print(y)

3) VBScript

i. Declaration of variables

Using either Public or Private or Dim statements, we can declare Variables.

Ex:

Dim a
Dim x, y, z
Or
Dim x
Dim y
Dim z

ii. Implicit and Explicit variables.

Ex:
Dim a
a = 100 ‘Explicit
b = 200 ‘Implicit
Msgbox a + b
———————
Dim Tickets, Price, Total
Tickets = 7
Price = 100
Total = Tickets * Priee
Msgbox Total ‘0 (Incorrect output)
———–
Option Explicit
Dim Tickets, Price, Total
Tickets = 7
Price = 100
Total = Tickets * Priee
Msgbox Total ‘Error