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 -...

C LANGUAGE - OVERVIEW C - VARIABLES

C LANGUAGE - OVERVIEW Advertisements C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972. In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available description of C, now known as the K&R standard. The UNIX operating system, the C compiler, and essentially all UNIX application programs have been written in C. C has now become a widely used professional language for various reasons − Easy to learn Structured language It produces efficient programs It can handle low-level activities It can be compiled on a variety of computer platforms Facts about C C was invented to write an operating system called UNIX. C is a successor of B language which was introduced around the early 1970s. The language was formalized in 1988 by the American National Standard Institute  A N S I A N S ...

environment

It looks like you meant "environment." The environment refers to the surroundings or conditions in which a person, animal, or plant lives or operates. It encompasses various elements, including natural resources, ecosystems, climate, and human-made structures. Protecting and preserving the environment is crucial for the well-being of all living organisms on Earth. Is there anything specific you'd like to discuss or learn about regarding the environment?