Sometimes, we need to exit the loop before it reaches the end. This can happen if we have found what we were looking for and don’t need to make any more computations in the loop.
A perfect example is the one we have just covered. At a certain point, n1 is 23 and n2 is 27. Our condition of n1 + n2 == n has been fulfilled. But the loops keep running and comparing all other pairs as well. This is why the pair is printed twice. It would be nice to just stop it when the pair is found once.
That’s what the break keyword is for. It can break the loop whenever we want.
Let’s add it to the example above: