The break Statement in a Loop - C Statement

C examples for Statement:break

Introduction

The break statement works the same way within the body of a loop-any kind of loop.

For instance:

Demo Code

#include <stdio.h> 
#include <stdlib.h> 
#include <ctype.h> 

int main(void) {

   char answer = 0;
   for (;; )/*from  w  w  w .  ja v a  2 s.  c  om*/
   {
      printf("Do you want  to enter some more(y/n): ");
      scanf("%c", &answer);
      if (tolower(answer) == 'n')
         break;                               // Go to statement after the loop
   }

   return 0;
}

Result

Here you have a loop that will potentially execute indefinitely until a n letter input.


Related Tutorials