While loops

If you want to run some code until certain conditional stop being True, then, you need a while loop. Loops are basically the backbone of coding. To code a while loop you need the instruction "while(x):" (remember the indentation), where x is a conditional that might or might not change after each iteration.

# This code will print 0, 1, 2, 3, 4, 5 per line
i = 0
while i < 6:
    print(i)
    i += 1

# This code will print the numbers as we pop them out of A
A = [1,2,3,4,5]
while A:
    print(A.pop())

# This code will append 5 integers in the list
i = 0
A = []
while len(A) < 5:
    A.append(i)
    i = i*i + 1

Break statement

The "break" statement stops the loop right in that place, and get out of it.

# This will print 10, 9, 8, 7, 6, 5 per line
i = 10
while i:
    print(i)
    if i == 5:
        break
    i -= 1

# This will search for a number in an array
i = 0
search = 5
A = [6, 4, 7, 2, 3, 5, 9]
while i < len(A):
    if A[i] == search:
        print("Found")
        break
    i += 1

Continue statement

The "continue" statement stops the current iteration, and continue with the next one.

# This code will print just even numbers
i = 0 
while i <= 9:
    i += 1
    if i%2:
        continue
    print(i)
    

Else statement

The "else" statement runs a block of code once when the condition no longer is true. This part of code will not run if we stop the while loop with a "break" (Honestly, It's not common, I read it just once in somebody's code).

# This will search for a number in an array and print Found
i = 0
search = 5
A = [6, 4, 7, 2, 3, 5, 9]
while i < len(A):
    if A[i] == search:
        print("Found")
        break
    i += 1
else:
    print("Not found")

# This will search for a number in an array and print Not found
i = 0
search = 5
A = [6, 4, 7, 2, 3, 9]
while i < len(A):
    if A[i] == search:
        print("Found")
        break
    i += 1
else:
    print("Not found")

# This will search for a number in an array and won't print anything
i = 0
search = 5
A = [6, 4, 7, 2, 3, 9]
while i < len(A):
    if A[i] == search:
        print("Found")
        break
    i += 1

Infinite loop

If the conditional to break the loop is really complex, you might need a infinite loop and inside of it you can describe better the breaks of the loop. Infinite loops can be very useful. Just remember to breake it out of at some point, so it doesn’t truly become infinite. In which case, you will need to press "CTRL + C", it generates an interrupt from the keyboard and stop the loop (actually is stops any code when you press them).

# This code will print 0 to 10
i = 0
lim = 100
while True:
    print(i)
    if i*i >= lim:
        break
    if i%4==0 and i%3==0:
        break
    i += 1

# This code is an error, so you will need to break it out with CTRL + C
i = 0
while True:
    print(i)
    i += 1
    if i == 0:
        break

Nested while loop

A nested loop is a loop inside a loop. Pretty simple and well used.

"""
This code will print
        *
       ***
      *****
     *******
    *********
   ***********
  *************
 ***************
*****************
"""
i = 0
lvl = 8
while i <= lvl:
    j = 0
    while j < lvl-i:
        print(' ', end='')
        j += 1
    
    k = 0
    while k < 2*i+1:
        print('*', end='')
        k += 1
    print('')
    i += 1