Sui Move Language - Control Flow

Sui Move Language - Control Flow

Control Flow - if else, loops, labels

·

3 min read

Hello everyone..

Welcome to another day of exploring Web3 Engineering.

In this series, we are learning the Move smart contracts for the Sui blockchain network. In our previous article, we had a look into the datatypes, variable declarations on move language. Today, let us check out the conditional execution using if-else statements, loops and labels. So, without any further ado, let's get started..

Conditionals: if - else

Just like any other programming language, move also contains if-else statements which can be used in 4 different ways.

  1. Multiple If conditions: A chain of if conditions to verify the input and execute all the matching conditions.

     if(conditon1) {
         // code 1
     }
     if(condition2) {
         // code 2
     }
     if(condition3) {
         // code 3
     }
    
  2. if-else: A regular if - else conditions to check for a particular case.

     if (condition) {
         // if block code
     } else {
         // else block code
     }
    
  3. else-if ladder: A ladder of if-else statements for checking multiple conditions in a serial fashion.

     if(conditon1) {
         // code 1
     } else if(condition2) {
         // code 2
     } else if(condition3) {
         // code 3
     } else {
         // code 4
     }
    
  4. nested if: A nested tree structure of if else conditions.

     if(condition1) {
         if(condition2) {
             // code 1
         } else {
             // code 2
         }
     } else {
         // code 3
     }
    

Loops

Move provides while and loop statements for iterating over values. And also has break and continue statements to alter the flow of the execution. Let us look into them.

Using while

The syntax of the while loop is as follows:

while (condition) {
    // execution code block
}

The execution is executed repeatedly till the condition in the while block evaluates to false. The move compiler doesn't have any checks for the infinite loops. So, it is up to the developer to test such situations so that users don't waste their gas while interacting with the smart contracts.

Using break and continue

The break can continue can be used as followed inside of a while loop.

let i = 0
while (i <10) {
    if(i == 5)
        continue;
    if(i == 8)
        break;
    i += 2;
}

Using loop

The loop runs the given block of code infinite times until it encounters a break statement. If a break is missed, then it turns into an infinite loop. The syntax of the loop is as follows:

 loop {
       i = i + 1;
       if (i >= n) break;
       sum = sum + i;
};

Labels

The control flow of the execution can be altered by using Labels as well. Using labels, we can make the execution jump from one statement to another statement. Labels are declare by preceding the labelled block with 'label_name: The syntax of labels is as follows:

'increment: block_of_code
'outer: loop {
    ...
    'inner: while (cond) {
        ...
        if (cond0) { break 'outer value };
        ...
        if (cond1) { continue 'inner }
        else if (cond2) { continue 'outer };
        ...
    }
    ...
}

In the next article, I will cover about the function declaration, types of functions etc., provided by the move language.