break Statement In C Programming Language


In this video tutorial lets learn more about the working of break statement or break keyword in C programming language.

Related Read:
Nested For Loop In C Programming Language

Video Tutorial: break Statement In C Programming Language


[youtube https://www.youtube.com/watch?v=7WsVGKKs2bg]

YouTube Link: https://www.youtube.com/watch?v=7WsVGKKs2bg [Watch the Video In Full Screen.]


Source Code: break Statement In C Programming Language : For Loop

#include<stdio.h>

int main()
{
    int i;

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

        printf("%d Apple\n", i);
    }
    printf("\nEnd of for loop.\n", i);
    return 0;
}

Output:
1 Apple
2 Apple

End of for loop.

In above C program we introduce break keyword when i is equal to 3. so for 3rd iteration of the for loop control exits the loop and whatever instructions present after the for loop gets executed.

Source Code: Continue Statement In C Programming Language : Nested For Loop

#include<stdio.h>

int main()
{
    int i, j;

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

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

            printf("\t%d Oracle\n", j);
        }

    }
    printf("\nEnd of for loop.\n", i);
    return 0;
}

Output:

1 Apple
        1 Oracle
2 Apple
        1 Oracle
3 Apple
        1 Oracle
4 Apple
        1 Oracle
5 Apple
        1 Oracle

End of for loop.

In above C program, inside inner for loop we’ve break keyword when j value is equal to 2. So once the keyword break is encountered, control exits the inner for loop. This doesn’t affect the outer for loop, as the keyword break is present inside the inner for loop.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Leave a Reply

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