C break and continue
Hello Guys, in this post I am going to show you C Break and Continue,
So, Let get started =
C break
Definition = The break statement ends the loop immediately when it is encountered.
Syntax = break;
Flowchart =
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
double number, sum = 0.0;
for (i = 1; i <= 10; ++i) {
printf("Enter n%d: ", i);
scanf("%lf", &number);
// if the user enters a negative number, break the loop
if (number < 0.0) {
break;
}
sum += number; // sum = sum + number;
}
printf("Sum = %.2lf", sum);
}
C continue
Definition = The continue statement skips the current iteration of the loop and continues with the next iteration.
Syntax = continue;
Flowchart =
Example =
#include<conio.h>
#include<stdio.h>
void 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; // sum = sum + number;
}
printf("Sum = %.2lf", sum);
}
No comments:
Post a Comment