Chapter 7 : Conditionals
[DROID-1242] INVALID CONDITIONAL INSTRUCTIONS. ERR %%$7834[[{23e3}]] PLEASE SPECIFY CONDITIONAL INSTRUCTIONS.
Booleans
Booleans are typed bool in LIGO :
const a: bool = true // or false
Comparing Values
Only values of the same type can be natively compared, i.e. int, nat, string, tez, timestamp, address, etc… However some values of the same type are not natively comparable, i.e. maps, sets or lists. You will have to write your own comparison functions for those.
// Comparing Strings
const a : string = “Alice”
const b : string = “Alice”
const c : bool = (a = b) // true
// Comparing numbers
const a : int = 5
const b : int = 4
const c : bool = (a = b) // false
const g : bool = (a >= b) // true
const h : bool = (a =/= b) // true
// Comparing tez
const a : tez = 5mutez
const b : tez = 10mutez
const c : bool = (a = b) // false
⚠️ Notice that equality is checked with a single = and not two like many languages.
⚠️ Also notice the use of =/= for the inequality operator.
Conditionals
Conditional logic enables forking the control flow depending on the state.
function isSmall (const n : nat) : bool is
if n < 10n then true else false
⚠️ When the branches of the conditional are not a single expression, as above, we need a block:
if x < y then
block {
x := x + 1;
y := y – 1
}
else skip;
Your mission
We want to conditionally change the engine attribute (third number) to 1 only if it is equal to 0.
1- Refactor modified_ship as a variable equal to my_ship
2- Then define a condition if the engine attribute equal 0. Don’t forget the attributes are defined as strings.
3- If the condition is met, change modified_ship to it new value. Otherwise, skip the instructions.
⚠️ If you have installed LIGO then you can test the execution of the modify_ship function by running the following command:
ligo run-function main.ligo modify_ship ‘(“010433”)’
AWAITING VALIDATION
Type your solution above and validate your answer