---
# System prepended metadata

title: 'Week 9: Control flow with IF/ELIF/ELSE and MATCH/CASE in Python'

---

### Week 9: Control flow with IF/ELIF/ELSE and MATCH/CASE in Python
> By *Akinlade Temitope Victory*

The week was basically about control flow and several task based on the topic. 

The tutor started off by explaining the concept as a logic that controls the flow of a program base on certain conditions and examples of this are the if/elif/else conditions and match/case. 

#### IF/ELIF/ELSE

These are traditional conditionals used to execute code based on whether a condition is True
#### IF

The "if" condition runs a block of code that is nested with four clicks of the space bar or just a click of that tab key only when the condition evaluates to True and does nothing when it's False. 

Example:
```python!
x = 10
if x > 0:
    print(f"{x} is greater") # Output: 10 is greater: Evaluation is True
    
if x < 0:
    print(f"{x} is lesser") # Output: No output: Evaluation is False, 10 is greater than 0
```

You can check multiple conditions in an "if" block by using logical operators. 

Example:
```python!
age = 18
entry_fee = 1500
if age >= 18 and entry_fee == 1500:
    print("welcome to the show")    # Output: welcome to the show
```

#### ELIF
The "elif" condition(else if) does same thing as the "if" condition including using logical operators to evaluate multiple conditions., the only difference is it only runs when the "if" block before it evaluates to False and a control flow never starts with it.

Example:
```python!
x = 7
if x < 7:
    print(f"{x} is lesser than 7")
elif x == 7:
    print(f"{x} is equal to 7")    # Output: 7 is equal to 7: Evaluation of the "if" block was False hence the "elif" block which evaluated to True 
```

#### ELSE
The "else" block only runs when all conditions before it evaluated to False and it does not take conditions as the default condition for it is to only execute when all other condition/conditions evaluate to False

Example:
```python!
x = 0
if x > 10:
    print(f"{x} is greater than 10")
elif x > 5:
    print(f"{x} is greater than 5")
else:
    print(f"{x} is not greater than 10 and 5")    # Output: 0 is not greater than 10 and 5: All other conditions before it was False hence the execution of the "else" block
```

#### MATCH/CASE
This is a newer feature added in Python 3.10. It simply lets you match complex data structures like a list,tuple, dictionary e.t.c.

Example:

```python!
operation = ("+",4,1)    # Data type --- tuple
match operation:
    case ("-",a,b):
        print(4-1)
    case ("*",a,b):
        print(4*1):
    case ("+",a,b):    # Output: 5
        print(4+1)
    case _:
        print("No match")
```
The above example uses cases to match a pattern by destructuring the tuple and assigning matched values to variables and if no match, the last case will be executed as it works as "else" here.

### CONCLUSION 
Working with control flows was quite tricky at first in the beginning of the week but finding solution to more problems with control flow made the concept clearer. Thanks for sticking around this far. Until next time!
