1.8 KiB
1.8 KiB
Not every building has an infinit amount of floors. Our simulated building is supposed to have 10. So it makes no sense to move to a floor higher than 10. To encode that kind of behavior we need a new language feature. The if-statement.
Knowledge
The if statement allows you to check, if a certain condition is met and only execute parts of your code if it is.
if(condition)
{
// gets executed, if condition is true
}
else
{
// gets executed, if condition is false
}
The else part is optional.
To formulate a condition you need new operators.
Operator | Meaning |
---|---|
== | True, when both sides are equal |
!= | True, when both side are not equal |
< | True, when left is less than right |
> | True, when left is bigger than right |
<= | True, when left is less than or equal to the right |
>= | True, when left is bigger or equal to the right |
&& | Only true, if both conditions are true |
|| | True, when one condition is true |
! | negates the condition |
Model
Our model now has a top floor, which is safed in the conveniently named variable TopFloor
.
To represent that in the code you need to add a check if we already reached the top and react accordingly.
Task
Your task is to check if the lift reached the top floor and change the direction to downwards.