Variables

A variable is a user defined name which holds a value during program execution. This value is stored in memory and variable is used to refer to it.
This value can be fetched or updated(modified) using the variable name.
variable declaration in goAbove illustration contains a variable “name” which points to a memory location. Value at this memory location can be fetched or modified only using this variable.
[the_ad id=”651″] Defining variables in go
Variables in go are declared using var keyword followed by the name of variable and then its type as shown below.

// declare a string variable
var name string
// declare an int variable
var count int

You can also declare multiple variables in the same statement if they are of the same data type separated by a comma.

// declare multiple string variables
var name, section, code string

//declare multiple int variables
var counter, class, id int

Initializing variables
Initializing means providing a value to variables. Variables are initialized using = with the variable at the left hand side and the value at the right side of =.
Variables can be initialized in the same statement in which they are declared or in a separate statement after they are declared. Example,

// declaration
var site string
// initialization
site = "codippa.com"

Variables can be initialized in the same statement as

// declaration and initialization
var site string = "codippa.com"

This is called Static variable declaration since the type of variables is defined at the time they are declared. It is also possible to declare and initialize variables without giving their types. This is called Dynamic type declaration and is discussed next.
[the_ad id=”656″] Dynamic Type declaration
Variables in go can be declared without specifying their types. Go automatically infers their types from the value given to them while initialization.
There are two ways to define the type of variable dynamically.
First is to declare and initialize the variable in the same statement. In this case, you can omit the type of variable. Go will determine its type from the value given. Example,

// dynamic type declaration
var site = "codippa.com"

In this case, Go will understand that the variable is of type string. Note that after this statement, you can not change the type of variable.
Thus, if you try to change its value to 20, you will be served with an error

cannot use 20 (type int) as type string in assignment

Multiple variables can also be initialized using this syntax with the declaration and initialization both separated by comma. Example,

// multiple variables dynamic declaration
var site, age, rate = "codippa.com", 24, 12.34

With this syntax, the number of variables and the count of values assigned to them should match else there will be an error.

assignment mismatch: 2 variables but 1 values

Second way to define the type of a variable dynamically is using := operator which is a colon followed by =.
This is a shorthand notation of creating a variable since it does not even require using var keyword. Example,

//dynamically declare and initialize string variable
site := "codippa.com"

Above variable declaration is perfectly legal. Again, Go will determine the type of variable from the value assigned.
[the_ad id=”3530″]

Variables

What is a variable?
A variable is a named memory location where you can store data or values temporarily during stored procedure execution.
It is called named because user can provide a meaningful name to a variable. This name is then used to store and fetch(or read) data from variable.
[the_ad id=”89″] Benefit of variable
A variable is a temporary data location, thus, in a scenario where you want to store data of one operation and require this data in another operation, variables are very beneficial.
Consider scenarios below where a variable becomes useful

  • Swapping the values of two or more columns of a table.
  • Suppose you have a couple of SQL queries in a procedure where a value from 1 query needs to be used in WHERE clause in second query, you can store it in the variable and read it back.
    This can also be done using an inner query but using variable in a stored procedure is a better alternative. Further, it will also be useful if some operation needs to be performed on that value before using it.

Syntax
A variable needs to be declared before using it which is done using below syntax.

DECLARE [variable name] [data type] DEFAULT [initial value];

where,
DECLARE keyword is used for declaring a variable. It is followed by a user defined name by which a variable will be identified.
Variable name is followed by the data type of the variable. It defines the type of values that the variable would hold. Example, INT, DECIMAL etc.
If you want an initial value for the variable, then define it using DEFAULT keyword followed by the initial value. This is optional.
If you do not specify the initial value using DEFAULT, then it will be null.
Examples of variable declaration are as below

DECLARE txnid INT;
DECLARE guest_name VARCHAR(100) DEFAULT null;
DECLARE cost DOUBLE DEFAULT 0.0;

[the_ad id=”94″] Remember that all the variables required by the procedure should be declared just after the BEGIN statement. You can not declare variables anywhere or just when required.
Assigning variable values
Once a variable is declared, it can be assigned values. This value may be assigned directly or as a result of some SQL query.
Direct assignment of a variable value is done using SET statement which is like an initialization statement. Example,

SET txnid = 18394;

Remember that the value should be of the same type as defined while declaring the variable.
A variable can also be assigned value as a result of an SQL query using INTO keyword followed by the name of variable. Example,

DECLARE lasttxnid INT;
SELECT MAX(id) INTO lasttxnid FROM transaction;

This is similar to how OUT and INOUT parameters were assigned values.
Variable example
Below is a complete example of using a variable in MySQL stored procedure. As before, we will be using our sample STUDENT table for illustration.

DELIMITER %%
CREATE PROCEDURE variable_usage()
BEGIN
— declare variable
DECLARE student_country varchar(30);
— fetch John’s country and store it in variable
SELECT country INTO student_country FROM student WHERE name=’John Doe’;
— fetch all students whose country is different than John’s country
SELECT name FROM STUDENT WHERE country != student_country;
END %%
DELIMITER ;

[the_ad id=”86″] Above procedure fetches the country for student whose name is John Doe and stores it in a variable. Then it fetches the names of all those students whose country is different than that of John Doe’s.
Though this can also be done using an inner query but the usage of variable makes it simpler and more flexible. This procedure is invoked as

CALL variable_usage;

Output is

Notice the use of INTO keyword to store value of a column into the variable.

Variables

What is a variable?
In javascript, all values are stored at some memory location.
Now, a user can not directly access a memory location hence variables are used.

A variable is a user defined name that is used to access a memory location. Using a variable, you can store or modify a value at the memory location.
Example,
Above illustration declares two variables(digit and lang) pointing to different memory locations having different values.
[the_ad id=”647″] How variables are declared?
Variables in javascript are declared using var keyword followed by the name of variable which can be any user defined name.
Example,

var digit = 5;
var lang = 'javascript';

ES6(or ECMAScript6) introduced a different way of declaring variable.
It suggests using let keyword in place of var.

Thus, above variable declarations can be modified as below.

let digit = 5;
let lang = 'javascript';

If the variable value will never change after initialized for the first time or you do not want it to be changed accidentally, then use const in place of let.

Javascript engine will not allow modification in variable values declared with const. Example,

const digit = 5;
digit = 10;   // Illegal

Variable naming rules
Though a variable can be a user defined name but there are few rules that should be followed while declaring a variable.

They are
1. A variable name can not begin with a number.
Thus, 12digit, 0lang are invalid variable declarations. Variable names should begin with a letter or underscore. Examples, _start, digit123, technology etc., are valid variables.

2. A variable name can not be a javascript keyword or a reserved word(explained below).

Variables in javascript are case-sensitive.
Thus, language, Language, languagE are three different variables.
[the_ad id=”3385″] Keywords
A keyword, also called reserved word, is a word which has some special meaning in the language syntax.
When a keyword is encountered, javascript engine performs some action that is specific to that keyword.
Example, break is a keyword.
When javascript engine finds this keyword, it terminates an executing loop.

A keyword cannot be used as a variable name, function or class name in javascript.
Following are the keywords in javascript.

abstract boolean break byte case catch
char class const continue debugger default
delete do double else enum export
extends false final finally float for
function goto if implements import in
instanceof int interface long native new
null package private protected public return
short static super switch synchronized this
throw throws transient true try typeof
var void volatile while with

Variables

A variable is a named identifier which contains a value. It always points to a memory location. In python variables do not have any type such as int, float etc. They are dynamically typed which means their type is decided at execution time according to the value they hold.
A variable is created at the time when a value is assigned to it. Example,

value = 4          #variable of integer type
name = “codippa”   #variable of string type

Variable declaration rules
A variable name defines the purpose of a value in a program. Example, a variable which holds a name can be declared as name or emp_name or person_name etc. However, this depends on the programmer and there are no rules for this. But there are some rules which should be followed while variable declaration. A variable name should

  1. start with a letter or an underscore. It cannot start with a number.
  2. contain only alphanumeric characters. No special characters are allowed.

Examples of valid variable names are name123, emp_id, address2, _version etc.
Examples of invalid variable names are 123name, emp#, a2ddress etc.

Variable names in python are case-sensitive. Address and address are two different variables.
If you declare an invalid variable name, then you get SyntaxError: invalid syntax at run time.

[the_ad id=”89″]

Printing the value of a variable
Python provides a built-in function named print which prints the value of a variable as shown below.

name = "codippa"
print(name)          # will print codippa

Getting type of a variable
Python’s type function can be used to determine the type of any variable, that is the type of value contained in a variable. Examples,

name = “codippa”
type(name)         # will print <class 'string'>

count = 1
type(count)        # will print <class 'int'>

Multi variable assignment
Python allows assigning a value to multiple variables simultaneously, that is, same value can be assigned to multiple variables at the same time as shown below.

first = second = third = 5
print(first)      # will print 5
print(second)     # will print 5
print(third)      # will print 5
[the_ad id=”95″]

Python also allows assigning different values to different variables at the same time, that is, different values(of same or different type) can be assigned to different variables in a single statement as shown below.

num, name, count = 2, "codippa", 5
print(num)      # will print 2
print(name)     # will print codippa
print(count)    # will print 5

Exit mobile version