In any programming language, Control statements are used to change the execution of the program. Solidity allows us to handle loops and switch statements. These statements are usually used when we have to terminate the loop without reaching the bottom or we have to skip some part of the block of code and start the new iteration. Solidity provides the following control statements to handle the program execution.
Break Statement
This statement is used when we have to terminate the loop or switch statements or some block of code in which it is present. After the break statement, the control is passed to the statements present after the break. In nested loops, break statement terminates only those loops which has break statement.
Example: In the below example, the contract Types is defined consisting of a dynamic array, an unsigned integer variable, and a function containing the while loop to demonstrate the working of the break statement.
Solidity
// Solidity program to demonstrate // use of Break statement pragma solidity ^0.5.0; // Creating a contract contract Types { // Declaring a dynamic array uint[] data; // Declaring the state variable uint8 j = 0; // Defining the function to // demonstrate use of Break // statement function loop( ) public returns(uint[] memory){ while(j < 5) { j++; if(j==3){ break; } data.push(j); } return data; } } |
Output :

Continue Statement
This statement is used when we have to skip the remaining block of code and start the next iteration of the loop immediately. After executing the continue statement, the control is transferred to the loop check condition, and if the condition is true the next iteration starts.
Example: In the below example, contract Types is defined consisting of a dynamic array, an unsigned integer variable, and a function containing the while loop to demonstrate the working of the continue statement.
Solidity
// Solidity program to demonstrate // use of Continue statement pragma solidity ^0.5.0; // Creating a contract contract Types { // Declaring a dynamic array uint[] data; // Declaring a state variable uint8 j = 0; // Defining a function to // demonstrate the use of // Continue statement function loop( ) public returns(uint[] memory){ while(j < 5) { j++; if(j==3){ continue; } data.push(j); } return data; } } |
Output :

Recommended Posts:
- Solidity - Decision Making Statements
- Solidity - View and Pure Functions
- Solidity - While, Do-While, and For Loop
- Solidity - Enums and Structs
- Creating a Smart Contract that Returns Address and Balance of Owner using Solidity
- Solidity - Types
- Solidity - Functions
- What are Events in Solidity?
- Solidity - Inheritance
- Solidity - Polymorphism
- Solidity - Encapsulation
- Solidity - Abstract Contract
- How to Install Solidity in Windows?
- Solidity - Variables
- Solidity - Error Handling
- Steps to Execute Solidity Smart Contract using Remix IDE
- Solidity - Arrays
- Solidity - Libraries
- Introduction to Solidity
- Solidity - Constructors
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

