Skip to content

Break

The break statement is used to jump out of the current loop. As shown in the visualisation below, this is useful for ending a loop early.

The Break Statement allows you to end a loop early

Break — when, why, and how?

Sometimes you get part-way through a loop and know that the remainder of the loop’s code is not needed, and there is no need to check the loop’s condition again. If you can capture that in a condition, then combining a branching statement with a break will let you jump out of the loop.

This isn’t something you will use often, but is good to know exists.

In C

The break statement is simply the break keyword terminated with a semicolon.

How break works

Ending a loop early

The following code demonstrates the use of the break statement in an event driven program. Here the if statement checks whether the user has hit the escape key on their keyboard, and it breaks the event loop when that is true.

using static SplashKitSDK.SplashKit;
OpenWindow("Circle Test", 400, 400);
ClearScreen(ColorWhite());
while (!QuitRequested())
{
FillCircle(RandomColor(), Rnd(ScreenWidth()), Rnd(ScreenHeight()), Rnd(50));
RefreshScreen();
ProcessEvents();
if (KeyTyped(KeyCode.EscapeKey))
{
break; // end the loop
}
}

The following images show how this works.

Lines 1-3 use the SplashKit SDK to create a new window
The while loop condition is tested (let's assume it's true) so processing proceeds into the body of the loop
Lines 6-8 draws a circle with a random color, position and radius in the window, then listens for events
Let's assume the user hasn't pressed the 'Escape' key, and so the 'if' condition is false. Processing moves back to the start of the while loop.
The while loop condition is tested (let's assume it's true) so processing proceeds into the body of the loop
Lines 6-8 draw another random circle to the window, and listen for user events
This time the user presses the 'Escape' key and execution moves.
The break statement instructs the CPU to terminate the while loop by jumping to the first line of code outside of the while loop. The counter increments from lines 11 to 14.
The WriteLine() function executes and prints 'Bye' to the terminal. The program counter increments and the program ends

Leaving intentionally infinite loops

You can create intentionally infinite loops, and use break statements within the code to terminate the loop. The following program uses an infinite loop, with a break in an if statement to terminate the loop.

using static System.Console;
using static System.Convert;
WriteLine("Before you stands a 12 foot tall Knight...");
WriteLine();
WriteLine("\"We are the Knights who say 'Ni'.\"");
WriteLine("\"I will say Ni to you again if you do not appease us!\"");
while(true)
{
WriteLine("\"Ni!\"");
Write("Submit? ");
if (ReadLine() == "y")
{
break;
}
}
WriteLine("\"Bring us a Shrubbery!\"");

The following images show how this works.

Lines 1-6 print some text to the terminal
The condition in the while loop always evaluates to true and moves the program counter into the body of the loop
Lines 9 and 10 write a text prompt to the terminal
The condition in the 'if' statement reads in user input ('n'), and compares it to the literal string 'y'. It evaluates to 'false' and moves to the next iteration of the while loop.
The condition in the while loop always evaluates to true and moves the program counter into the body of the loop
Lines 9 and 10 write further text prompts to the terminal
The condition in the 'if' statement reads in user input ('y'), which in this case evaluates to true, so the program counter moves to the first line in the body of the 'if' statement
The break statement instructs the CPU to terminate the while loop by jumping to the first line of code outside of the while loop
Line 16 writes some text to the terminal. The program counter increments, and the program ends.

This example was inspired by the classic Monty Python sketch below. This comedy group was the inspiration behind the name for the Python programming language.