Loops

  • Loops are used for eprfoming the same, or similar operation multiple times.

  • Used to prevent the need to write the same thing over and over.

  • The for loop has following syntax:

for i in range(start, end): # Determines start and end value, and i is the reciever of values
	code

# Here is an example

for i in range(0, 5):
	print(i)

# or

for i in range(5):
	print(i)

While Loop

  • While loop is different from the for loop, since it allows to keep iterating as long as a certain condition is met.

  • The while loop is written the following way:

while condition:
	code

# Code will only execute if True

Break

  • Break stops the loop instantly when its encountered.

  • Break can be used following way:

for i in range(10):
	if i == 6:
		break
	print(i)

# Loop iterates itself until it reaches 6. Then it ends.

Continue

  • Continue stops the current iteration adn continues to the next iteration.

  • Continue can be used following way:

for i in range(3, 9):
	if i == 5:
		continue
	print(i)

# The loop will skip 5 and continue.