Loading...
While writing a contract, you may encounter a situation where you need to perform an action over and over again.
They have different types of Loops:-
1. While Loop
The most basic loop in solidity is the while loop.
2. do...while loop
The do...while loop is similar to the while loop except that the condition check happens at the end of the loop.
3. For Loop
The for loop is the most compact form of looping. It includes the following three important parts.
4. Loop Control
Solidity provides full control to handle loops and switch statements.
Solidity supports conditional statements which are used to perform different actions based on different conditions.
1. if statement
The if statement is the fundamental control statement that allows solidity to make decisions and execute statements conditionally.
2. if...else statement
The 'if...else' statement is the next form of control statement that allows Solidity to execute statements in a more controlled way.
The if...else if... statement is an advanced form of if...else that allows Solidity to make a correct decision out of several conditions.
Solidity supports string literal using both double quote (") and single quote ('). It provides a string as a data type to declare a variable of type String.
pragma solidity ^0.8.0;
contract TestString{
string data = "test";
}
In the above example, "test" is s string literal and data is a string variable.
The array is a data structure, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data.
In Solidity, an array can be a compile-time fixed size or dynamic size.
Declaring Arrays
To declare an array of fixed size in Solidity, the programmer specifies the type of the elements and the number of elements required by an array.
type arrayName [ arraySize];
Let take an example they have one variable they have contains the same data type unit and they store 10 different values.
uint balance[10];
If you want to declare dynamic size array in Solidity-
type[] arrayName;
You can initialize Solidity array elements either one by one or using a single statement-
uint balance[3] = [1,2,3];
Dynamic memory arrays are created using the new keywords.
uint size = 3;
uint balance[] = new uint[](size);
Members
Example
pragma solidity ^0.8.0;
contract Test {
function testArray() public pure{
uint len = 7;
//dynamic array
uint[] memory a = new uint[](7);
//bytes is same as byte[]
bytes memory b = new bytes(len);
assert(a.length == 7);
assert(b.length == len);
//access array variable
a[6] = 8;
//test array variable
assert(a[6] == 8);
//static array
uint[3] memory c = [uint(1) , 2, 3];
assert(c.length == 3);
}
}
Struct types are used to represent a record. Suppose you want to keep track of your books in a library.
Example:-
struct Book{
string title;
string author;
uint book_id;
}
Accessing a Struct and its variable
To access any member of a structure, we use the member access operator (.). The member access operator is coded as a period between the structure variable name and the structure member that we wish to access.
Example:-
pragma solidity ^0.8.0;
contract Test{
struct Book{
string title;
string author;
unit book_id;
}
Book book;
function setBook() public{
book = Book("Learn Solidity", "Anurag", 1);
}
function getBookId() public view returns(unit){
return book.book_id;
}
}
Output:-
uint256: 1
Mapping is a reference type as arrays and structs. The syntax to declare a mapping type.
mapping(_keyType => _valueType)
_KeyType − can be any built-in types plus bytes and string. No reference type or complex objects are allowed.
_ValueType − can be any type.
Example:-
pragma solidity ^0.8.0;
contract Test{
mapping(address => unit) balances;
function updateBalance(uint newBalance) public{
balances[msg.sender] = newBalance;
}
}
contract Test2{
function updateBalance() public returns(uint){
Test test = new Test();
test.updateBalance(10);
returns test.balances(address(this));
}
}
Output:-
{
"0": "uint256: 10"
}
Special variables are globally available variables and provide information about the blockchain.
blockhash(uint blockNumber) returns (bytes32)
Hash of the given block - only works for 256 most recent, excluding current, blocks.
block.coinbase (address payable)
Current block miner's address.
block.difficulty (uint)
current block difficulty.
block.gaslimit (uint)
Current block gaslimit.
block.number (uint)
Current block number.
block.timestamp
Current block timestamp as seconds since unix epoch.
gasleft() returns (uint256)
Remaining gas.
msg.data (bytes calldata)
Complete calldata.
msg.sender (address payable)
Sender of the message (current call).
msg.value (uint)
Number of wei sent with the message.
tx.gasprice (uint)
Gas price of the transaction.
now (uint)
Current block timestamp (alias for block.timestamp).