# Control flow

:::{card} Learning goals
- apply control flow syntax to implement a program
:::

:::{wpd} control flow
the order in which individual statements are executed
:::

(if)=
## `if`

::::{grid} 2
:::{grid-item-card} syntax
```c
if (question1)
  action1;
else if (question2)
  action2;
else
  action3;
```
:::
:::{grid-item-card} flowchart
```{mermaid} ../img/if-else-if-else-labeled.mmd
```
:::
::::

Question examples:
- *is the time 1?*
  - `time == 1`
- *is the temperature lower or equal 20?*
  - `temperature <= 20`

Action examples:
- *set the speed to 10*
  - `speed = 10`


## Example

::::{grid} 2
:::{grid-item-card}
In words:

- assume everything is turned off, i.e., `0`
- if `temperature` is greater than 30 then activate `air_conditioner`
- else if `temperature` greater than 25 activate `ventilator`
- else turn everything off.
:::
:::{grid-item-card}
In code:
```c
if (temperature > 30)
  air_conditioner = 1;
else if (temperature > 25)
  ventilator = 1;
else {  // ️⚠️ Use parantheses if you have multiple actions 
  air_conditioner = 0;
  ventilator = 0;
}
```
:::
::::

## Terms

`if-else-if-else` chain and actions (e.g., `a = 0`) are statements.

:::{wpd} statement
:id: statement_(computer_science)
a syntactic unit of a programming language that expresses some action to be carried out
:::

C statements usually end with a semicolon. Example exception: if we have a *compound statement*

:::{wpd} compound statement
:id: Statement_(computer_science)#Compound_statements
a statement that contains sequences of statements
:::

Questions must be expressions.

:::{wpd} expression
:id: expression_(computer_science)
syntactic entity in a programming language that may be evaluated to determine its value 
:::

Other expression examples:
- `42`{l=c}
- `a + b`{l=c}
- `temperature > 30`{l=c}

Not expressions but statements:
- `temperature = 10;`{l=c}
- `if (temperature > 30) printf("Hot");`{l=c}

:::{activity}
:label: which-are-statements
Which of the below are statements?

1. `air_conditioner = 1;`{l=c}
1. `if (...) else ...`{l=c}
1. `a > b`{l=c}
1. `5`{l=c}
1. `success = a > b;`{l=c}
 
Process (1 min):
1. think individually
2. answer the poll in the virtual classroom
:::


We have to adhere to this *syntax*, otherwise we get a *syntax error*.

:::{wpd} syntax error
a mismatch in the syntax
:::

Can you spot the syntax error in the following example?

```c
if temperature > 30
  air_conditioner = 1;
```

(lunar-lander-control)=
## Lunar lander control

::::{activity}
:label: lunar-lander-control-code
Write code for landing using the following flowchart:

<!--this is an activity diagram, not flowchart-->
:::{plantuml}
:align: center

@startuml
start

if (is altitude > 100?) then (yes)
  #pink:thruster off;
elseif (is altitude > 0?) then (yes)
  #palegreen:thruster on;
else (no)
  #pink:thruster off;
endif

stop
@enduml
:::

Copy the following template into your IDE and work on it:
:::{literalinclude} ../code/masked/lunar_lander_control.c
:language: c
:::

Steps:
1. Work ~2 min individually. The program automatically tests your code.
2. Compare your result with your neighbor and help them.

:::{dropdown} Confused 😕 and want some clues? Then click here. 

Use the following *independent* lines:

```
else
else if (altitude > 0)
if (altitude > 0)
thruster = 1;
thruster = 0;
thruster = 0;
```
:::
::::