if class_room.is_empty then
class_room.close_windows
class_room.switch_lights_off
class_room.lock_door
.
if instructions
if then instruction
Example:
if then else instruction
Example:
variable message string
if the_sun_shines then
message = "Antonio is very happy."
else
message = "Antonio is happy."
.
Note that, as we’ve seen previously in chapter Expressions and operators, we can also use an if expression (instead of an if instruction):
const message = if the_sun_shines then "Antonio is very happy." else "Antonio is happy."
This is the preferred code, because the code is shorter and we use a constant instead of a variable.
if then else if instruction
Example:
const num_orders = ...
variable message string
if num_orders =v 0 then
message = "There are no orders."
else if num_orders =v 1 then
message = "There is one order."
else if num_orders <= 5 then
message = "There are 5 orders or less."
else
message = "There are more than 5 orders"
.