Learning coding means GreatToCode Be more than a Coder ! Greattocode , Join GreatToCode Community,1000+ Students Trusted On Us .If You want to learn coding, Then GreatToCode Help You.No matter what It Takes !


CODE YOUR WAY TO A MORE FULFILLING And HIGHER PAYING CAREER IN TECH, START CODING FOR FREE Camp With GreatToCode - Join the Thousands learning to code with GreatToCode
Interactive online coding classes for at-home learning with GreatToCode . Try ₹Free Per Month Coding Classes With The Top Teachers . Flow Control In Python (If-Else & Loop)

Flow Control In Python (If-Else & Loop)

Become More Then A coder | Learn & Start Coding Now.

🔹 1. If-Else Statements in Python

✅ Purpose:

Used to make decisions in your code. Based on a condition, different blocks of code are executed.


🔸 Syntax:

if condition:
    # Code runs if condition is True
elif another_condition:
    # Runs if first condition is False and this is True
else:
    # Runs if none of the above conditions are True

🔸 Example:

x = 15

if x > 20:
    print("Greater than 20")
elif x > 10:
    print("Greater than 10 but not more than 20")
else:
    print("10 or less")

🧠 Explanation:

  • x = 15
  • x > 20 is False
  • x > 10 is True, so it prints "Greater than 10 but not more than 20"

🔹 2. Loops in Python

Loops help repeat code until a condition is met.


➤ A. for Loop

✅ Used for:

  • Iterating over a sequence (like list, range, string)

🔸 Example:

for i in range(5):
    print("Number:", i)

🧠 Explanation:

  • range(5) gives: 0, 1, 2, 3, 4
  • Loop prints each number from 0 to 4

➤ B. while Loop

✅ Used for:

  • Repeating a block while a condition is True

🔸 Example:

count = 1

while count <= 5:
    print("Count is:", count)
    count += 1

🧠 Explanation:

  • Starts from 1 and continues until 5
  • count += 1 increases the value in each loop

🔹 3. Loop Control Statements

🔸 break

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

➡️ Stops at 4, doesn't print 5


🔸 continue

  • Skips the current loop iteration and continues with the next
for i in range(5):
    if i == 2:
        continue
    print(i)

➡️ Skips printing 2, prints 0, 1, 3, 4


✅ Summary Table

Statement Purpose
if Executes code when condition is True
elif Checks next condition if previous is False
else Executes when none of the above are True
for Loop over sequences/ranges
while Loop until condition is False
break Exit the loop early
continue Skip current iteration



Post a Comment

0 Comments

•Give The opportunity to your child with GreatToCode Kid's • Online Coding Classes for Your Kid • Introduce Your kid To the world's of coding
•Fuel You Career with our 100+ Hiring Partners, Advance Your Career in Tech with GreatToCode. •Join The Largest Tech and coding Community and Fast Forward Your career with GreatToCode. •10000+ Learner's+ 90 % placement Guarantee. • Learning Coding is Better with the GreatToCode community .
•Greattocode Kid's •GreatToCode Career •GreatToCode Interview •GreatToCode Professional •GreatToCode for schools •GreatToCode For colleges •GreatToCods For Businesses.
Are you ready to join the millions of people learning to code? GreatToCode Pass is your one-stop-shop to get access to 1000+ courses, top-notch support, and successful job placement. What are you waiting for? Sign up now and get your future in motion with GreatToCode Pass.