Member-only story
Solidity Tutorial: All About Conditionals
Understand through Smart contract examples
Everyone writing code on a daily basis knows what a if / else
statement is. But still, since we are in the “All About Solidity” article series, we’ll cover everything about the Solidity smart contract language!
No bent rules! We cannot omit conditionals!
In today’s article, we will still cover the basics of conditionals in Solidity, and some of their variants.
But instead of re-explaining if
statements all over again from scratch, we will the Solidity code of 4 very popular smart contract projects: CryptoKitties, BarnBridge, Sablier, and LUKSO.
So we can as well learn and understand how some popular smart contracts work and increase our curiosity.
Table of Contents
OverviewExample 1 — CryptoKitties
Basic if statement (one condition)
Either one condition or another with ||
Use a variable as a conditionExample 2 — BarnBridge
Concatenating multiple conditions with &&
if { … } else { … }Example 3 — Sablier
One line if statements
What else can be written inside if statements in Solidity?Example 4 — LUKSO
omitting curly bracesFinal Notes on Solidity conditionalsReferences
Overview
Conditionals in Solidity follow the basic constructs of other programming languages:
- a condition is wrapped inside the brackets of an
if
statement. - if the condition evaluates to
true
, the code inside the curly brackets runs. - if the condition evaluates to
false
, the program execution jumps to the next statement after the closing bracket}
.
Nothing new here! But let’s see in practice with some real Solidity code currently running on mainnet!