Break and Continue Statement in C


The continue statement in C programming works somewhat like the break statement. Instead of forcing termination, it forces the next iteration of the loop to take place, skipping any code in between.

Break :
   By using break,you can force immediate termination of a loop, bypassing the conditional expression and any remaininh code in the body of the loop.When a break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop.

Continue :
   The continue statement performs such an action. In for, while and do-while loops, a continue statement causes control to be transferred directly to the conditional expression that controls the loop.

This program calculates the sum of a given set of numbers, with the condition that if a number entered is zero, it will be ignored and not added to the final sum. The program first takes a limit 'n' as input from the user, using the scanf function. It then enters a for loop that runs 'n' times. This program is a simple C program that takes input from the user to define a limit (n) and then prompts the user to enter n numbers. The program then uses a for loop to iterate from 1 to n, reading in each number using the scanf() function. The program checks if the entered number is 0, if it is, the program skips the current iteration and continues to the next iteration. If the number is not 0, the program adds the number to a running sum. Finally, the program prints out the total sum of all the non-zero numbers entered by the user.

Source Code

//Break & Continue
#include<stdio.h>
int main()
{
    int i,n,num,sum=0;
    printf("\nEnter The Limit : ");
    scanf("%d",&n);
    for(i=1; i<=n; i++)
    {
        printf("\nEnter The Number : ");
        scanf("%d",&num); //1 2 3 0 5
        if(num==0)
            continue;
       sum+=num;
    }
    printf("\nTotal : %d",sum);
    return 0;
}
 

Output

Enter The Limit : 5

Enter The Number : 11

Enter The Number : 12

Enter The Number : 13

Enter The Number : 14

Enter The Number : 15

Total : 65
To download raw file Click Here

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C