Sui Move Language - Control Flow
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.
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 }
if-else: A regular if - else conditions to check for a particular case.
if (condition) { // if block code } else { // else block code }
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 }
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.
Subscribe to my newsletter
Read articles from Jay Nalam directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Jay Nalam
Jay Nalam
Hi, I'm Jay Nalam, a seasoned Web3 Engineer committed to advancing decentralized technologies. Specializing in EVM-based blockchains, smart contracts, and web3 protocols, I've developed NFTs, DeFi protocols, and more, pushing boundaries in the crypto realm.