Search code examples
cloopsif-statementbreak

Break statement inside a loop which is enclosed by if condition


Will the break statement inside a loop, which is enclosed by an if statement, get the control out of the outer if statement too? I wanted to print an array in a particular pattern and encountered this scenario. I wanted to break out of 'printing' when the count exceeded array size. Initially I put break statements inside the loops and also inside its outer if statement and it worked fine.

    //printing the pattern

    int space=1,spacecount=4,x=2,xcount=1,arrcount=0;
    for(i=1;i<=8;i++)
    {

        if(i==space)
        {
            printf("  ");

            for(j=1;j<=spacecount;j++)
                {
                printf("%d",arr[arrcount++]);
                if(arrcount>=size)
                break;
                }

            if(arrcount>=size)
                        break;
            printf("\n ");

            for(j=1;j<=spacecount;j++)
                {
                printf("%d",arr[arrcount++]);
                if(arrcount>=size)
                break;
                }

            if(arrcount>=size)
            break;

            printf("\n");
            space=space+2;
            spacecount--;
        }

        if(i==x)
        {
            for(j=1;j<=xcount;j++)
                {
                printf("%d\n",arr[arrcount++]);
                if(arrcount>=size)
                break;
                }

            if(arrcount>=size)
            break;

            x=x+2;
            xcount++;
        }

        if(arrcount>=size)
            break;
    }


Then I deleted the break statements inside the outer if and that too seems to be giving the same result.

    //printing the pattern

    int space=1,spacecount=4,x=2,xcount=1,arrcount=0;
    for(i=1;i<=8;i++)
   {
        if(i==space)
        {
            printf("  ");

            for(j=1;j<=spacecount;j++)
                {
                printf("%d",arr[arrcount++]);
                if(arrcount>=size)
                break;
                }
            
            printf("\n ");

            for(j=1;j<=spacecount;j++)
                {
                printf("%d",arr[arrcount++]);
                if(arrcount>=size)
                break;
                }
            
            printf("\n");
            space=space+2;
            spacecount--;
        }

        if(i==x)
        {
            for(j=1;j<=xcount;j++)
                {
                printf("%d\n",arr[arrcount++]);
                if(arrcount>=size)
                break;
                }
            
            x=x+2;
            xcount++;
        }

        if(arrcount>=size)
            break;
   }

But I always thought the break statement brings control out of just the nearest loop? I would love to have more clarity on this. What exactly is going on and why did it work? Any help is appreciated.

Edit : Ok I tried more inputs and it printed beyond the size i wanted. So I what I thought initially and what you guys are saying is true. Thank you.


Solution

  • Answering your question:

    When the break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop.

    So yes, you are correct in thinking that the break statement brings control out of just the nearest loop.

    In your example:

    for (j = 1; j <= spacecount; j++) {
        printf("%d", arr[arrcount++]);
        if (arrcount >= size)
            break; // Breaks the inner loop (j)
    }
    if (arrcount >= size)
        break; // Breaks the outer main loop (i)