# What are loops in coding?

Python provides the keywords and code structure, called loops, for repeating a code snippet. This will make your code more efficient.
4.1
Python has two methods of repeating code blocks, for loops and while loops. For loops are mainly used for iterating over lists. In this case, iterate simply means working through each item in the sequence as part of a cycle. The for loop uses a variable to store the current item from the list as the cycle repeats. As the program moves through each iteration, the variable updates to the next item in the list. The for loop can have as many statements as you need as part of its code block. Use indents to separate the statements from the rest of the program.
42.8
After the code block runs for the last item in the list, the for loop stops. In contrast, a while loop repeats as long as a condition remains True. For example, the condition might be user input. The loop might repeat until the user types a specific word or presses a key. As another example, the condition might be a counter variable that updates on each iteration of the loop. When the counter reaches a certain number, the loop ends.
73.9
As with for loops, you can include as many statements as you need in the while loop. Just remember to indent every line that’s part of the code block. Use loops and make your programmes more efficient and powerful.

There are scenarios, based on the evaluation of a condition, where we may need to repeat a block of code. Imagine you are a software developer and you are required to provide a software module for all the employees in your department, for example.

To do this, you must print the payroll details of each employee separately. Printing the details of all the employees will be a tiresome task, so instead, you can use the logic for calculating the details and keep on iterating the same logic statement.

For such practical purposes, Python provides the keywords and code structure, called loops, for repeating a code snippet. This will save you time and make your code more efficient.

Watch the video to learn about loops and how they work in Python.

## What is ‘for loops’?

We use for loops in Python to loop over a collection (like list or tuple) or an iterator, and perform some action repetitively.

The standard syntax for a for loop is:

for value in collection:
# do something with value

For example, in the code snippet, we are printing all the values stored in a list.

Code:

alist = [10,20,30,40,50,5,56,57]
for num in alist:
... print(num)
...


Output:
10
20
30
40
50
5
56
57

## Loop control statements

There are various loop control statements that are used to break the usual repetition of code. Let’s look at code snippets of what these loop statements look like.

### Break

A for loop can be exited altogether using the break keyword. In the following code, we are summing up the elements of the list until a None value is encountered, at which point the termination is executed.

Code:

sequence = [1,2,3,4,None,6,None,8,9,10]
total = 0
for value in sequence:
... if value == None:
... break
... total+=value
...
print(total)


Output:
10

### Continue

In the following code we are summing up all the elements in the list, skipping the None values.

Code:

total = 0
for value in sequence:
... if value is None:
continue
... total += value
...
print(total)


Output: 43