C Programming
C programming break and continue
statements
Register for Canton Fair
Now
Discover endless business opportunities at
the world’s largest trading event - Canton
Fair
Canton Fair
Sign Up
In this tutorial, you will learn about c programming break continue statements.
Break and continue statements are used to jump out of the loop and continue
looping.
Break and continue statements in c
Break and continue statements in c
Till now, we have learned about the looping with which we can repeatedly execute
the code such as, for loop and while & do … while loop.
Just think what will you do when you want to jump out of the loop even if the
condition is true or continue repeated execution of code skipping some of the parts?
For this C provides break and continue statements. By the help of these
statements, we can jump out of loop anytime and able continue looping by skipping
some part of the code.
The break statement in C
In any loop break is used to jump out of loop skipping the code below it without
caring about the test condition.
It interrupts the flow of the program by breaking the loop and continues the
execution of code which is outside the loop.
The common use of break statement is in switch case where it is used to skip
remaining part of the code.
How does break statement works?
Structure of Break statement
In while loop
while (test_condition)
{
statement1;
if (condition )
break;
statement2;
}
In do…while loop
do
{
statement1;
if (condition)
break;
statement2;
}while (test_condition);
In for loop
for (int-exp; test-exp; update-exp)
{
statement1;
if (condition)
break;
statement2;
}
Now in above structure, if test_condition is true then the statement1 will be
executed and again if the condition is true then the program will encounter break
statement which will cause the flow of execution to jump out of loop and
statement2 below if statement will be skipped.
Programming Tips
break statement is always used with if statement inside a loop and loop
will be terminated whenever break statement is encountered.
Example: C program to take input from the user until he/she
enters zero.
#include <stdio.h>
int main ()
{
int a;
while (1)
{
printf("enter the number:");
scanf("%d", &a);
if ( a == 0 )
break;
}
return 0;
}
Explanation
In above program, while is an infinite loop which will be repeated forever and there
is no exit from the loop.
So the program will ask for input repeatedly until the user will input 0.
Air Medical Transport Services in Pātrun
Safe and comfortable air transportation for all
patients in need.
sponsored by: Medical Transport LEARN M…
When the user enters zero, the if condition will be true and the compiler will
encounter the break statement which will cause the flow of execution to jump out
of the loop.
Air Medical Transport Services in Pātrun
Safe and comfortable air transportation for all patients in need.
sponsored by: Medical Transport
LEARN MORE
The continue statement in C
Like a break statement, continue statement is also used with if condition
inside the loop to alter the flow of control.
When used in while, for or do...while loop, it skips the remaining statements in
the body of that loop and performs the next iteration of the loop.
Unlike break statement, continue statement when encountered doesn’t
terminate the loop, rather interrupts a particular iteration.
How continue statement work?
Structure of continue statement
In while loop
while (test_condition)
{
statement1;
if (condition )
continue;
statement2;
}
In do…while loop
do
{
statement1;
if (condition)
continue;
statement2;
}while (test_condition);
In for loop
for (int-exp; test-exp; update-exp)
{
statement1;
if (condition)
continue;
statement2;
}
Explanation
Explanation
In above structures, if test_condition is true then the continue statement will
interrupt the flow of control and block of statement2 will be skipped, however,
iteration of the loop will be continued.
Example: C program to print sum of odd numbers between 0
and 10
#include <stdio.h>
int main ()
{
int a,sum = 0;
for (a = 0; a < 10; a++)
{
if ( a % 2 == 0 )
continue;
sum = sum + a;
}
printf("sum = %d",sum);
return 0;
}
Output
sum = 25
Air Medical Transport Services in Pātrun
Safe and comfortable air transportation for all patients in need.
sponsored by: Medical Transport
LEARN MORE
Air Medical Transport Services in Pātrun
Safe and comfortable air transportation for all
patients in need.
sponsored by: Medical Transport LEARN M…
All rights reserved @trytoprogram.com