Skip to study material
← Question index

QUESTION 26 · Jump Statements

Explain the break statement with an example.

5 marks

Answer in brief

The break statement immediately leaves the nearest enclosing loop or switch.

Syntax and working

break;

Loop behaviour

When execution reaches break, the remaining statements in that loop body are skipped. Control moves to the statement after the loop. In a for loop, the update is not performed for that abandoned iteration. The statement does not leave the whole function; use return when that is the intention.

Example trace

The example counts from 1 towards 5. It prints 1 and 2. When i reaches 3, the if condition is true and break executes before printf. The loop ends, so neither 3 nor any later value is printed. The message Done then prints because it follows the loop.

Uses and restrictions

  1. Stop a search when the required item is found.
  2. Stop repeated input when a sentinel or stopping condition is detected.
  3. Prevent unwanted switch fall-through.
  4. Use break only inside a loop or switch. It cannot be used as a general jump anywhere in a function.

Nested structures and complexity

A break in an inner loop leaves only that inner loop; the outer loop can continue. A break inside a switch nested in a loop leaves the switch. An early break may reduce work on a particular input, but worst-case complexity still depends on the longest permitted execution. A search through n items can still be O(n) in the worst case.

C program and output

/* CodeBhavya | PPS AR25 | Learning example */
#include <stdio.h>
int main(void)
{
    for (int i = 1; i <= 5; ++i) {
        if (i == 3) { break; }
        printf("%d\n", i);
    }
    puts("Done");
    return 0;
}

Sample input and output

This example uses values already written in the program.

Output

1
2
Done

Related exam questions

Related questions in the jump statements category from the supplied papers. The marks below are the printed allocations, including any combined subquestion.

  • July 2021 · Q2(b) · 5 marks

    Mention the pros and cons of goto in C.

    Paper P04 · page 1

View this category in the exam question bank →