C Tutorial/Statement/Break — различия между версиями


Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/includes/exception/MWExceptionHandler.php on line 179

Deprecated: Creation of dynamic property PPDStack::$accum is deprecated in /var/www/cppe.ru/www/includes/parser/Preprocessor_DOM.php on line 803

Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/includes/exception/MWExceptionHandler.php on line 179

Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/vendor/mediawiki/at-ease/src/Functions.php on line 48
Материал из C\C++ эксперт
Перейти к: навигация, поиск
м (1 версия: Импорт контента...)
 
(нет различий)

Текущая версия на 10:32, 25 мая 2010

Breaking out of a for loop

#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);
}
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.


#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;
        }
    }
}
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