DEV Community

Cover image for Break Statement in Solidity
Shlok Kumar
Shlok Kumar

Posted on

Break Statement in Solidity

The break statement in Solidity allows for breaking out of a loop early, providing a convenient way to exit a loop anytime.

A "break" statement is a control flow statement used in programming languages to exit early from a loop or switch statement. The break statement terminates the nearest enclosing loop and transfers control to the line of code following the loop. In most programming languages, including C, C++, Java, and Python, the break statement can be used to exit both for and while loops.

The main purpose of the break statement is to allow the programmer to prematurely exit a loop and move on to the next line of code. This can be useful in situations where a certain condition has been met, such as finding a specific item in an array or reaching a certain iteration limit. By using the break statement, the programmer can avoid executing any unnecessary code and improve the overall efficiency of the program. Additionally, the break statement can also be used in switch statements to exit the current case and move on to the next case.
break
In Solidity, you can use the break statement within for, while, and do-while loops. This statement is commonly used in conjunction with an if statement to create a conditional break. For example, you could use an if statement to check if a specific condition has been met, and then use a break statement within the if block to exit the loop if the condition is true. In this way, you can control the flow of your loop and ensure that it only executes as many times as necessary to meet the conditions you have specified.

The break statement in Solidity is used to exit a loop early, breaking out of the enclosing curly braces. This statement is useful when you need to come out of a loop without reaching its bottom or start the next iteration of any loop. The flow chart of a break statement would look as follows: after the break statement, the control is passed to the statements present after the loop.

The break statement can be used to optimize code when removing an element from a storage array. Without it, one would have to do a linear search to find it first. However, by using the break statement, one can get more complicated and use binary search algorithms for faster searches.

The break statement can also be used in switch statements. It helps us terminate the switch by passing control to the first instruction after it. This allows us to handle different cases within a single switch block and exit them early if needed.

Example of a break statement:

pragma solidity ^0.8.0;

contract SolidityTest {
   uint storedData; 
   constructor() public{
      storedData = 10;   
   }
   function getResult() public returns(string memory){
      uint a = 1; 
      uint b = 2;
      uint result = a + b;
      return integerToString(result); 
   }
   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)