Rise In Logo

Build on Scroll

Control flow mechanisms in Solidity, like in any programming language, direct the execution of code. They are crucial for making decisions (`if-else` statements) and performing repetitive tasks (`for` and while loops). This lesson will explain these concepts in detail, focusing on how they can be effectively implemented in Solidity to manage complex logic within smart contracts.

Let’s start with some basics, shall we?

If-Else Statements

If-else statements allow a program to execute different parts of code based on certain conditions. These conditions are evaluated to either true or false, directing the flow of execution accordingly.

Let’s take a look at its syntax with an example.

function feeCalculator(uint amount) public pure returns (uint) {

    if (amount < 500) {

        return 10;  // Flat fee for small transactions

    } else if (amount < 2000) {

        return amount \* 2 / 100;  // 2% fee

    } else {

        return amount \* 15 / 1000;  // 1.5% fee for larger transactions


  • if (amount < 500): This is the first condition checked. If the amount is less than 500, the code within this block is executed. In this case, the function will return a flat fee of 10. After executing this block, the function ends and no further code is executed.
  • else if (amount < 2000): If the first condition is not met, the program checks this condition. If the amount is 500 or more but less than 2000, the code within this block is executed. The function calculates a fee of 2% of the amount and returns this value.
  • else: If neither the if nor the else if conditions are met, the code within the else block is executed. This means the amount is 2000 or more. The function calculates a fee of 1.5% of the amount and returns this value.

The if-else statement allows the function to execute different code blocks based on the value of amount, thus providing different behavior for different input ranges.

For Loops

For loops are used in Solidity to repeat a block of statements a specific number of times. They are typically used when the number of iterations is known before the loop starts.

Syntax and Example

function sumArray(uint\[\] memory data) public pure returns (uint sum) {

    for (uint i = 0; i &lt; data.length; i++) {

        sum += data\[i\];


    return sum;

  • uint i = 0;: This is the initialization part of the for loop. A variable i is declared and initialized to 0. This variable serves as the index for accessing elements in the array.
  • i < data.length;: This is the condition part of the for loop. The loop will continue to execute as long as i is less than the length of the array data. Once i is equal to or greater than data.length, the loop will stop.
  • i++: This is the increment part of the for loop. After each iteration of the loop, the value of i is increased by 1. This moves the index to the next element in the array.
  • sum += data[i];: Inside the loop, the ith element of the array data is added to the sum. This operation is repeated for each element in the array.

After the loop has executed for all elements in the array, the function returns the total sum.

While Loops

While loops continue executing a block of code as long as a specified condition remains true. They are useful when the number of iterations needed isn't known before the loop starts.

Syntax and Example

function decrement(uint initial) public pure returns (uint) {

    while (initial &gt; 0) {



    return initial;

  • while (initial > 0): This is the condition for the while loop. The loop will continue to execute as long as the initial variable is greater than 0.
  • initial--;: This is the code block that gets executed in each iteration of the loop. It decrements the initial variable by 1 in each iteration.

The loop will keep executing and decrementing the initial value until initial is no longer greater than 0. At that point, the loop ends and the program continues with any code following the loop.

In summary, this loop effectively counts down from the initial value to 0.

Best Practices and Considerations

  • Minimize Complexity: Avoid deeply nested loops and conditionals to keep the contract code simple and maintainable.
  • Guard Against Infinite Loops: Always ensure loops have a break condition to prevent infinite execution, which can consume all provided gas and cause transactions to fail.
  • Efficient Use of Gas: Especially with loops, be mindful of the gas costs associated with each iteration. Use conditions and breaks wisely to minimize execution where possible.

Using Break and Continue

In both for and while loops, you can control the flow further using break to exit the loop completely, or continue to skip the rest of the current loop iteration and begin the next one.

function findFirstEven(uint\[\] memory data) public pure returns (uint) {

    for (uint i = 0; i &lt; data.length; i++) {

        if (data\[i\] % 2 == 0) {

            return data\[i\];  // Return the first even number



    return 0;  // Return 0 if no even number is found


If you have used control flow and conditionals in any other language, as you can see Solidity is not much different on this subject.

You are doing great and we are almost there. We have just 2 more subjects. Let’s continue with error handling.




You need to enroll in the course to be able to comment!

Stay in the know

Never miss updates on new programs and opportunities.

Rise In Logo

Rise together in web3!