0% found this document useful (0 votes)
872 views

C Programming - Break and Continue Statement

The document discusses the break and continue statements in C programming. Break is used to terminate a loop immediately, while continue skips the remaining statements in the current loop iteration and continues with the next one. An example uses break to end a loop that calculates the average of positive numbers entered by the user, terminating early if a negative number is input. Another example uses continue to skip multiplying by 0 when calculating the product of four integers entered by the user. Both statements can be used with loops like for, while, and do-while to control program flow.

Uploaded by

R-jayVenturillo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
872 views

C Programming - Break and Continue Statement

The document discusses the break and continue statements in C programming. Break is used to terminate a loop immediately, while continue skips the remaining statements in the current loop iteration and continues with the next one. An example uses break to end a loop that calculates the average of positive numbers entered by the user, terminating early if a negative number is input. Another example uses continue to skip multiplying by 0 when calculating the product of four integers entered by the user. Both statements can be used with loops like for, while, and do-while to control program flow.

Uploaded by

R-jayVenturillo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

C Programming break and continue Statement

There are two statement built in C, break; and continue; to interrupt the normal flow of control
of a program. Loops performs a set of operation repeately until certain condition becomes false but,
it is sometimes desirable to skip some statements inside loop and terminate the loop immediately
without checking the test expression. In such cases, break and continue statements are used.
break Statement
In C programming, break is used in terminating the loop immediately after it is encountered. The
break statement is used with conditional if statement.
Syntax of break statement
break;
The break statement can be used in terminating all three loops for, while and do...while loops.

The figure below explains the working of break statement in all three type of loops.

Example of break statement


Write a C program to find average of maximum of n positive numbers entered by user. But, if
the input is negative, display the average(excluding the average of negative input) and end the
program.

/* C program to demonstrate the working of break statement by terminating a loop, if user


inputs negative number*/
# include <stdio.h>
int main(){
float num,average,sum;
int i,n;
printf("Maximum no. of inputs\n");
scanf("%d",&n);
for(i=1;i<=n;++i){
printf("Enter n%d: ",i);
scanf("%f",&num);
if(num<0.0)
break;

//for loop breaks if num<0.0

sum=sum+num;
}
average=sum/(i-1);
printf("Average=%.2f",average);
return 0;
}
Output
Maximum no. of inputs
4
Enter n1: 1.5
Enter n2: 12.5
Enter n3: 7.2
Enter n4: -1

Average=7.07
In this program, when the user inputs number less than zero, the loop is terminated using break
statement with executing the statement below it i.e., without executing sum=sum+num.
In C, break statements are also used in switch...case statement. You will study it in C switch...case
statement chapter.
continue Statement
It is sometimes desirable to skip some statements inside the loop. In such cases, continue statements
are used.
Syntax of continue Statement
continue;
Just like break, continue is also used with conditional if statement.

For better understanding of how continue statements works in C programming. Analyze the figure
below which bypasses some code/s inside loops using continue statement.

Example of continue statement


Write a C program to find the product of 4 integers entered by a user. If user enters 0 skip it.

//program to demonstrate the working of continue statement in C programming


# include <stdio.h>
int main(){
int i,num,product;
for(i=1,product=1;i<=4;++i){
printf("Enter num%d:",i);
scanf("%d",&num);
if(num==0)
continue; / *In this program, when num equals to zero, it skips the statement
product*=num and continue the loop. */
product*=num;
}
printf("product=%d",product);
return 0;
}

Output
Enter num1:3
Enter num2:0
Enter num3:-5
Enter num4:2
product=-30

You might also like