What is the difference between a break and an exit?

Even I have used break () and exit () many times, I am a little confused between them. I need to know the exact meaning of both when we should use them. Please explain with a small example. Thank.

+5
source share
3 answers

breakis a keyword that completes the current construct, such as loops. exitis a function non-returningthat returns a control to the operating system. For example:

// some code (1)
while(true)
{
   ...
   if(something)
     break;
}
// some code (2)

In the above code, the interrupt terminates the current loop, which is the while loop. that is, some code (2) must be executed after breaking the loop.

To exit, it simply exits the program completely:

// some code (1)
while(true)
{
   ...
   if(something)
     exit(0);
}
// some code (2)

. (2) exit().

+20

break - . , , , - , ( switch)

 while (...) {  /* same for "do {} while" or "for" */ 
   ... 
   break;  -----+    
   ...          |  
 }              |
 ....       <---+  JUMP HERE!



 switch (...) {
   ... 
   break;  -----+    
   ...          |  
 }              |
 ....       <---+  JUMP HERE!

exit(), , , , , . , , . atexit() ( C99), , .

+8

break .

.

#include<stdio.h>
#include<stdlib.h>
main()
{
        int d;
        while(1)
        {
        scanf("%d",&d);
        if(d==1)
        {
                break;
        }
        else if(d==4)
        {
                exit(0);
        }
        }
        printf("WELCOME YOU MATCH BREAK\n");
}

If you press 1, it will exit the loop. Not from the program. So this time it will print a line.

If you press 4, it will exit program, it will not print a line.

+2
source

All Articles