0 votes
128 views
in C programming by (98.9k points)
edited
Write syntax of break and continue statement with example.

1 Answer

0 votes
by (98.9k points)
selected by
 
Best answer

It is sometimes desirable to skip some statements inside the loop or terminate the loop immediately without checking the test expression. In such cases, break and continue statements are used.
break Statement :-
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. In C programming, break statement is also used with switch…case statement. 

Syntax of break statement :
break;

// Program to calculate the sum of maximum of 10 numbers
// Calculates sum until user enters positive number
# include <stdio.h>
# include <conio.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 user enters negative number, loop is terminated
if(number < 0.0)
  {
             break;
  }
sum += number; // sum = sum + number;
}
printf("Sum = %.2lf",sum);
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

continue Statement :-
      The continue statement skips some statements inside the loop. The continue statement is used with decision making statement such as if…else.
Syntax of continue Statement :
continue; 

// Program to calculate sum of maximum of 10 numbers
// Negative numbers are skipped from calculation
# include <stdio.h>
# include <conio.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 user enters negative number, loop is terminated
if(number < 0.0)
{
continue;
}
sum += number; // sum = sum + number;
}
printf("Sum = %.2lf",sum);
return 0;
}

Output : 

Enter a n1: 1.1
Enter a n2: 2.2
Enter a n3: 5.5
Enter a n4: 4.4
Enter a n5: -3.4
Enter a n6: -45.5
Enter a n7: 34.5
Enter a n8: -4.2
Enter a n9: -1000
Enter a n10: 12
Sum = 59.70

Related questions

0 votes
1 answer 110 views
0 votes
1 answer 128 views
0 votes
1 answer 130 views
0 votes
1 answer 126 views
0 votes
1 answer 94 views
0 votes
1 answer 94 views
asked Jun 11, 2022 in Discuss by nehapatil (3.2k points)

Doubtly is an online community for engineering students, offering:

  • Free viva questions PDFs
  • Previous year question papers (PYQs)
  • Academic doubt solutions
  • Expert-guided solutions

Get the pro version for free by logging in!

5.7k questions

5.1k answers

108 comments

537 users

...