1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Continue and Break Statements in C

Discussion in 'C' started by lionaneesh, Jul 21, 2011.

  1. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    Break and Continue are two special kind of statements used in some programming languages including C , they are used in some special conditions when the programmer needs an early exit from the loop or continuation with the next iteration.

    Break

    The Break statement is used to terminate any kind of loop including while, do while and for loops and it follows a basic syntax.

    Code:
    while(1)
    {
        if(condition)
        {
            break;
        }
    }
    
    Explanation:-

    Here, we are declaring an infinite loop, in which there is a terminating condition, If this condition is true , the break statement is executed and hence the program breaks out of that loop.

    Continue

    The Continue statement is used to skip over the rest of the loop iteration and directly jump to the loop control (i.e. Start of the next iteration).

    Syntax:-


    Code:
    while(1)
    {
        someCode;
        if(condition)
        {
            continue;
        }
        moreCode;
    }
    
    Explanation:-


    Here we again declare a while loop, in which there is a condition, If this condition evaluates to true then the further execution of this iteration (which includes moreCode; ) stops and the program jumps back to the loop control and starts with the next iteration.

    Now that we know something about these special statements, let’s use them to construct a basic C Program.

    Using Break and Continue



    In this section we’ll be constructing a basic C Program in which the program inputs an integer , If the integer is between 1 and 99 it will print correct. If the integer input is greater than 99 then the program will exit and if it is less than 1 then the program will prompt for an integer again.

    Code:
    #include<stdio.h>
    
    int main()
    {
        int input;
        while(1)
        {
            printf("Please Input an Integer : ");
            scanf("%d",&input);
            if(input < 1)
            {
                continue;
            }
            else if(input > 99)
            {
                break;
            }
            printf("Correct! :D\n");
        }
    }
    
    Output:-


    Session 1 (input : 0 and 100)
    Code:
    Please Input an Integer : 0
    Please Input an Integer : 100
    
    Session 2 (Input : 1,50,99) :-
    Code:
    Please Input an Integer : 1
    Correct! :D
    Please Input an Integer : 50
    Correct! :D
    Please Input an Integer : 99
    Correct! :D
    
    This program was only to demonstrate the basic functionality which is provided by Continue and Break statements in C.


    That’s all for this tutorial stay tuned for more
     

Share This Page