Categories
3. Variables, Operators, Arrays and mapping

Solidity – Operators

In any programming language, operators play a vital role i.e. they create a foundation for the programming. Similarly, the functionality of Solidity is also incomplete without the use of operators. Operators allow users to perform different operations on operands. Solidity supports the following types of operators based upon their functionality.

  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Assignment operators
  6. Conditional Operator

Arithmetic Operators

These operators are used to perform arithmetic or mathematical operations. Solidity supports the following arithmetic operators : 

OperatorDenotationDescription
Addition+Used to add two operands
SubtractionUsed to subtract the second operand from first
Multiplication*Used to multiply both operands
Division/Used to divide numerator by denominator
Modulus%Gives the remainder after integer division
Increment++Increases the integer value by one
DecrementDecreases the integer value by one

Example: In the below example, the contract SolidityTest demonstrates the above mentioned different types of arithmetic operators.

// Solidity contract to demonstrate
// Arithmetic Operator
pragma solidity ^0.5.0;

// Creating a contract
contract SolidityTest {

	// Initializing variables
	uint16 public a = 20;
	uint16 public b = 10;

	// Initializing a variable
	// with sum
	uint public sum = a + b;

	// Initializing a variable
	// with the difference
	uint public diff = a - b;

	// Initializing a variable
	// with product
	uint public mul = a * b;

	// Initializing a variable
	// with quotient
	uint public div = a / b;

	// Initializing a variable
	// with modulus
	uint public mod = a % b;

	// Initializing a variable
	// decrement value
	uint public dec = --b;

	// Initializing a variable
	// with increment value
	uint public inc = ++a;
	
}

Output :  

Arithematic Operator

Relational Operators

These operators are used to compare two values. Solidity supports the following relational operators :  

OperatorDenotationDescription
Equal==Checks if two values are equal or not, returns true if equals, and vice-versa
Not Equal!=Checks if two values are equal or not, returns true if not equals, and vice-versa
Greater than>Checks if left value is greater than right or not, returns true if greater, and vice-versa
Less than<Checks if left value is less than right or not, returns true if less, and vice-versa
Greater than or Equal to>=Checks if left value is greater and equal than right or not, returns true if greater and equal, and vice-versa
Less than or Equal to<=Checks if left value is less than right or not, returns true if less and equals, and vice-versa

Example: In the below example, the contract SolidityTest demonstrates the above mentioned different types of relational operators. 

// Solidity program to demonstrate
// Relational Operator
pragma solidity ^0.5.0;

// Creating a contract
contract SolidityTest {

	// Declaring variables
	uint16 public a = 20;
	uint16 public b = 10;

	// Initializing a variable
	// with bool equal result
	bool public eq = a == b;

	// Initializing a variable
	// with bool not equal result
	bool public noteq = a != b;
	
	// Initializing a variable
	// with bool greater than result
	bool public gtr = a > b;

	// Initializing a variable
	// with bool less than result
	bool public les = a < b;

	// Initializing a variable
	// with bool greater than equal to result
	bool public gtreq = a >= b;

	// Initializing a variable
	// bool less than equal to result
	bool public leseq = a <= b;
	
}

Output :  

Relational Operator

Logical Operators

These operators are used to combine two or more conditions. Solidity supports the following arithmetic operators :  

OperatorDenotationDescription
Logical AND&&Returns true if both conditions are true and false if one or both conditions are false
Logical OR||Returns true if one or both conditions are true and false when both are false
Logical NOT!Returns true if the condition is not satisfied else false

Example: In the below example, the contract logicalOperator demonstrates the above mentioned different types of logical operators. 

// Solidity program to demonstrate
// Logical Operators
pragma solidity ^0.5.0;

// Creating a contract
contract logicalOperator{

	// Defining function to demonstrate
	// Logical operator
	function Logic(
	bool a, bool b) public view returns(
	bool, bool, bool){
		
	// Logical AND operator
	bool and = a&&b;
		
	// Logical OR operator
	bool or = a||b;
		
	// Logical NOT operator
	bool not = !a;
	return (and, or, not);
}
}

Output : 

Logical Operator

Bitwise Operators

These operators work at a bit level used to perform bit-level operations. Solidity supports the following arithmetic operators :

OperatorDenotationDescription
Bitwise AND&Performs boolean AND operation on each bit of integer argument
BitWise OR|Performs boolean OR operation on each bit of integer argument
Bitwise XOR^Performs boolean exclusive OR operation on each bit of integer argument
Bitwise Not~Performs boolean NOT operation on each bit of integer argument 
Left Shift<<Moves all bits of the first operand to the left by the number of places specified by the second operand
Right Shift>>Moves all bits of the first operand to the right by the number of places specified by the second operand

Example: In the below example, the contract SolidityTest demonstrates the above mentioned different types of bitwise operators.

// Solidity program to demonstrate
// Bitwise Operator
pragma solidity ^0.5.0;

// Creating a contract
contract SolidityTest {

	// Declaring variables
	uint16 public a = 20;
	uint16 public b = 10;

	// Initializing a variable
	// to '&' value
	uint16 public and = a & b;

	// Initializing a variable
	// to '|' value
	uint16 public or = a | b;

	// Initializing a variable
	// to '^' value
	uint16 public xor = a ^ b;

	// Initializing a variable
	// to '<<' value
	uint16 public leftshift = a << b;

	// Initializing a variable
	// to '>>' value
	uint16 public rightshift = a >> b;

	// Initializing a variable
	// to '~' value
	uint16 public not = ~a ;
	
}

Output : 

Bitwise Operators

Assignment Operator

These operators are for the assignment of value to a variable. The operand at the left side is variable while operand at the right side is value. Solidity supports the following arithmetic operators : 

OperatorDenotationDescription
Simple Assignment =Simply assigns the value at the right side to the operand at the left side
Add Assignment+=Adds operand at the right side to operand at the left side and assigns the value to left operand
Subtract Assignment-=Subtracts operand at the right side from operand at the left side and assigns the value to left operand
Multiply Assignment*=Multiplies both the operands and assign the value to left operand
Divide Assignment/=Divides operand at the left side by operand at the right side and assign the value to left operand
Modulus Assignment%=Divides operand at the left side by operand at the right side and assign the remainder to left operand

Example: In the below example, the contract SolidityTest demonstrates the above mentioned different types of assignment operators.

// Solidity program to demonstrate
// Assignment Operator
pragma solidity ^0.5.0;

// Creating a contract
contract SolidityTest {

		// Declaring variables
		uint16 public assignment = 20;
		uint public assignment_add = 50;
		uint public assign_sub = 50;
		uint public assign_mul = 10;
		uint public assign_div = 50;
		uint public assign_mod = 32;
	
		// Defining function to
		// demonstrate Assignment Operator
		function getResult() public{
		assignment_add += 10;
		assign_sub -= 20;
		assign_mul *= 10;
		assign_div /= 10;
		assign_mod %= 20;
		return ;
		}
}

Output : 

Assignment Operator

Conditional Operators

It is a ternary operator that evaluates the expression first then checks the condition for return values corresponding to true or false. 

Syntax: 

if condition true ? then A: else B

Example: In the below example, the contract SolidityTest demonstrates the conditional operator. 

// Solidity program to demonstrate
// Conditional Operator
pragma solidity ^0.5.0;

// Creating a contract
contract SolidityTest{

	// Defining function to demonstrate
	// conditional operator
	function sub(
	uint a, uint b) public view returns(
	uint){
	uint result = (a > b? a-b : b-a);
	return result;
}
}

Output :  

Conditional Operator
Categories
3. Variables, Operators, Arrays and mapping

Solidity – Variables

Variable is basically a placeholder for the data which can be manipulated at runtime. Variables allow users to retrieve and change the stored information. 

Rules For Naming Variables

1. A variable name should not match with reserved keywords.

2. Variable names must start with a letter or an underscore (_), and may contain letters from “a to z” or “A to Z” or digits from “0 to 9” and characters also.

Example:

Geeks123, geeks, _123geeks are valid variable names 
123geeks, $Geeks, 12_geeks are invalid variable names

3. The name of variables are case sensitive i.e.

Example:

Geeks123 and geeks123 are different variables

Declaration of Variables

In Solidity declaration of variables is a little bit different, to declare a variable the user has to specify the data type first followed by access modifier. 

Syntax:

<type> <access modifier> <variable name> ; 

Example:

int public int_var; 

Types of Variables

Solidity is a statically typed language i.e. each declared variable always has a default value based on its data type, which means there is no concept of ‘null’ or ‘undefined’. Solidity supports three types of variables: 

1. State Variables: Values of these variables are permanently stored in the contract storage. Each function has its own scope, and state variables should always be defined outside of that scope.

Example: In the below example. the contract Solidity_var_Test initializes the values of an unsigned integer state variable using a constructor.

// Solidity program to
// demonstrate state
// variables
pragma solidity ^0.5.0;

// Creating a contract
contract Solidity_var_Test {
	
// Declaring a state variable
uint8 public state_var;	

// Defining a constructor
constructor() public {
	state_var = 16;
}
}

Output : 

State Variables Output

2. Local Variable: Values of these variables are present till the function executes and it cannot be accessed outside that function. This type of variable is usually used to store temporary values.

Example: In the below example, the contract Solidity_var_Test defines a function to declare and initialize the local variables and return the sum of the two local variables.

// Solidity program to demonstrate
// local variables
pragma solidity ^0.5.0;

// Creating a contract
contract Solidity_var_Test {

// Defining function to show the declaration and
// scope of local variables
function getResult() public view returns(uint){
	
	// Initializing local variables
	uint local_var1 = 1;
	uint local_var2 = 2;
	uint result = local_var1 + local_var2;
	
	// Access the local variable
	return result;
}
}

Output : 
 

Local Variables Output

3. Global Variables: These are some special variables that can be used globally and give information about the transactions and blockChain properties. Some of the global variables are listed below :

VariableReturn value
blockhash(uint blockNumber) returns (bytes32)Hash of a given block, works for only 256 most recent transactions excluding current blocks
block.coinbase (address payable)Address of current blocks miner
block.difficulty (uint)The difficulty of the current block
block.gaslimit (uint)Gaslimit of the current block
block.number (uint)Block number of the current block
block.timestamp (uint)The timestamp of the current block as seconds since Unix epoch
gasleft() returns (uint256)Amount of gas left
msg.data (bytes calldata)Complete call data of block
msg.sender (address payable)The sender of message i.e. current caller
msg.sig (bytes4)First four bytes of call data i.e. function identifier
msg.value (uint)Amount of Wei sent with a message
now (uint)The timestamp of the current block
gasleft() returns (uint256)Amount of gas left
tx.gasprice (uint)Price of gas for the transaction
tx.origin (address payable)Transaction sender

Example: In the below example, the contact Test uses the msg.sender variable to access the address of the person deploying the contract.

// Solidity program to
// show Global variables
pragma solidity ^0.5.0;

// Creating a contract
contract Test {
	
// Defining a variable
address public admin;
	
// Creating a constructor to
// use Global variable
constructor() public {	
	admin = msg.sender;
}
}

Output:

Global variable