Skip to main content

C Programming break and continue Statement

C Programming break and continue Statement

The break statement terminates the loop, whereas continue statement forces the next iteration of the loop. In this tutorial, you will learn to use break and continue with the help of examples.
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 (forwhile and do...while loop) immediately when it is encountered. Its syntax is:
break;
The break statement is almost always used with if...else statement inside the loop.

How break statement works?

Working of break statement

Example 1: break statement

  1. // Program to calculate the sum of maximum of 10 numbers
  2. // If negative number is entered, loop terminates and sum is displayed
  3. # include <stdio.h>
  4. int main()
  5. {
  6. int i;
  7. double number, sum = 0.0;
  8. for(i=1; i <= 10; ++i)
  9. {
  10. printf("Enter a n%d: ",i);
  11. scanf("%lf",&number);
  12. // If user enters negative number, loop is terminated
  13. if(number < 0.0)
  14. {
  15. break;
  16. }
  17. sum += number; // sum = sum + number;
  18. }
  19. printf("Sum = %.2lf",sum);
  20. return 0;
  21. }
Output
Enter a n1: 2.4
Enter a n2: 4.5
Enter a n3: 3.4
Enter a n4: -3
Sum = 10.30
This program calculates the sum of maximum of 10 numbers. Why maximum of 10 numbers? It's because if the user enters negative number, the break statement is executed which terminates the for loop, and sum is displayed.
In C, break is also used with switch statement.

continue Statement

The continue statement skips statements after it inside the loop. Its syntax is:
continue;
The continue statement is almost always used with if...else statement.

How continue statement works?

Working of continue statement in C programming

Example 2: continue statement

  1. // Program to calculate sum of maximum of 10 numbers
  2. // Negative numbers are skipped from calculation
  3. # include <stdio.h>
  4. int main()
  5. {
  6. int i;
  7. double number, sum = 0.0;
  8. for(i=1; i <= 10; ++i)
  9. {
  10. printf("Enter a n%d: ",i);
  11. scanf("%lf",&number);
  12. if(number < 0.0)
  13. {
  14. continue;
  15. }
  16. sum += number; // sum = sum + number;
  17. }
  18. printf("Sum = %.2lf",sum);
  19. return 0;
  20. }
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
In the program, when the user enters positive number, the sum is calculated using sum += number; statement.
When the user enters negative number, the continue statement is executed and skips the negative number from calculation
============================================================


https://babuakash.blogspot.com/2019/06/c-switch.html


NEXT PAGE..

Comments

Popular posts from this blog

short key of c programming

short key of c programming Shortcut Keys(Hot Keys)     Operation    Alt + F  -            Open file Alt + F3 -           Close Alt + F + N  -    New file Alt + X  -           Exit turbo c Alt + F5 -          Output Screen Alt + F + L  -    Check programme load or not. Alt + ENTER  - Full screen Shift + Del -       Cut Shift + Insert -   Paste Ctr + Insert -    Copy Alt+F9 -           Compile a program Ctr + F8           Bbreak point Ctrl+F9 -          To run a program Ctrl+s -...

Unordered List, Nested lists, Description List,TUTE-8

                                                             Unordered List An unordered list can be created with the <ul> tag and each list item can be created with the <li> tag as shown by the example below: <ul>   <li>Item</li>   <li>Another Item</li>   <li>Yet Another Item</li>  </ul> This will produce a bulleted list (which is the default style): You should use ul to display a list of items, where the order of the items is not important. If changing the order of the items makes the list incorrect, you should use <ol>.   Nested lists You can nest lists to represent sub-items of a list item. <ul>   <li>item 1</li>   <li>item 2     <ul>    ...