Friday, July 29, 2016

C Basics - The break Statement in C

We often come across situations where we want to jump out of a loop instantly, without waiting to get back to the conditional test. The keyword break allows us to do this.

The syntax for a break statement is,

break;

When break is encountered inside any loop, control automatically passes to the first statement after the loop. It can be used to terminate a case in the switch statement. In nested loops, the break statement will stop the execution of the innermost loop and start executing the next line of code after the inner loop code block.

#include <stdio.h>
 
int main () {

   int i = 1;

   while( i < 10 ) {
   
      printf("value of i is: %d\n", i);
      i++;
  
      if( i == 5) {
         break;
      }
  
   }
 
   return 0;
}

Output of above program,

value of i is: 1
value of i is: 2
value of i is: 3
value of i is: 4



Related topics:
Loops in C   |   The for Loop in C   |   The while Loop in C   |   The do-while Loop in C   |   Nested Loop in C   |   Infinite Loop in C   |   Loop Control Statements in C   |   The continue Statement in C   |   The goto Statement in C   |   The return Statement in C

List of topics: C Programming

No comments:

Post a Comment