icp/oer/courses/c-newcomers/sections/03-lift/03-smaller-building/content.md

72 lines
1.8 KiB
Markdown

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 <b>if-statement</b>.
# 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.
<table>
<tr>
<th>Operator</th>
<th>Meaning</th>
</tr>
<tr>
<th>==</th>
<th>True, when both sides are equal</th>
</tr>
<tr>
<th>!=</th>
<th>True, when both side are not equal</th>
</tr>
<tr>
<th>&lt;</th>
<th>True, when left is less than right</th>
</tr>
<tr>
<th>&gt;</th>
<th>True, when left is bigger than right</th>
</tr>
<tr>
<th>&lt;=</th>
<th>True, when left is less than or equal to the right</th>
</tr>
<tr>
<th>&gt;=</th>
<th>True, when left is bigger or equal to the right</th>
</tr>
<tr>
<th>&&</th>
<th>Only true, if both conditions are true</th>
</tr>
<tr>
<th>||</th>
<th>True, when one condition is true</th>
</tr>
<tr>
<th>!</th>
<th>negates the condition</th>
</tr>
</table>
## 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.