Break Statements
The Break Statement can alter the flow of a normal loop. Loops iterate over a block of code until the Boolean expression is false, but sometimes we wish to terminate the iteration or even the whole loop early. The break statement is used in these cases.
The break statement terminates the loop containing it. Control of the program flows to the statement immediately after the body of the loop. If the break statement is inside a nested loop (loop inside another loop), the break will terminate the innermost loop only. Note you will most likely need to place an if statement inside the loop to use the break statement, because if you just have a break statement all by itself inside a loop, it will always hit it the first time through and that is not really useful!
The break statement (in most computer programming languages), takes the generic form of:
In this example program, the user is asked to enter a positive integer and the program will count how many times it goes through the loop until it reaches that number, except it will always stop when it hits 5!
Top-Down Design for the Break Statement

Flowchart for the Break Statement

Pseudocode for the Break Statement
Code for the Break Statement
1// Copyright (c) 2020 Mr. Coxall All rights reserved.
2//
3// Created by: Mr. Coxall
4// Created on: Sep 2020
5// This program uses a break statement
6
7#include <stdio.h>
8
9int main() {
10 // this function uses a break statement
11 int positiveInteger;
12
13 // input
14 printf("Enter how many times to repeat: ");
15 scanf("%d", &positiveInteger);
16 printf("\n");
17
18 // process & output
19 for (int loopCounter = 0; loopCounter < positiveInteger; loopCounter++) {
20 printf("%d time(s) through the loop.\n", loopCounter);
21 if (loopCounter >= 5) {
22 break;
23 }
24 }
25
26 printf("\nDone.\n");
27 return 0;
28}
1// Copyright (c) 2020 St. Mother Teresa HS All rights reserved.
2//
3// Created by: Mr. Coxall
4// Created on: Sep 2020
5// This program uses a break statement
6
7#include <iostream>
8
9int main() {
10 // this function uses a break statement
11 int positiveInteger;
12
13 // input
14 std::cout << "Enter how many times to repeat: ";
15 std::cin >> positiveInteger;
16 std::cout << std::endl;
17
18 // process & output
19 for (int loopCounter = 0; loopCounter < positiveInteger; loopCounter++) {
20 std::cout << loopCounter <<" time through loop." << std::endl;
21 if (loopCounter >= 5) {
22 break;
23 }
24 }
25
26 std::cout << "\nDone." << std::endl;
27 return 0;
28}
1/* Created by: Mr. Coxall
2 * Created on: Sep 2020
3 * This program uses a break statement
4*/
5
6using System;
7
8/*
9 * The Program class
10*/
11class Program {
12 static void Main() {
13 // this function uses a break statement
14
15 int positiveInteger;
16
17 // input
18 Console.Write("Enter how many times to repeat: ");
19 positiveInteger = Convert.ToInt32(Console.ReadLine());
20 Console.WriteLine();
21
22 // process & output
23 for (int loopCounter = 0; loopCounter < positiveInteger; loopCounter++) {
24 Console.WriteLine(loopCounter + " time(s) through the loop.");
25 if (loopCounter >= 5) {
26 break;
27 }
28 }
29
30 Console.WriteLine("\nDone.");
31 }
32}
1/**
2 * Created by: Mr. Coxall
3 * Created on: Sep 2020
4 * This program uses a break statement
5 */
6
7package main
8
9import (
10 "fmt"
11)
12
13func main() {
14 // this function uses a break statement
15
16 var counter int // in go no need to set to 0, it automaticall is!
17 var positiveInteger int
18
19 // input
20 fmt.Print("Enter how many times to repeat: ")
21 fmt.Scan(&positiveInteger)
22 fmt.Println()
23
24 // process & output
25 for counter < positiveInteger {
26 fmt.Printf("%d time(s) through the loop.\n", counter)
27 if counter >= 5 {
28 break
29 }
30 counter++
31 }
32
33 fmt.Println("\nDone.")
34}
1/*
2 * This program uses a break statement
3 *
4 * @author Mr Coxall
5 * @version 1.0
6 * @since 2020-09-01
7 */
8
9import java.util.Scanner;
10
11public class Main {
12 public static void main(String[] args) {
13 // this function uses a break statement
14
15 // create Scanner object for user input
16 Scanner scanner = new Scanner(System.in);
17
18 // input
19 System.out.print("Enter how many times to repeat: ");
20 String positiveIntegerStr = scanner.nextLine();
21 System.out.println();
22
23 // process & output
24 int positiveInteger = Integer.parseInt(positiveIntegerStr);
25
26 for (int loopCounter = 0; loopCounter < positiveInteger; loopCounter++) {
27 System.out.println("%d time(s) through the loop.".formatted(loopCounter));
28 if (loopCounter >= 5) {
29 break;
30 }
31 }
32
33 // close the Scanner object
34 scanner.close();
35 System.out.println("\nDone.");
36 }
37}
1/* Created by: Mr. Coxall
2 * Created on: Sep 2020
3 * This program uses a break statement
4 */
5
6const prompt = require("prompt-sync")()
7
8let counter = 0
9
10// input
11const positiveIntegerStr = prompt("Enter how many times to repeat: ")
12console.log("")
13
14// process & output
15const positiveInteger = parseInt(positiveIntegerStr)
16
17for (var loopCounter = 0; loopCounter < positiveInteger; loopCounter++) {
18 console.log(`${loopCounter} time(s) through the loop.`)
19 if (loopCounter >= 5){
20 break
21 }
22}
23
24console.log("\nDone.")
1#!/usr/bin/env python3
2"""
3Created by: Mr. Coxall
4Created on: Sep 2020
5This module uses a break statement
6"""
7
8
9def main() -> None:
10 """The main() function uses a break statement, returns None."""
11
12 # input
13 positive_integer = int(input("Enter how many times to repeat: "))
14 print("")
15
16 # process & output
17 for loop_counter in range(positive_integer):
18 print(f"{loop_counter} time through loop.")
19 if loop_counter >= 5:
20 break
21
22 print("\nDone.")
23
24
25if __name__ == "__main__":
26 main()
Example Output
