Learn C - C break






We can use the break statement to exit the for loop.

This example computes the average of an arbitrary number of values:


#include <stdio.h>
#include <ctype.h>                    // For tolower() function
/* w ww . j a va  2 s .  c  om*/
int main(void)
{
  char answer = 'N';                  // Decision to continue the loop
  double total = 0.0;
  double value = 0.0;
  unsigned int count = 0;

  for( ;; )                           // Indefinite loop
  {
    printf("\nEnter a value: ");      // Prompt for the next value
    scanf(" %lf", &value);            // Read the next value
    total += value;                   // Add value to total
    ++count;                          // Increment count of values

    // check for more input
    printf("Do you want to enter another value? (Y or N): ");
    scanf(" %c", &answer);            // Read response Y or N

    if(tolower(answer) == 'n')        // look for any sign of no
       break;                          // Exit from the loop
  }
  // Output the average to 2 decimal places
  printf("\nThe average is %.2lf\n", total/count);
  return 0;
}

The code above generates the following result.





Example

uses break to exit a loop


#include <stdio.h>
int main(void)
{//from www. ja  va 2  s .  co m
    float length, width;
    
    printf("Enter the length of the rectangle:\n");
    while (scanf("%f", &length) == 1)
    {
        printf("Length = %0.2f:\n", length);
        printf("Enter its width:\n");
        if (scanf("%f", &width) != 1)
            break;
        printf("Width = %0.2f:\n", width);
        printf("Area = %0.2f:\n", length * width);
        printf("Enter the length of the rectangle:\n");
    }
    
    return 0;
}

The code above generates the following result.





Example 2

uses a switch statement and break


#include <stdio.h>
#include <ctype.h>
int main(void)
{/*from  w ww.  j  a  v  a  2s. co m*/
    char ch;
    
    printf("Please type in a letter; type # to end.\n");
    while ((ch = getchar()) != '#')
    {
        if('\n' == ch)
            continue;
        if (islower(ch))     /* lowercase only          */
            switch (ch)
        {
            case 'a' :
                printf("a\n");
                break;
            case 'b' :
                printf("b\n");
                break;
            case 'c' :
                printf("c\n");
                break;
            case 'd' :
                printf("d\n");
                break;
            case 'e' :
                printf("e\n");
                break;
            case 'f' :
                printf("f\n");
                break;
            default :
                printf("That's a stumper!\n");
        }                /* end of switch           */
        else
            printf("only lowercase letters.\n");
        while (getchar() != '\n')
            continue;      /* skip rest of input line */
        printf("Please type another letter or a #.\n");
    }                        /* while loop end          */
   
    return 0;
}

The code above generates the following result.