Contents
What are Loops in Programming?
In the programming language, loops are constructs that let you execute a block of code multiple times till certain conditions remain true. When the condition does not remain true the execution comes out of the loop. It is very important to keep in mind that there has to be some condition that can terminate the loop, otherwise, it will enter into an infinite loop and your code will crash.
Loop in Solidity
In Solidity, there are three types of loop statements as follows –
- While Loop
- Do While Loop
- For Loop
Let us see these different types of loops in more detail.
While Loop – Solidity
In the while loop, the code inside the block is executed multiple times in a loop till the condition remains true. Once the terminating condition is reached it comes out of the loop. It should however be noted that the execution may not even enter the while loop if the condition is evaluated to be false at the very start.
Below is the syntax of the while Loop in Solidity.
while (conditions) { //code to be executed till condition is true }
Example
In the below example, the while loop is executed till the value of ‘i’ does not become equal to 5. In each loop, the value of ‘i’ is increment by 1 so that the terminating condition is reached.
// SPDX-License-Identifier: MIT pragma solidity ^0.8.10; contract DemoContract { uint storedData; function set(uint x) public { storedData = x; uint i = 0; while(i != 5) { storedData = storedData + i; i++; } } function get() public view returns (uint) { return storedData; } }
Do While Loop – Solidity
In do while loop, the condition is checked at the end of the loop in every iteration. This means that the loop is executed at least once even if the condition is not true at the start and it is in contrast to the while loop we discussed above.
Below is the syntax of the do while Loop in Solidity.
do { //code to be executed till condition is true } while (conditions);
Example
In this example, the do while loop is executed till the value of ‘i’ becomes 5. The condition is checked at the end of each loop.
// SPDX-License-Identifier: MIT pragma solidity ^0.8.10; contract DemoContract { uint storedData; function set(uint x) public { storedData = x; uint i = 0; do { storedData = storedData + i; i++; } while(i != 5); } function get() public view returns (uint) { return storedData; } }
For Loop – Solidity
In this style, the initialization of the counter, testing condition, and increment/decrement of the counter are all written in a single line inside for loop. While the test condition remains true the code inside for loop is executed in iteration.
Below is the syntax of the for Loop in Solidity.
for (initialize counter; test condition; update counter) { //code to be executed till condition is true }
Example
In this example, the for loop increments the value of storedData till the value of ‘i’ reaches 5.
// SPDX-License-Identifier: MIT pragma solidity ^0.8.10; contract DemoContract { uint storedData; function set(uint x) public { storedData = x; uint i; for (i=0; i !=5; i++) { storedData = storedData + i; } } function get() public view returns (uint) { return storedData; } }