on
Italy
- Get link
- X
- Other Apps
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 statements are used. The break; statement is also used in switch statement to exit switch statement.break;The break statement can be used in terminating all three loops for, while and do...while loops.
/* C program to demonstrate the working of break statement by terminating a loop, if user inputs negative number*/
# include
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="" 0="" average="" break="" breaks="" code="" for="" if="" loop="" num="" printf="" return="" sum="sum+num;" verage="%.2f">0>
OutputMaximum no. of inputs 4 Enter n1: 1.5 Enter n2: 12.5 Enter n3: 7.2 Enter n4: -1 Average=7.07
sum=sum+num.continue;Just like break, continue is also used with conditional if statement.
//program to demonstrate the working of continue statement in C programming
# include
int main(){
int i,num,product;
for(i=1,product=1;i<=4;++i){
printf("Enter num%d:",i);
scanf("%d",&num);
if(num==0)
continue; / *In this program, when num equals to zero, it skips the statement product*=num and continue the loop. */
product*=num;
}
printf("product=%d",product);
return 0;
}
Enter num1:3 Enter num2:0 Enter num3:-5 Enter num4:2 product=-30
Comments
Post a Comment