Break and Continue Statement in C
In this post, We will learn about the break and continue statement in c programming language.
In this post, We will learn about the break and continue statement in c programming language.
Break Statement in C
The break statement terminates the loop (for, while and do while loop) immediately when it is encountered. The break statement is used with decision-making statement such as if...else.
Syntax Break
break;
Example: write a program to calculate the sum numbers (maximum 10 numbers). Calculates sum until the user enters a positive number
# include <stdio.h>
int main()
{
int i;
double number, sum = 0.0;
for(i=1; i <= 10; ++i)
{
printf("Enter a n%d: ",i);
scanf("%lf",&number);
if(number < 0.0)
{
break;
}
sum += number;
}
printf("Sum = %.2lf",sum);
return 0;
}
Continue Statement in C
The continue statement skips some statements inside the loop. The continue statement is used with decision-making statement such as if...else.
Syntax Continue
continue;
Example: write a program to calculate sum numbers (maximum of 10 numbers). skip the negative numbers from the calculation.
# include <stdio.h>
int main()
{
int i;
double number, sum = 0.0;
for(i=1; i <= 10; ++i)
{
printf("Enter a n%d: ",i);
scanf("%lf",&number);
if(number < 0.0)
{
continue;
}
sum += number;
}
printf("Sum = %.2lf",sum);
return 0;
}
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Comments
Post a Comment