C break statement Tutorial

  • Post author:
  • Post category:C

In this section, we will learn what the break statement is and how to use it in C.

Note: before reading this section, we’re expecting that you already familiar with the `while-loop`, `for-loop` and the `if-statement`. If don’t please make sure you read those sections first.

Break Statement in C

In the `while-loop` and `for-loop` sections, we mentioned that as long as the condition within these loops is true, those loops continue to run their blocks.

But sometimes, even though the condition within a loop is true, we want to `break` that loop and start to run those instructions that come after.

This is where the `break` statement can be very helpful.

Whenever the CPU reaches this statement within a loop, it knows that the execution of the loop is over and it should break that loop and start to run those instructions that come after.

Example: while loop break statement

#include <stdio.h>

int main() {

    int inValue  = 100;

    while (inValue >20){
        printf("The value of the inValue-variable is: %d\n",inValue);
        if (inValue == 90){
            break;
        }
        inValue--;
    }
    printf("End\n");
    return 0;
}

Output:

The value of the inValue-variable is: 100

The value of the inValue-variable is: 99

The value of the inValue-variable is: 98

The value of the inValue-variable is: 97

The value of the inValue-variable is: 96

The value of the inValue-variable is: 95

The value of the inValue-variable is: 94

The value of the inValue-variable is: 93

The value of the inValue-variable is: 92

The value of the inValue-variable is: 91

The value of the inValue-variable is: 90

End

How does break statement work in C while loop?

In this example, the initial value assigned to the `inValue` is 100 and so the condition of the `while-loop` results true (because 100 is greater than 20) and the body of this loop will start to run.

Within the body, first, we have the `printf` function that displays a message on the console and then there’s the `if-statement` and its instructions which will only run if the value of the `inValue` is equal to 90 and inside the body of this `if-statement` we have the `break` statement which will break the loop and sends the CPU to run those instructions after the loop.

Also, within the body of the `while-loop` the value assigned to the `inValue` is decremented each time the loop is iterated.

After repeating the loop 10 times, the condition of the `if-statement` in the example becomes true and so its body will run.

As mentioned before, within the body of `if-statement`, the instruction is to break the loop via `break-statement`. This is what happened after the value of the `inValue` reached to 90 in the example and that’s how we’ve got the result above.

Example: for loop break statement

#include <stdio.h>

int main() {
    for (int i = 0; i<10;i++){
        printf("The value of the i variable is: %d\n", i);
        if (i==4){
            break;
        }
    }
    return 0;
}

Output:

The value of the i variable is: 0

The value of the i variable is: 1

The value of the i variable is: 2

The value of the i variable is: 3

The value of the i variable is: 4

Example: do-while break statement

#include <stdio.h>

int main() {
    int i=0;
    do{
        printf("The value of the i variable is: %d\n",i);
        i++;
        if (i==4){
            break;
        }
    }while(i<10);
    return 0;
}

Output:

The value of the i variable is: 0

The value of the i variable is: 1

The value of the i variable is: 2

The value of the i variable is: 3

Example: nested loop and break statement

We can use the break statement within the body of a nested loop as well. In a situation like this, you should know that only the nested loop will break and not the enclosing one!

#include <stdio.h>

int main() {
    for (int i = 0; i<4; i++){
        for (int b = 0; b<40;b++){
            printf("The value of the b variable is: %d\n",b);
            if (b==3){
                break;
            }
        }
        printf("i variable: %d\n",i);
    }
    return 0;
}

Output:

The value of the b variable is: 0

The value of the b variable is: 1

The value of the b variable is: 2

The value of the b variable is: 3

i variable: 0

The value of the b variable is: 0

The value of the b variable is: 1

The value of the b variable is: 2

The value of the b variable is: 3

i variable: 1

The value of the b variable is: 0

The value of the b variable is: 1

The value of the b variable is: 2

The value of the b variable is: 3

i variable: 2

The value of the b variable is: 0

The value of the b variable is: 1

The value of the b variable is: 2

The value of the b variable is: 3

i variable: 3

Difference between break and continue

The continue statement is used to skip the current iteration of a loop and move on to the next iteration. This means we stay in the same loop and just the current iteration will be skipped.

On the other hand, using the break statement will break the loop altogether! That means there won’t be any loop iteration after a call to the break statement.

Leave a Reply