Break Statement

Break statement:

it is used to terminate the while loops, switch case loops and for loops from the subsequent execution.
• Syntax: break;

break

 

Example

#include<stdio.h>

int main()

{

int p;

for(p=0;p<10;p++)

{

if(p==5)

{

printf(“\nComing out of for loop when p = 5″);

break;

}

printf(“%d “,p);

}

return 0;

}

Output

0 1 2 3 4

Coming out of for loop when i = 5

WAP to calculate the sum of maximum of 10 numbers and Calculates sum until user enters positive number

# include <stdio.h>

int main()

{

int p;

double number1, sum1 = 0.0;

for(p=1; p <= 10; ++p)

{

printf(“Enter a n%d: “,p);

scanf(“%lf”,&number1);

// If user enters negative number, loop is terminated

if(number1 < 0.0)

{

break;

}

sum1 += number1; // sum = sum + number;

}

printf(“Sum = %.2lf”,sum1);

return 0;

}

Output
Enter a n1: 2.4

Enter a n2: 4.5

Enter a n3: 3.4

Enter a n4: -3

Sum = 10.30

 

Leave a Reply

Your email address will not be published. Required fields are marked *