C break statement

Description

break statement exit a loop.

Syntax

break statement has the following syntax.

break;

Example

Break out of a while loop


#include<stdio.h>
/* w  w w .  j av a2 s.co m*/
main(){

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

The code above generates the following result.


Breaking out of a for loop


#include <stdio.h>
 //  w  w  w .  j ava  2 s  .co  m
int main()
{
    char ch;
 
    puts("Start typing");
    puts("Press ~ then Enter to stop");
 
    for(;;)
    {
        ch=getchar();
        if(ch=='~')
        {
            break;
        }
    }
    printf("Thanks!\n");
    return(0);
}

The code above generates the following result.





















Home »
  C Language »
    Language Basics »




C Language Basics
Data Types
Operator
Statement
Array