WHAT'S NEW?
Loading...

break and continue Statement in C Programming

break Statement
The break statement terminates the execution of the loop and the control is transferred to the statement immediately following the loop. Generally, the loop is terminated when its test condition becomes false. But if we have to terminate the loop instantly without testing loop termination condition, the break statement is useful. The syntax for this is
          break; 
break statement is also used in switch  statement which causes a transfer of control out of the entire switch statement.

Example :
#include<stdio.h>
void main(){
 int i; 
 for(i=1; i<=10; i++){
  printf("%d\n", i); 
  if(i==7)
   break; 
 }
        break;  
}
Output
1
2
3
4
5
6
7

In the above program we have defined 10 loops in for statement, but we set the condition, if the value of looping variable is 7 then break the loop. That's why in the 7'th loop it breaks the loop and print the numbers from 1 to 7 only. 

continue Statement 
The continue statement is used to bypass the remainder of the current pass through a loop. The loop does not terminate when a continue statement is encountered. instead, the remaining loop statements are skipped and the computation proceeds directly to the next pass through the loop. The syntax for this statement is:
       continue; 

Example:
#include<stdio.h>
void main(){
 int i; 
 for(i=1; i<=10; i++){
  if(i==7)
   continue; 
  printf("%d\n", i); 
 }
 getch(); 
}
Output
1
2
3
4
5
6
8
9
10
In the above program we saw that in the output number 7 is missing, this is because of the continue statement. It spiked the 7'th loop from the program.

0 comments:

Post a Comment