• Home
  • 2.16 break Statement

2.16 break Statement

View Categories

2.16 break Statement

< 1 min read

Table of Contents

The break statement immediately terminates the nearest enclosing loop. Control continues with the statement following that loop.

#include <stdio.h>

int main()
{
    int i;

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

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

    return 0;
}

Example Output #

1
2
3
4
5

Explanation #

The for loop normally continues until i becomes greater than 10.

When i reaches 6, the condition inside the if statement becomes true:

if (i == 6)
{
    break;
}

The break statement immediately terminates the for loop. Therefore, 6 and the remaining values are not printed.

break affects only the nearest enclosing loop or switch statement.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *