DEV Community

Cover image for The Continue Statement in Solidity
Shlok Kumar
Shlok Kumar

Posted on

The Continue Statement in Solidity

The continue statement in Solidity skips the current iteration of a loop and jumps to the next iteration, providing more control over loop execution.

The continue statement is a common control flow structure used in programming languages. It is used to skip the current iteration of a loop and move on to the next iteration. The continue statement is typically used inside loops, such as for and while loops. The purpose of using the continue statement is to allow a programmer to control the flow of execution and make decisions based on certain conditions within the loop.

When a continue statement is encountered, the current iteration of the loop is skipped and the next iteration begins immediately. This is useful when certain conditions are met within the loop and you want to skip certain iterations without completely exiting the loop. For example, you might use a continue statement to skip processing data that is not valid or to continue processing data that is valid. This can help you to optimize your code and improve its performance. Additionally, it can also help to simplify your code and make it easier to understand and maintain.

The continue statement in Solidity is used to skip the remaining code block and start the next iteration of a loop. This statement is useful when you need to come out of a loop without reaching its bottom or when you want to start the next iteration of any loop.

The syntax for using the continue statement is as follows: "continue;". This statement tells the interpreter to immediately start the next iteration of the loop and skip the remaining code block. After executing the continue statement, control is transferred to the beginning of the loop.

An example of using a continue statement with a while loop can be found in Solidity Programming Essentials:

while (i < 10) {
    if (i == 5) 
        {i++;continue;}
        // rest of code here...i++;
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, when i reach 5, it will increase by 1 and then skip over any remaining code in that iteration and move on to the next one. This allows for more efficient use of loops in Solidity programming.

By using the continue statement, you can instruct the interpreter to begin the next iteration of the loop right away and bypass the remaining code block. Whenever a continue statement is met, the program flow jumps directly to the loop check expression, and if the condition stays true, the program flow continues to the next iteration; otherwise, the control is released from the loop and the program ends.

Example of a continue statement:

pragma solidity ^0.8.0;

contract SolidityTest {
   uint storedData; 
   constructor() public{
      storedData = 10;   
   }
   function getResult() public returns(string memory){
      uint n = 1;
      uint sum = 0;
      
      while( n < 10){
         n++;
         if(n == 5){
            continue; // skip n in sum when it is 5.
         }
         sum = sum + n;
      }
      return integerToString(sum); 
   }
   function integerToString(uint i) public
      returns (string memory) {
      
      if (i == 0) {
         return "0";
      }
      uint j = i;
      uint len;
      
      while (true) {
         len++;
         j /= 10;
         if(j==0){
            break;   //using break statement
         }
      }
      bytes memory bstr = new bytes(len);
      uint k = len - 1;
      
      while (i != 0) {
         bstr[k--] = byte(uint8(48 + i % 10));
         i /= 10;
      }
      return string(bstr);
   }
} 
Enter fullscreen mode Exit fullscreen mode

For more content, follow me on - https://linktr.ee/shlokkumar2303

Top comments (0)