Jump statements
Loop performs a set of statements, till the condition becomes false. The number of times a loop is to be repeated is decided in advance and a condition is set. But sometimes it becomes necessary to skip certain part of the loop or terminate the loop if a particular condition is true then jump statements come to action:
C supports four jump statements:
i. break
ii. continue
iii. goto
iv. return
i) break
The break statement is used inside a loop, to directly come out of the loop. We have already used break in switch statement. It is used to skip the execution of the loop any further and transfer the control of the program to the statement following the loop.
Here loop counter condition from 1 to 10 and we will use break and continue when counter’s value is 7, let’s see what will be the output.
#include < stdio.h >
int
main()
{
int
cnt;
for
(cnt=1; cnt<=10; cnt++)
{
printf
(
"%d "
,cnt);
if
(cnt==7)
break
;
printf
(
"OK\n"
);
}
printf
(
"\nAfter the loop cnt= %d"
,cnt);
return
0;
}
ii) continue
Sometimes it is necessary to skip the execution of the statement when certain condition is true. continue does not terminate the loop but jumps directly to the next iteration. In for loop continue brings the control to the next increment or decrement but in while or do while loop continue causes the control of the loop to jump directly to the condition.
When cnt==7, break will transfer the program’s control just after the loop statement ( after the closing brace “}” ).
'); var s = document.createElement('script'); s.type = 'text/javascript'; s.async = true; s.src = 'https://ad.admitad.com/shuffle/289c251618/'+subid_block+'?inject_to='+injectTo; var x = document.getElementsByTagName('script')[0]; x.parentNode.insertBefore(s, x); })();