Does C Have Continue Nd Break
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
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 loopbreak
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 doesbreak 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.
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.
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
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
mclaughlinloste1945.blogspot.com
Source: http://www.trytoprogram.com/c-programming/c-programming-break-continue-statements/
0 Response to "Does C Have Continue Nd Break"
Post a Comment