---
# System prepended metadata

title: Control Statements (Revamped)

---

# Control Statements

---
title: Agenda
description: 
duration: 300
card_type: cue_card
---

### Agenda

1. Control Statements
2. Flowchart
3. Syntax
4. Indentation
5. `if` Block
6. `if-else` Block
7. `elif` Block

---
title: Control Statements & Flowcharts
description: 
duration: 2000
card_type: cue_card
---

### **Control Statements**

1. `if`
2. `if else`
3. `elif`

- To help the computer make some decisions based on the specified condition(s).

### **Flow Charts**
- Represents the decision making process.
- Determine what should be done based on the condition(s).

#### **Example 1** - What to wear?

<img src = https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/059/882/original/flow1.png?1703144870 height = 300 width = 600>

<img src = https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/059/883/original/flow2.png?1703144935 height = 200 width = 600>

#### **Example 2** - Greeting program

 * 9am-12pm - good morning
 * 12pm-5pm - good afternoon
 * 5pm-8pm - good evening
 * 8pm onwards - good night

<img src = https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/059/884/original/flow3.png?1703145031 height = 500 width = 600>

---
title: IF block
description: 
duration: 2000
card_type: cue_card
---

## IF block

**Pseudo code**
```python=
if condition:
  print(something)
  a = True
```

<img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/039/991/original/download_%289%29.png?1689583498" width=600 height=350>

<img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/039/992/original/download_%2810%29.png?1689583529" width=600 height=275>

### Indentation

- Python uses indentation over curly braces.
- Everything with the same indent becomes part of one block.

Code:
``` python=
#one indent- 4spaces/2spaces
if 56 > 50:
  print("1")
  print("2") #1 2
```
>Output
```
1
2
```

Code:
``` python=
if 56 > 50:
  print("1")
  print("2")
print("3") # 1 2 3
```
>Output

```
1
2
3
```

Code:
``` python=
if 5 > 50:
  print("1")
  print("2")
print("3")
```
>Output
```
3
```

---
title: IF-ELSE block
description: 
duration: 2000
card_type: cue_card
---

## IF-ELSE block

**Pseudo code:**
``` python=
if True:
  #Run these statements
else:
  #Run these statements
#Run these always
```

Code:
``` python=
a=2
result = a ** 5
if result > 25:
  print("That is massive!")
else:
  print("pretty small!")
print("complete")
```
>Output
```
That is massive!
complete
```

#### Flowchart for the problem above -

<img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/039/993/original/download_%2811%29.png?1689583559" width=600 height=450>

#### Question 1: Check Password

-   print `"password incorrect"` if **incorrect**.
-   print `"password correct"` if **correct**.

Code:
``` python=
original_password="password"
password=input("Please enter password: ")
if (original_password == password):
  print("password correct")
else:
  print("password incorrect")
```

>Output
```
Please enter password: adad
password incorrect
```

#### Question 2: Greeting in 24hr format

Code:
``` python=
time = int(input())
if time > 9 and time <= 12:
  print("Good morning")
else:
  if time > 12 and time <= 17:
    print("Good afternoon")
  else:
    if(time > 17 and time <= 24):
      print("Good night")
    else:
      print("SO JAO!")
```

>Output 
```
1
SO JAO!
```

#### Question 3: ATM Dispatch

* Only Rs. 500 and Rs. 200 notes are available.
* Take an amount as input from user
  * if amount is dischargeable, `print("Discharging cash")`
  * else, `print("Denomination not available")`

Code:
``` python=
amount = int(input('Enter amount: '))
if (amount%500)%200 == 0 :
  print('Discharging cash')
else:
  print('Denomination not available')
```

>Output 1
```
Enter amount: 500
Discharging cash
```

- But there's a problem with this solution.
- This code checks if the remainder amount after dispensing Rs. 500 notes can be dispensed in Rs. 200 notes.
- But if we enter an amount which can be dispensed using only Rs. 200 notes, it fails to give the desired output.

>Output 2
```
Enter amount: 600
Denomination not available
```

Code:
``` python=
amount=int(input('Enter amount: '))
if amount%500==0 or amount%200==0:
  print("Discharging cash")
else:
  print("Denomination not available")
```

>Output
```
Enter amount: 1700
Denomination not available
```

- But there's a problem with this solution as well.
- This code checks whether the entered amount can be dispensed using either Rs. 500 notes or Rs. 200 notes.
- But if we enter an amount which can be dispensed using a combination of both the notes (e.g. 1700), it fails to give the desired output.

We will see how to solve the problems in these approches in a few minutes.

---
title: Break & Doubt Resolution
description:
duration: 600
card_type: cue_card
---

### Break & Doubt Resolution

`Instructor Note:`
* Kindly take this time (up to 5-10 mins) to give a short break to the learners.
* Meanwhile, you can ask the them to share their doubts (if any) regarding the topics covered so far.

---
title: Quiz-1
description: Quiz-1
duration: 60
card_type: quiz_card
---

# Question

What is the output of the following code?

``` python=
x = 5
if x > 10:
    print("Hello")
else:
    print("World")
```

# Choices
- [ ] Hello
- [x] World
- [ ] Error

---
title: Quiz-2
description: Quiz-2
duration: 60
card_type: quiz_card
---

# Question

What is the output of the following code?

``` python=
x = 0
if x:
    print("True")
else:
    print("False")
```
# Choices

- [ ] True
- [x] False
- [ ] Error

---
title: ELIF block
description: 
duration: 2000
card_type: cue_card
---

## ELIF block

**Pseudo code:**
```python=
if condition1:
  this
elif condition2:
  this
else:
  this
```

#### Greeting in 24hr format -

Code:
``` python=
time = int(input())
if time > 9 and time <= 12:
  print("Good morning")
elif time > 12 and time <= 17:
  print("Good afternoon")
elif (time > 17 and time <= 24):
  print("Good night")
else:
  print("SO JAO!")
```

>Output
```
10
Good morning
```

#### Solution for the ATM Dispatch problem -

Code:
```python=
amount = int(input('Enter amount: '))
if amount <= 500 and (amount%500)%200 == 0:
  print('Discharging cash')
elif amount > 500 and ((amount%500)%200)%100 == 0:
  print('Discharging cash')
else:
  print('Denomination not available')
```

---
title: Quiz-3
description: Quiz-3
duration: 60
card_type: quiz_card
---

# Question

What is the output of the following code?

``` python=
x = 5
if x > 2:
    if x < 10:
        print("x is between 2 and 10")
    else:
        print("x is greater than or equal to 10")
else:
    print("x is less than or equal to 2")
```

# Choices

- [x] x is between 2 and 10
- [ ] x is greater than or equal to 10
- [ ] x is less than or equal to 2

---
title: Quiz-4
description: Quiz-4
duration: 60
card_type: quiz_card
---

# Question

What is the output of the following code snippet?

``` python=
age = 15
if age < 18:
    print("You are not eligible to vote")
elif age >= 18 and age < 21:
    print("You can vote but can't drink")
else:
    print("You can vote and drink")
```

# Choices

- [x] You are not eligible to vote
- [ ] You can vote but can't drink
- [ ] You can vote and drink

---
title: Practice Coding Question(s)
description: 
duration: 600
card_type: cue_card
---

### Practice Coding Question(s)

You can pick the following question and solve it during the lecture itself.

This will help the learners to get familiar with the problem solving process and motivate them to solve the assignments.

<span style="background-color: pink;">Make sure to start the doubt session before you start solving the question.</span>

Q. https://www.scaler.com/hire/test/problem/96776/
