Intro (Control Flow Statment)

Conditionals: Mengontrol alur eksekusi berdasarkan kondisi tertentu dengan if, elif, dan else.

Loops: Digunakan untuk mengulang kode; for digunakan untuk iterasi elemen dalam urutan, dan while digunakan selama kondisi masih terpenuhi.

Comprehensions: Memberikan cara yang lebih efisien dan singkat untuk membuat koleksi seperti list, set, atau dictionary, menggunakan satu baris kode.

Untuk versi 3.11 ke atas...

Control Flow Statements

The flow control statements are divided into three categories

  1. Conditional statements

  2. Iterative statements.

  3. Transfer statements


1. Conditional Statements

These statements are used to execute a block of code based on a condition.

Types of Conditional Statements:

  • if

  • if-else

  • if-elif-else

  • Nested if

Example:

nilai = 85

if nilai >= 90:
    print("Grade: A")
elif nilai >= 80:
    print("Grade: B+")
else:
    print("Grade: C")

The block of code inside the if statement executes only if the condition is True.

Syntax of the if statement

if condition:
     statement 1
     statement 2
     statement n

Example Code:

number = 6
if number > 5:
    # Calculate square
    print(number * number)
print('Next lines of code')

If – else statement

The if-else statement checks the condition and executes the if block of code when the condition is True, and if the condition is False, it will execute the else block of code.

Syntax of the if-else statement:

if condition:
    statement 1
else:
    statement 2

If the condition is True, then statement 1 will be executed If the condition is False, statement 2 will be executed. See the following flowchart for more detail.

password = input('Enter password ')

if password == "PYnative@#29":
    print("Correct password")
else:
    print("Incorrect Password")

In Python, the if-elif-else condition statement has an elif blocks to chain multiple conditions one after another. This is useful when you need to check multiple conditions.

With the help of if-elif-else we can make a tricky decision. The elif statement checks multiple conditions one by one and if the condition fulfills, then executes that code.

Syntax of the if-elif-else statement:

if condition-1:  
     statement 1 
elif condition-2:
     stetement 2 
elif condition-3:
     stetement 3 
     ...         
else:            
     statement   
def user_check(choice):
    if choice == 1:
        print("Admin")
    elif choice == 2:
        print("Editor")
    elif choice == 3:
        print("Guest")
    else:
        print("Wrong entry")

user_check(1)
user_check(2)
user_check(3)
user_check(4)

2. Iterative Statements

These statements are used to execute a block of code repeatedly.

Types of Iterative Statements:

  • for loop

  • while loop

  • Nested loops

Example: for loop

for i in range(5):
    print("Loop iteration:", i)

Example: while loop

x = 0
while x < 5:
    print("x is:", x)
    x += 1

These loops continue executing until a condition is met.


3. Transfer Statements

These statements are used to control the flow of execution by breaking, skipping, or stopping loops and functions.

Types of Transfer Statements:

  • break → Terminates the loop

  • continue → Skips the current iteration and moves to the next

  • pass → Acts as a placeholder

  • return → Exits a function and returns a value

Example: break

for i in range(10):
    if i == 5:
        break  # Stops the loop when i == 5
    print(i)

Example: continue

for i in range(5):
    if i == 2:
        continue  # Skips printing 2
    print(i)

Example: pass

def fungsi_belum_jadi():
    pass  # Placeholder, doesn't execute anything

Example: return

def add(a, b):
    return a + b  # Returns the sum and exits the function

hasil = add(3, 5)
print(hasil)

Summary

Category
Statements
Purpose

Conditional Statements

if, if-else, if-elif-else

Decision making

Iterative Statements

for, while, nested loops

Repeating execution

Transfer Statements

break, continue, pass, return

Controlling execution flow

Sumber: https://pynative.com/python-control-flow-statements/

Last updated