Python

Python Conditions & Loops

Conditions and loops are fundamental constructs in Python that allow you to control the flow of your program.

---

1. Conditional Statements

Conditional statements allow you to execute code only if certain conditions are met.

1.1 if Statement

x = 10

            if x > 5:
                print("x is greater than 5")
            

1.2 if-else Statement

x = 3

            if x > 5:
                print("x is greater than 5")
            else:
                print("x is 5 or less")
            

1.3 elif Statement

Used for multiple conditions:

x = 10

            if x > 15:
                print("x is greater than 15")
            elif x > 5:
                print("x is greater than 5 but <= 15")
            else:
                print("x is 5 or less")
            

1.4 Nested Conditions

x = 10
            y = 20

            if x > 5:
                if y > 15:
                    print("Both conditions are true")
            
---

2. Loops

Loops are used to execute a block of code repeatedly until a condition is met.

2.1 for Loop

Iterates over a sequence (list, tuple, string, range, etc.):

fruits = ["apple", "banana", "cherry"]

            for fruit in fruits:
                print(fruit)

            # Using range()
            for i in range(5):
                print(i)  # Prints 0 to 4
            

2.2 while Loop

Repeats as long as a condition is True:

count = 0

            while count < 5:
                print(count)
                count += 1
            

2.3 break Statement

Stops the loop prematurely:

for i in range(10):
                if i == 5:
                    break
                print(i)  # Prints 0 to 4
            

2.4 continue Statement

Skips the current iteration and continues with the next:

for i in range(5):
                if i == 2:
                    continue
                print(i)  # Prints 0,1,3,4
            

2.5 else with Loops

An optional else block can run after the loop finishes normally (not via break):

for i in range(3):
                print(i)
            else:
                print("Loop completed")  # Runs after loop finishes
            
---

3. Nested Loops

Loops inside loops:

for i in range(3):
                for j in range(2):
                    print(f"i={i}, j={j}")
            
---

4. Best Practices

  • Keep loop bodies small and simple for readability.
  • Prefer for loops over while when the number of iterations is known.
  • Avoid deep nesting of loops and conditions to maintain clarity.
---

5. Interview Questions

Q1. Difference between if-else and elif?

elif allows checking multiple conditions without writing multiple nested if-else blocks.

Q2. Can a while loop run forever?

Yes, if its condition never becomes False. Example: while True:

Q3. What is the purpose of break and continue?

break exits a loop immediately; continue skips the current iteration.

Q4. Can loops have else block?

Yes, the else block executes if the loop finishes normally (not via break).