4) b. Break and Continue Statements

There are two additional control flow statements that can be used with loops.

The break statement is used to forcibly exit out of the current loop.

In the following example we have a list of students. Our task is to find the first student whose name with a letter “M” and print his position in the list.

What would happen if you didn’t put the break statement after printing the student’s position? As you probably guessed all of the students’ positions whose names start with the letter “M” would have been printed. But we only needed one of them which is why we break out of the loop right after we find the first match.

Another statement that you need to learn is the continue statement. Instead of breaking out of the loop, this statement allows you to forcibly skip to the next iteration of the current loop.