1 of 17

Control Flow : if, elif, else

2 of 17

Kurnia Anwar Ra’if

A Highly-motivated Data Scientist

Senior Data & AI Platform - PT. Mastersystem Infotama Tbk

Data Scientist @ PT. KitaLulus International

Data Scientist @ PT. Sharing Vision– BRI Consultant

Software Engineering @ PT. AILIMA Geothermal

Mentor DS/BI/AI-ML - @ Dibimbing.id

3 of 17

Outline :

  • Comparison & logical operators
  • Conditional statements (if, elif, else)
  • Nested conditions
  • User input + logic combo
  • Age or Grade Classifier

4 of 17

  • Remainder

5 of 17

6 of 17

7 of 17

Control Flow

8 of 17

9 of 17

10 of 17

Conditional Statements (if, elif, else)

Summary :

- if: checks the first condition

- elif: checks additional conditions

- else: runs if none of the above conditions are true

These are essential for building decision-making logic in your Python programs.

The if statement checks whether a condition is True. If it is, the indented block of code under it will be executed.

elif (else if) Used to check another condition if the previous if condition was False. You can have multiple elif blocks.

The else block is executed when none of the previous conditions are True.

11 of 17

Hands on

12 of 17

Nested Conditions

Nested condition adalah conditional statement yang berada di dalam conditional statement lainnya.�Artinya, setelah suatu kondisi if bernilai True, di dalam blok tersebut kita bisa menambahkan kondisi if lagi untuk pengecekan tambahan.

Kapan digunakan nested condition?

  • Saat keputusan bersifat bertingkat.
  • Saat hasil dari kondisi pertama mempengaruhi logika kondisi kedua.
  • Contoh nyata:
    • Jika umur ≥ 18 → cek apakah sudah punya SIM atau belum.
    • Jika skor ≥ 75 → cek apakah lulus ujian praktikum atau tidak.

13 of 17

Nested Conditions

Perlu diingat:

  • Nested terlalu dalam bisa membuat kode sulit dibaca (deep nesting).
  • Bisa digabungkan dengan logical operator untuk mengurangi kedalaman nesting.

14 of 17

Hands on

15 of 17

Brief Assignment

16 of 17

Brief Assignment :

  1. Buat program untuk menentukan kategori BMI berdasarkan input berat (kg) dan tinggi (m), carilah referensi rentang nilai kategori BMI yang valid. Jawaban Harus memenuhi :
  2. if,elif,else
  3. Nested condition, logical operator & comparison

17 of 17

Thank you

https://www.linkedin.com/in/anwaraif/

kurniafreelancer@gmail.com