C Tutorial/Statement/Break

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Breaking out of a for loop

<source lang="cpp">#include <stdio.h>

int main() {

   char ch;

   puts("Start typing");
   puts("Press ~ then Enter to stop");

   for(;;)
   {
       ch=getchar();
       if(ch=="~")
       {
           break;
       }
   }
   printf("Thanks!\n");
   return(0);

}</source>

Start typing
      Press ~ then Enter to stop
      
      12312
      1
      231
      
      12
      12
      3~
      Thanks!

The break statement

break statement can break any type of loop.


<source lang="cpp">#include<stdio.h> main(){

   int i = 0;
   while (1)
   {
       i = i + 1;
       printf(" the value of i is %d\n",i);
       if (i>5) {
          break;
       }
   }

}</source>

the value of i is 1
      the value of i is 2
      the value of i is 3
      the value of i is 4
      the value of i is 5
      the value of i is 6