Lesson 31 of 60 – break Statement in C
52%

break Statement in C

The break statement is used to immediately terminate the nearest enclosing loop or switch statement. When C reaches break, control moves to the first statement after that loop or switch.

Note: The break statement stops the nearest enclosing loop or switch. It does not stop the entire program.

1. What is break Statement?

The break statement is used to exit a loop immediately.

break;

After executing break, control moves to the statement after the nearest enclosing loop.

2. Syntax of break

break;

The break statement consists of the keyword break followed by a semicolon.

3. Simple break Example

for (int i = 1; i <= 10; i++)
{
    if (i == 5)
    {
        break;
    }

    printf("%d ", i);
}

Output:

1 2 3 4

When i becomes 5, the break statement terminates the loop.

4. break with for Loop

for (int i = 1; i <= 10; i++)
{
    printf("%d ", i);

    if (i == 5)
    {
        break;
    }
}

The loop stops immediately after printing 5.

5. break with while Loop

int i = 1;

while (i <= 10)
{
    if (i == 5)
    {
        break;
    }

    printf("%d ", i);
    i++;
}

Output:

1 2 3 4

6. break with do-while Loop

int i = 1;

do
{
    if (i == 5)
    {
        break;
    }

    printf("%d ", i);
    i++;
}
while (i <= 10);

The loop stops when i reaches 5.

7. break and if Statement

The break statement is often placed inside an if statement.

for (int i = 1; i <= 10; i++)
{
    if (i > 5)
    {
        break;
    }

    printf("%d ", i);
}

The if condition decides when the loop should stop.

8. break Stops the Nearest Loop

The break statement terminates only the nearest enclosing loop.

for (int i = 1; i <= 5; i++)
{
    if (i == 3)
    {
        break;
    }

    printf("%d ", i);
}

printf("\nProgram continues.");

Output:

1 2
Program continues.

9. break in Infinite Loop

A break statement can be used to terminate an intentionally infinite loop.

int i = 1;

while (1)
{
    printf("%d ", i);

    if (i == 5)
    {
        break;
    }

    i++;
}

The loop stops when i becomes 5.

10. Finding a Number

int numbers[] = {10, 20, 30, 40, 50};
int target = 30;

for (int i = 0; i < 5; i++)
{
    if (numbers[i] == target)
    {
        printf("Number found!");
        break;
    }
}

Once the target is found, there is no need to continue searching.

11. Searching an Array

int numbers[] = {4, 8, 12, 16, 20};
int target = 12;
int found = 0;

for (int i = 0; i < 5; i++)
{
    if (numbers[i] == target)
    {
        found = 1;
        break;
    }
}

if (found)
{
    printf("Found");
}
else
{
    printf("Not Found");
}

The break stops the search as soon as the target is found.

12. Stop When User Enters Zero

int number;

while (1)
{
    printf("Enter number: ");
    scanf("%d", &number);

    if (number == 0)
    {
        break;
    }

    printf("You entered %d\n", number);
}

The loop continues until the user enters 0.

13. break in switch Statement

int choice = 2;

switch (choice)
{
    case 1:
        printf("One");
        break;

    case 2:
        printf("Two");
        break;

    default:
        printf("Invalid choice");
}

In a switch, break is commonly used to prevent execution from continuing into the following cases.

14. break Prevents Fall-Through in switch

int choice = 1;

switch (choice)
{
    case 1:
        printf("One\n");
        break;

    case 2:
        printf("Two\n");
        break;

    default:
        printf("Other");
}

When choice is 1, the program prints only One and then leaves the switch.

15. break with User Input

int number;

for (int i = 1; i <= 5; i++)
{
    printf("Enter number: ");
    scanf("%d", &number);

    if (number == 0)
    {
        break;
    }

    printf("Number = %d\n", number);
}

Entering 0 immediately terminates the loop.

16. break After a Condition

for (int i = 1; i <= 20; i++)
{
    if (i % 7 == 0)
    {
        break;
    }

    printf("%d ", i);
}

Output:

1 2 3 4 5 6

The loop stops when it reaches the first multiple of 7.

17. break with Character Input

char ch;

while (1)
{
    printf("Enter a character: ");
    scanf(" %c", &ch);

    if (ch == 'q')
    {
        break;
    }

    printf("You entered: %c\n", ch);
}

The loop terminates when the user enters q.

18. break in Nested Loops

for (int i = 1; i <= 3; i++)
{
    for (int j = 1; j <= 5; j++)
    {
        if (j == 3)
        {
            break;
        }

        printf("%d ", j);
    }

    printf("\n");
}

The break exits the inner loop, not the outer loop.

19. Important Rule in Nested Loops

When loops are nested, an ordinary break exits only the nearest enclosing loop.

for (int i = 1; i <= 3; i++)
{
    for (int j = 1; j <= 3; j++)
    {
        if (j == 2)
        {
            break;
        }

        printf("i=%d j=%d\n", i, j);
    }
}

The outer loop continues after the inner loop is terminated.

20. break Does Not Exit an if Statement Alone

The break statement must be inside a loop or switch statement. It is not a general-purpose statement for exiting an if statement.

if (number == 5)
{
    printf("Number is 5");
}

An if statement itself does not require break.

21. break vs Condition

Sometimes a loop can stop naturally through its condition, while break allows an early exit.

for (int i = 1; i <= 10; i++)
{
    if (i == 5)
    {
        break;
    }

    printf("%d ", i);
}

The normal condition allows values through 10, but break stops the loop earlier at 5.

22. break with do-while

int i = 1;

do
{
    printf("%d ", i);

    if (i == 5)
    {
        break;
    }

    i++;
}
while (i <= 10);

The loop terminates immediately when i reaches 5.

23. Common Mistake: Wrong Placement

The position of break affects when the loop terminates.

for (int i = 1; i <= 5; i++)
{
    printf("%d ", i);

    if (i == 3)
    {
        break;
    }
}

Output:

1 2 3

If the break appears before the printf(), 3 would not be printed.

24. Common Mistake: Forgetting break in switch

If a case does not end with break, execution can continue into the next case when applicable.

switch (choice)
{
    case 1:
        printf("One\n");

    case 2:
        printf("Two\n");
        break;
}

When choice is 1, both messages can be printed because there is no break after case 1.

25. Finding the First Divisible Number

for (int i = 1; i <= 100; i++)
{
    if (i % 9 == 0)
    {
        printf("First divisible number = %d", i);
        break;
    }
}

The loop stops at the first number divisible by 9.

26. Finding the First Negative Number

int numbers[] = {5, 8, 12, -4, 10};
int size = 5;

for (int i = 0; i < size; i++)
{
    if (numbers[i] < 0)
    {
        printf("First negative number = %d", numbers[i]);
        break;
    }
}

The loop stops as soon as the first negative number is found.

27. Complete Search Program

#include <stdio.h>

int main()
{
    int numbers[] = {10, 20, 30, 40, 50};
    int target;
    int found = 0;

    printf("Enter number to search: ");
    scanf("%d", &target);

    for (int i = 0; i < 5; i++)
    {
        if (numbers[i] == target)
        {
            found = 1;
            break;
        }
    }

    if (found)
    {
        printf("Number found.");
    }
    else
    {
        printf("Number not found.");
    }

    return 0;
}

28. Complete Input Loop Program

#include <stdio.h>

int main()
{
    int number;

    while (1)
    {
        printf("Enter a number (0 to exit): ");
        scanf("%d", &number);

        if (number == 0)
        {
            break;
        }

        printf("Square = %d\n", number * number);
    }

    printf("Program ended.");

    return 0;
}

29. Complete Menu Program

#include <stdio.h>

int main()
{
    int choice;

    while (1)
    {
        printf("\n1. Say Hello\n");
        printf("2. Show Message\n");
        printf("3. Exit\n");
        printf("Enter choice: ");
        scanf("%d", &choice);

        if (choice == 1)
        {
            printf("Hello!\n");
        }
        else if (choice == 2)
        {
            printf("Welcome to C programming!\n");
        }
        else if (choice == 3)
        {
            break;
        }
        else
        {
            printf("Invalid choice.\n");
        }
    }

    printf("Program ended.");

    return 0;
}

Here, break provides the exit from the otherwise continuous menu loop.

30. Practice Programs Using break

Practice the following programs:

  1. Print numbers from 1 to 10 and stop at 5.
  2. Print numbers from 1 to 100 and stop at the first multiple of 10.
  3. Search for a number in an array using break.
  4. Read numbers until the user enters 0.
  5. Read characters until the user enters q.
  6. Find the first negative number in an array.
  7. Find the first number divisible by 7.
  8. Create an infinite loop with a controlled exit.
  9. Create a menu using break.
  10. Use break inside a nested loop and observe which loop stops.

Practice these examples carefully to understand that break provides an immediate exit from the nearest enclosing loop or switch.

📌 Key Points

  • break immediately terminates the nearest enclosing loop or switch.
  • It transfers control to the statement after that loop or switch.
  • It can be used with for, while, and do-while loops.
  • It is commonly used with an if condition.
  • It can stop a search when the required value is found.
  • It can terminate an intentional infinite loop.
  • In a nested loop, it exits only the nearest enclosing loop.
  • In a switch, break commonly prevents fall-through to later cases.
  • The position of break determines when the loop stops.
  • break does not terminate the entire program by itself.

🧠 Quick Quiz

Question: What does the break statement do?