Recently, I have been revisiting Solidity, consolidating the finer details, and writing "WTF Solidity" tutorials for newbies.
Twitter: @0xAA_Science | @WTFAcademy_
Community: Discord|Wechat|Website wtf.academy
Codes and tutorials are open source on GitHub: github.com/AmazingAng/WTF-Solidity
In this section, we will introduce control flow in Solidity, and write an insertion sort (InsertionSort
), a program that looks simple but is actually bug-prone.
Control Flow
Solidity's control flow is similar to other languages, mainly including the following components:
if-else
for loop
while loop
do-while loop
- Conditional (
ternary
) operator
The ternary
operator is the only operator in Solidity that accepts three operands: a condition followed by a question mark (?
), then an expression x
to execute if the condition is true followed by a colon (:
), and finally the expression y
to execute if the condition is false: condition ? x : y
.
This operator is frequently used as an alternative to an if-else
statement.
In addition, there are continue
(immediately enter the next loop) and break
(break out of the current loop) keywords that can be used.
Solidity
Implementation of Insertion Sort
Note: Over 90% of people who write the insertion algorithm with Solidity will get it wrong on the first try.
Insertion Sort
The sorting algorithm solves the problem of arranging an unordered set of numbers from small to large, for example, sorting [2, 5, 3, 1]
to [1, 2, 3, 5]
. Insertion Sort (InsertionSort
) is the simplest and first sorting algorithm that most developers learn in their computer science class. The logic of InsertionSort
:
- from the beginning of the array
x
to the end, compare the elementx[i]
with the element in front of itx[i-1]
; ifx[i]
is smaller, switch their positions, compare it withx[i-2]
, and continue this process.
The schematic of insertion sort:

Python Implementation
Let's first look at the Python Implementation of the insertion sort:
Solidity Implementation (with Bug)
Python version of Insertion Sort takes up 9 lines. Let's rewrite it into Solidity by replacing functions
, variables
, and loops
with solidity syntax accordingly. It only takes up 9 lines of code:
But when we compile the modified version and try to sort [2, 5, 3, 1]
. BOOM! There are bugs! After 3-hour debugging, I still could not find where the bug was. I googled "Solidity insertion sort", and found that all the insertion algorithms written with Solidity are all wrong, such as Sorting in Solidity without Comparison
Errors occurred in Remix decoded output
:

Solidity Implementation (Correct)
With the help of a friend from Dapp-Learning
community, we finally found the problem. The most commonly used variable type in Solidity is uint
, which represents a non-negative integer. If it takes a negative value, we will encounter an underflow
error. In the above code, the variable j
will get -1
, causing the bug.
So, we need to add 1
to j
so it can never take a negative value. The correct insertion sort solidity code:
Result:
!["Input [2,5,3,1] Output[1,2,3,5]"](/_next/image?url=https%3A%2F%2Fimages.mirror-media.xyz%2Fpublication-images%2FS-i6rwCMeXoi8eNJ0fRdB.png%3Fheight%3D300%26width%3D554&w=3840&q=75)
Summary
In this lecture, we introduced control flow in Solidity and wrote a simple but bug-prone sorting algorithm. Solidity looks simple but has many traps. Every month, projects get hacked and lose millions of dollars because of small bugs in the smart contract. To write a safe contract, we need to master the basics of Solidity and keep practising.