Differentiate between break and System. Exit (0)
Answers
Answer:
break is a keyword that exits the current construct like loops. exit is a non-returning function that returns the control to the operating system. For example:
// some code (1)
while(true)
{
...
if(something)
break;
}
// some code (2)
In the above code, break exits the current loop which is the while loop. i.e. some code (2) shall be executed after breaking the loop.
For exit, it just gets out of the program totally:
// some code (1)
while(true)
{
...
if(something)
exit(0);
}
// some code (2)
You would get out of the program. i.e. some code (2) is not reached in the case of exit().
Answer:
Break:
it is used to terminate the loop or condition.
Example:
int main(){
int a;
for(a=0;a<10;a++){
if(a==5)
break;
else
printf("*");
}
printf("hii");
}
//after 5 reached the loop will be terminated
//output
* * * * * hii
Exit:
it is used to totally terminate from the program.
#include<stdio.h>
#include<stdlib.h>
int main()
{
printf("Hello");
exit(0);
printf("Hi");
return 0;
}
//after exit(0) program will not execute.
//output
Hello
Explanation: