Skip to content

Latest commit

 

History

History
54 lines (43 loc) · 877 Bytes

File metadata and controls

54 lines (43 loc) · 877 Bytes

While Loop

i = 0
while i < 5:
    print(i)
    i += 1
  • break: Exit the loop.

    i = 0
    while i < 10:
        if i == 5:
            break
        print(i)
        i += 1
  • continue: Skip the rest of the code in this iteration.

    i = 0
    while i < 10:
        i += 1
        if i % 2 == 0:
            continue
        print(i)

While Loop Techniques

  • Combining with else: The else block after a while loop executes when the loop condition becomes false.

    i = 0
    while i < 5:
        print(i)
        i += 1
    else:
        print("Loop ended")
  • Simulating Do-While Loop: Python doesn't have a do-while loop, but you can simulate it.

    while True:
        print("This will execute at least once")
        if not condition:
            break

Go Back