Loop Control Statements in C Introduction to HTML How to use the Github API The image tag, anchor tag and the button tag Ordered and Unordered Lists in HTML The division tag HTML Forms Tables in HTML Introduction to C Programming Introduction to Python Varibles and Datatypes in Python Operators in Python Typecasting in Python Input and Output in Python If Else in Python Loops in Python Break, Continue and Pass in Python Python practice section 1 Lists in Python Tuple in Python

Loop Control Statements

We have already looked at the working of loops. The loop continues to execute the statements inside it till the termination condition is met. However, we can interpret this flow such as terminating the loop early or skipping an iteration. This can be done by using loop control statements such as break, continue, and goto. In this article, we will look at them in detail and why we need them.

Break statement

It is used inside the body of a loop to terminate it early i.e we can terminate the loop even when the termination condition is not met by using break. Suppose that you want to print the smallest divisor of a given number n.We will start from 2 and go all the way till n-1 and check if that number is divisible by n. Since, we already know that the number is divisible by 1 and itself. This can be done easily using loops.

    #include <stdio.h>
    void main()
    {
        int n;
        printf("Enter a number : ");
        scanf("%d",&n);
        for(int i = 2 ; i < n ; i++)
        {
         if(n%i == 0)      
         printf("The smallest divisor of %d = %d \n",n,i);
        } 
    }
                
Let's check if the above program gives the correct results.

Output 1

    Enter a number : 4
    The smallest divisor of 4 = 2
    
Indeed it gave the correct results! Let's check for some other number.

Output 2

    Enter a number : 6
    The smallest divisor of 6 = 2
    The smallest divisor of 6 = 3
    
Wait ! The smallest divisor of 6 is indeed 2 but we don't want 3. After finding the first divisor, we want the loop to terminate. But since the loop is not over yet, it keeps on checking for the remaining numbers as well. We don't want to print all the divisors of a number. This is where the use of break statement comes in handy. When we get the first divisor, we will use the break statement to end the loop.

    #include <stdio.h>
    void main()
    {
    int n;
    printf("Enter a number : ");
    scanf("%d",&n);
        for(int i = 2 ; i < n ; i++)
        {
            if(n%i == 0)      
            {
                printf("The smallest divisor of %d = %d \n",n,i);
                break;
            }
        } 
    }
        
Thats a small change to the program we wrote earlier.Lets re-run it now.
    Enter a number : 6
    The smallest divisor of 6 = 2
    
As we can see, now it did not print 3.Let's check for some other number as well.
    Enter a number : 25
    The smallest divisor of 25 = 5
    
Now our program is ready. It just gives us the smallest divisor of a number.

Continue Statement

It is also used inside the body of a loop to skip a particular iteration. For example - you have to print all the numbers from 1 to n except the divisors of 3. This is how you would do using the continue statement.

    #include <stdio.h>
    void main()
    {
    int n;
    printf("Enter a number : ");
    scanf("%d",&n);
        for(int i = 1 ; i <= n ; i++)
        { 
            if(i%3 == 0)
            continue;
            printf("%d ",i);
        } 
    }
        

Output

    Enter a number : 10
    1 2 4 5 7 8 10
    
When the if condition is true, we use the continue statement to skip the rest of the statements in the for loop. When i is not divisible by 3, we print it otherwise we do not print it with the help of continue statement. Hence, the continue statement did not terminate the loop but just skipped the execution of the statements in an iteration of the loop.

Goto statement

The goto statement is used when we want to skip some of the statement(s) in our program. It can be used anywhere in the program. The goto statement has a label (you can give any valid name to the label) which indicates where to go in the program. A label is defined by writing the label name along with colon(:).You can write any statement in the label.
Consider the following example.

        #include <stdio.h>
        void main()
        {   
            int n;
            printf("Enter a number : ");
            scanf("%d",&n);
            if(n%5 == 0)
            goto rapid;
            n*=2;
            rapid : printf("Your number is : %d ",n); 
        }
      

Output 1

    Enter a number : 3
    Your number is : 6
    

Output 2

    Enter a number : 5
    Your number is : 5
    
When n is divisible by 5, we print it as it is. We skipped the statement where it is multiplied by 2 by using the goto statement. It goes directly to the rapid label and then prints the number. Otherwise, we multiply n by 2 and then print it.