(control-flow)=
# 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-else)=
## `if-else` Syntax

```csharp
if (question1)
  action1;
else if (question2)
  action2;
else
  action3;
```

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:
```csharp
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 end with a semicolon.
- curly braces `{}` group 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`
- `a + b`
- `temperature > 30`

Not expressions but statements:
- `temperature = 10;`
- `if (temperature > 30) Console.WriteLine("Hot");`

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

1. `air_conditioner = 1;`
1. `if (...) else ...`
1. `a > b`
1. `5`
1. `success = a > b;`
 
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
:::

Example:

```csharp
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:

:::{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:
```csharp
int Control(int altitude)
{
    int thruster = 0;
    
    // YOUR CODE HERE
    
    return thruster;
}

void Test(int altitude)
{
    int thruster = Control(altitude);
    bool behaviorCorrect = (altitude > 100 && thruster == 0) ||
                           (altitude is <= 100 and > 0 && thruster == 1) ||
                           (altitude <= 0 && thruster == 0);
    var behaviorCorrectIcon = behaviorCorrect ? "✅" : "❌";
    Console.WriteLine($"For altitude {altitude}, your thruster is {thruster} |{behaviorCorrectIcon}|");
}

Test(150);
Test(100);
Test(50);
Test(0);
Test(-1);
```

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;
```
:::


::::

