Continue Statement in C: What is Break & Continue Statement in C with Example

Continue Statement in C: What is Break & Continue Statement in C with Example

27 May 2024
Beginner
20.6K Views
16 min read
Learn via Video Course & by Doing Hands-on Labs

C Programming For Beginners Free Course

Break and Continue Statements in C: An Overview

Learning to code can be a difficult endeavor, especially when it comes to techniques used to control program flow. It is important to know the use of break and continue statements in Learn C for writing efficient and organized code. If you're looking for comprehensive guidance on using these statements, consider exploring C Online Training.

What is the Continue Statement in C?

The continue statement is used to skip the current iteration of any loop and bring the control to the beginning of the iteration. The statements following the continue statement are skipped and the iteration starts again.

continue statement in c flowchart

Syntax

//loop statements 
continue; 
//some lines of the code which is to be skipped

Example of Continue Statement in C


#include<stdio.h> 
void main () 
{
for (int i = 1; i <= 10; i++) {
 if (i == 5) {
 continue;
 }
 printf("%d\n", i);
}
  • This C program in the C Compiler iterates 10 times using a for loop.
  • The continue statement is encountered when i=5. So it skips the remainder of the loop's body for that iteration.
  • As a result, it only prints the numbers 1-4 and 6-10, skipping out 5.

Output

1
2
3
4
6
7
8
9
10

Read More - Top 50 C Interview Questions and Answers

What is the Break Statement in C?

If you remember we already came across the break statement while learning the switch statement. We used to check each case in switch and after finding the correct match used to get out using break.

  • It is one of the most used jump statements.
  • In C, break is used to prematurely exit from a loop or switch statement, before the block has been fully executed.
  • As soon as a break statement is encountered inside a loop, the loop terminates immediately, and control is transferred to the next statement outside the loop.

Syntax

//loop or switch case 
break;

Example of Break Statement in C


#include <stdio.h>
int main()
{
 for(int i=1; i <=10; i++)
 {
 if(i == 5)
 {
 break; // terminates the loop when i is equal to 5
 }
 printf("%d\n", i);
 }
 return 0;
}
  • The above C code iterates from 1 to 10 using a for loop. It determines whether the value of i inside the loop equals 5.
  • It outputs numbers from 1 to 4 (inclusive) as a result
  • When i=5, the break statement is executed, which abruptly ends the loop.

`Output

1
2 
3 
4

Where to use Break Statements in C

We can use break statements in two ways in C:

  1. Break statement in switch case
  2. Break statement in a loop

  1. Break Statement in Switch Case in C

The break statement is used inside a switch statement to terminate the execution of the enclosing switch statement and exit the block of code. When a break statement is encountered inside a switch block, the control flow is transferred to the end of the switch block, and the remaining case statements are skipped.

You have already executed this while learning the switch case in C.

Syntax

switch (expression) {
 case 1:
 // code to be executed if expression is equal to 1
 break;
 case 2:
 // code to be executed if expression is equal to 2
 break;
 case 3:
 // code to be executed if expression is equal to 3
 break;
 default:
 // code to be executed if expression doesn't match any case
 break;
}

  1. Break statement in a loop in C

We will see here, how to use the break statement in all three types of loops

Break statement in a loop in C

  • Break statement in a for loop
  • The break statement can be used anywhere inside the for loop's body. When executed, it immediately terminates the loop, and control is transferred to the statement following the loop.

    The example you have executed above for the break statement is an example of a break statement in a for loop.

    Example of Break statement in a for loop
    
    #include <stdio.h>
    int main()
    { 
    for (int i = 1; i <= 10; i++) 
    { if (i == 5)
    { break; // terminates the loop when i is equal to 5
    }
    printf("%d\n", I);
    }return 0;
    }

    The above C code in the C Editor iterates from 1 to 10 using a for loop. It determines whether the value of i inside the loop equals 5.

    It outputs numbers from 1 to 4 (inclusive) as a result

    When i=5, the break statement is executed, which abruptly ends the loop.

    Output

    1
    2
    3
    4
    

  • Break statement in a while loop
  • Example of Break statement in a while loop
    
    #include<stdio.h>
    void main () 
    {
    int i = 0;
    while(1) 
    { printf("%d\n",i); 
     i++; 
    if(i == 5) 
    break;
    }
    printf("The loop terminates");
    }

    Here the loop would have become an infinite one had the break statement not been there. When i becomes 5, the control comes out of the otherwise infinite while loop.

    Output
    0
    1
    2
    3
    4
    The loop terminates 

  • Break statement in a do...while loop
  • Example of Break statement in a do...while loop
    
    #include<stdio.h> 
    
    void main () 
    {
    int n=1,i,choice; 
    do 
     { 
     i=1; 
     while(i<=10) 
     { 
     printf("%d X %d = %d\n",n,i,n*i); 
     i++; 
     } 
     printf("Do you want to further print the table of %d , enter any non-zero value to continue.",n+1); 
     scanf("%d",&choice); 
     if(choice == 0) 
     { 
     break; 
     } 
     n++; 
    }while(1); 
    }
    Output
    1 X 1 = 1
    1 X 2 = 2
    1 X 3 = 3
    1 X 4 = 4
    1 X 5 = 5
    1 X 6 = 6
    1 X 7 = 7
    1 X 8 = 8
    1 X 9 = 9
    1 X 10 = 10
    Do you want to further print the table of 2, enter any non-zero value to continue.2
    2 X 1 = 2
    2 X 2 = 4
    2 X 3 = 6
    2 X 4 = 8
    2 X 5 = 10
    2 X 6 = 12
    2 X 7 = 14
    2 X 8 = 16
    2 X 9 = 18
    2 X 10 = 20
    Do you want to further print the table of 2, enter any non-zero value to continue.0

    Difference between Break and Continue Statements in C

    ParametersBreak Statement in CContinue Statement in C
    PurposeExits the current loopSkips the current iteration
    ApplicabilityUsed within loops (for, while, do-while)Used within loops (for, while, do-while)
    EffectTerminates the loop prematurely and continues with the next statement after the loopImmediately moves to the next iteration of the loop, skipping the remaining code in the current iteration
    Loop ControlAffects the entire loopAffects only the current iteration of the loop
    Typical Use CasesWhen a specific condition is met, and you want to exit the loopWhen you want to skip the current iteration of the loop based on a certain condition and continue with the next iteration
    Examplefor (int i = 1; i <= 10; i++) { if (i == 5) { break; } printf("%d\n", i); }for (int i = 1; i <= 10; i++) { if (i == 5) { continue; } printf("%d\n", i); }

    Summary

    To summarize, breaks and continues allow for more advanced flexibility in C programming. These break and continue statements in the C example, and different types of break and continue statements in the C language, allow the programmer to control the content of the loop, which can save a lot of time down the line. Therefore, understanding how these statements work and when to use them is important when developing in C. It's also crucial to explore other online resources, including C Certification courses, to gain a comprehensive understanding of this break and continue statement in C and become a better programmer.

    Share Article

    Live Classes Schedule

    Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
    Angular Certification Course Jun 16 SAT, SUN
    Filling Fast
    09:30AM to 11:30AM (IST)
    Get Details
    Advanced Full-Stack .NET Developer Certification Training Jun 17 MON, WED, FRI
    Filling Fast
    07:00AM to 08:30AM (IST)
    Get Details
    ASP.NET Core Certification Training Jun 17 MON, WED, FRI
    Filling Fast
    07:00AM to 08:30AM (IST)
    Get Details
    .NET Microservices Certification Training Jun 23 SAT, SUN
    Filling Fast
    05:30PM to 07:30PM (IST)
    Get Details
    ASP.NET Core (Project) Jun 23 SAT, SUN
    Filling Fast
    08:30PM to 10:30PM (IST)
    Get Details
    Azure Developer Certification Training Jun 23 SAT, SUN
    Filling Fast
    07:00AM to 09:00AM (IST)
    Get Details
    React JS Certification Training | Best React Training Course Jun 30 SAT, SUN
    Filling Fast
    08:30PM to 10:30PM (IST)
    Get Details
    ASP.NET Core Certification Training Jun 30 SAT, SUN
    Filling Fast
    10:00AM to 12:00PM (IST)
    Get Details
    Advanced Full-Stack .NET Developer Certification Training Jun 30 SAT, SUN
    Filling Fast
    10:00AM to 12:00PM (IST)
    Get Details
    Generative AI For Software Developers Jul 14 SAT, SUN
    Filling Fast
    08:30PM to 10:30PM (IST)
    Get Details

    Can't find convenient schedule? Let us know

    About Author
    Sakshi Dhameja (Author and Mentor)

    She is passionate about different technologies like JavaScript, React, HTML, CSS, Node.js etc. and likes to share knowledge with the developer community. She holds strong learning skills in keeping herself updated with the changing technologies in her area as well as other technologies like Core Java, Python and Cloud.

    Self-paced Membership
    • 22+ Video Courses
    • 800+ Hands-On Labs
    • 400+ Quick Notes
    • 55+ Skill Tests
    • 45+ Interview Q&A Courses
    • 10+ Real-world Projects
    • Career Coaching Sessions
    • Email Support
    Upto 60% OFF
    Know More
    Accept cookies & close this