Introduction

Today, we are going to learn about break and continue, which are control tools used in loops in Go. The break command allows us to exit a loop early, while continue facilitates skipping unnecessary iterations. Let's dive in!

Break Statement

You can liken the break command to the moment when the music stops in a game of musical chairs, prompting you to leave the loop. It ends the loop, irrespective of the original condition of the loop.

Here is a quick example:

Our loop operates on numbers from 0 through 6 and breaks when it reaches 7, thereby exiting early and skipping all remaining iterations.

Continue Statement

The continue keyword in Go can be compared to bypassing a boring view during a walk. It disregards the current loop iteration and moves ahead to the next one.

Here is an example:

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal