if expression
An if expression
is a control flow construct that allow us to conditionally execute a piece of code depending on the value of a boolean condition. Since it is an expression, it returns the value of the last expression in the branch.
use debug::PrintTrait;
fn main() {
let is_awesome = true;
if is_awesome {
'Cairo is awesome!'.print();
}
}
You can combine several conditions using boolean operators like &&
(and), ||
(or), etc.
You can also add an optional else-branch like in many other programming languages.
use debug::PrintTrait;
fn main() {
let version: u8 = 2;
let is_awesome = true;
if is_awesome && version > 0 {
'Lets code!'.print();
} else {
'Great things are coming'.print();
}
}
Try it out!
- Install the toolchain:
- For macOS and Linux, run our script:
curl -sL https://raw.githubusercontent.com/lambdaclass/cairo-by-example/main/build/installer.sh | bash -s 2.2.0
- For Windows and others, please see the official guide
- Run the example:
- Copy the example into a if.cairo file and run with:
%!s(<nil>) if.cairo