defination
syntax
flowchart
example
There are two Statements built in C programming, break; and continue; to alter the normal flow of a program. loops perform a set of repetitive task until text expression becomes false but it is sometimes desirable to skip some statement/s inside loop or terminate the loop immediately without checking the test expression. In such cases, break and continue statement are used. The break; statement is also used in switch statement to exit switch statements.
break Statement
In C programming, break is used in terminating the loop immediately after it is encountered. The break statement is used with conditional if statement.
Syntax :
break;
working:
Example of break statement
Write a C program to find average of maximum of n positive numbers entered by user. But, if the input is negative, display the average(excluding the average of negative input) and end the program.
/* C program to demonstrate the working of break statement by terminating a loop, if user inputs negative number*/
# include <stdio.h>
int main(){
float num,average,sum;
int i,n;
printf("Maximum no. of inputs\n");
scanf("%d",&n);
for(i=1;i<=n;++i){
printf("Enter n%d: ",i);
scanf("%f",&num);
if(num<0.0)
break; //for loop breaks if num<0.0
sum=sum+num;
}
average=sum/(i-1);
printf("Average=%.2f",average);
return 0;
}
Output
Maximum no. of inputs
4
Enter n1: 1.5
Enter n2: 12.5
Enter n3: 7.2
Enter n4: -1
Average=7.07
In this program, when the user inputs number less than zero, the loop is terminated using break statement with executing the statement below it i.e., without executing sum=sum+num.
In C, break statements are also used in switch...case statement.
unconditional statements
syntax
flowchart
example
There are two Statements built in C programming, break; and continue; to alter the normal flow of a program. loops perform a set of repetitive task until text expression becomes false but it is sometimes desirable to skip some statement/s inside loop or terminate the loop immediately without checking the test expression. In such cases, break and continue statement are used. The break; statement is also used in switch statement to exit switch statements.
break Statement
In C programming, break is used in terminating the loop immediately after it is encountered. The break statement is used with conditional if statement.
Syntax :
break;
working:
Example of break statement
Write a C program to find average of maximum of n positive numbers entered by user. But, if the input is negative, display the average(excluding the average of negative input) and end the program.
/* C program to demonstrate the working of break statement by terminating a loop, if user inputs negative number*/
# include <stdio.h>
int main(){
float num,average,sum;
int i,n;
printf("Maximum no. of inputs\n");
scanf("%d",&n);
for(i=1;i<=n;++i){
printf("Enter n%d: ",i);
scanf("%f",&num);
if(num<0.0)
break; //for loop breaks if num<0.0
sum=sum+num;
}
average=sum/(i-1);
printf("Average=%.2f",average);
return 0;
}
Output
Maximum no. of inputs
4
Enter n1: 1.5
Enter n2: 12.5
Enter n3: 7.2
Enter n4: -1
Average=7.07
In this program, when the user inputs number less than zero, the loop is terminated using break statement with executing the statement below it i.e., without executing sum=sum+num.
In C, break statements are also used in switch...case statement.
unconditional statements
No comments:
Post a Comment