answersLogoWhite

0


Best Answer

A break is a jump statement that allows code to exit a statement block without processing the remaining instructions in that statement block. Execution continues with the statement following the statement block.

Jump statements (the break, return, continue and goto keywords) are typically used in conjunction with a conditional expression, to allow execution to branch to another section of code within the same function whenever the expression is true. Function calls do a similar type of thing, branching off to other sections of code, but execution always returns to the point the call was made. Jump statements do not.

However, jump statements are also used in conjunction with switch statements. If a case label does not have a jump statement before the next case label is encountered, execution will fall through to the next label (just as as if that label did not exist). Sometimes this may be the intention but, in most cases, once a specific case has been handled, execution is normally passed to the statement immediately following the switch block, in which case a break is required (return and goto can also be used to exit a case label, but not continue). The final case (or default case) does not require a break if execution is expected to fall through to the statement following the switch statement.

With regards to the other jump statements:

The continue statement is used in loops, to start a new loop without executing any remaining command in the loop. In do..while() loops, execution is passed to the while(condition) expression.

The goto statement jumps to a given label, which must be present somewhere within the same function. A label is a user-defined name followed by a colon, marking the point in the function the goto will jump to.

The return statement exits a function altogether, returning control back to the calling function.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

In order to return from a function, C provides the returnkeyword, which can be used anywhere in a function for an immediate exit of that function.

For void functions, which do not return values, return by itself is sufficient to exit the function. I.e.:

void printhello(int numtimes)

{

int count;

if (numtimes<1) {

printf("Cannot print hello!\n");

return;

}

for (count=0; count

}

For functions returning a value, specify a value or variable of the return type. Example:

char *stringcat(char *str1, char *str2)

{

char *returnstr;

if ((str1==NULL)(str2==NULL) return NULL;

returnstr=(char*)malloc(strlen(str1)+strlen(str2)+1);

*returnstr=0;

strcat(returnstr, str1);

strcat(returnstr, str2);

return returnstr;

}

If you wish to stop the program immediately, use exit(n), where n is an integer value. This will return n as an exit code which is returned to the system calling the program. (-1 is a good generic value for errors, 0 for success.) I.e.:

void doprint(int numparam, char **paramlist)

{

int count;

if (argc<1) {

printf("Specify one or more parameters.\n");

exit(-1);

}

for (count=0; count

printf("#%d:%s\n", count, argv[count]);

}

int main(int argc, char **argv)

{

doprint(argc, char **argv);

return 0;

}

Always be sure you've closed files that are open and freed memory you no longer need prior to using return (and, for sake of habit, exit()).

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

The break statement is used to terminate the nearest enclosing do, for, switch or while statement. Control passes immediately to the statement that immediately follows that enclosing statement.

The exit() function terminates the program at the point of the call and returns the given value to the host environment. It effectively does the same thing as a return statement in the main function would:

#include<stdlib.h>

#include<stdio.h>

int main (void) {

FILE* pf;

pf = fopen ("file.txt","r");

if (pf == NULL) {

printf ("Error opening file\n");

exit (EXIT_FAILURE); /* same as calling return -1; */

} else {

/* use file */

fclose (pf);

}

return 0; /* same as calling exit (EXIT_SUCCESS); */

}

Note that calling exit() from any other function besides main should be used cautiously as this will effectively prevent any return to the main function (exit() does not and cannot return to its caller). However, any functions registered with atexit will be invoked by exit(), all open streams will be closed (and flushed if buffered) and all files created with tmpfile will be removed before control passes to the host environment. If any other clean up is required, you must manually invoke that clean up prior to calling exit().

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the use of break and exit statements in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the use of break in c programming?

The break statement is used to exit the nearest enclosing scope. Control passes to the first statement that comes after that enclosing scope.


List of command used in c language?

It's easy: there are no commands in C, but a few statements (such as: expression, if, else, switch, while, do-while, for, break, continue, return and goto), and countless library functions (like printf, malloc and fopen).


Four basic instruction in C language?

They are 'statements' to speak strictly, they are: , , if, else, while, for, do, return, switch, break, continue, gotoNote: is zero or more statements between '{' and '}'


How do you terminate statements in C?

All statements must be terminated with a semi-colon in C.


What is the repetitive control structure in c plus plus?

C provides two sytles of flow control:BranchingLoopingBranching is deciding what actions to take and looping is deciding how many times to take a certain action. Branching: Branching is so called because the program chooses to follow one branch or another.if statementThis is the most simple form of the branching statements. It takes an expression in parenthesis and an statement or block of statements. if the expression is true then the statement or block of statements gets executed otherwise these statements are skipped.? : OperatorThe ? : operator is just like an if ... else statement except that because it is an operator you can use it within expressions. ? : is a ternary operator in that it takes three values, this is the only ternary operator C has.switch statement:The switch statement is much like a nested if .. else statement. Its mostly a matter of preference which you use, switch statement can be slightly more efficient and easier to read. Using break keyword:If a condition is met in switch case then execution continues on into the next case clause also if it is not explicitly specified that the execution should exit the switch statement. This is achieved by using break keyword. Try out given example Show ExampleWhat is default condition:If none of the listed conditions is met then default condition executed. Looping Loops provide a way to repeat commands and control how many times they are repeated. C provides a number of looping way. while loopThe most basic loop in C is the while loop.A while statement is like a repeating if statement. Like an If statement, if the test condition is true: the statements get executed. The difference is that after the statements have been executed, the test condition is checked again. If it is still true the statements get executed again.This cycle repeats until the test condition evaluates to false. for loopfor loop is similar to while, it's just written differently. for statements are often used to proccess lists such a range of numbers: do...while loopdo ... while is just like a while loop except that the test condition is checked at the end of the loop rather than the start. This has the effect that the content of the loop are always executed at least once. break and continue statements C provides two commands to control how we loop: break -- exit form loop or switch.continue -- skip 1 iteration of loop.

Related questions

What is break in c?

The break statement is used to exit a loop or switch-case.


What is the use of break in c programming?

The break statement is used to exit the nearest enclosing scope. Control passes to the first statement that comes after that enclosing scope.


What are all the looping statements in c?

1. goto 2. while-break-continue 3. for-break-continue 4. do-while-break-continue 5. recoursion


Short note on conditional statements in c?

Use them carefully.


How many arithmetic statement in c?

Only one: expression. Yes, in C expression is one of the statements. Some other statements are: if, do, goto, while, for, switch, break, continue, return, NULL-statement, compound-statement.


Which directory to be included to use the function exit in C?

The one with the libraries in.


List of command used in c language?

It's easy: there are no commands in C, but a few statements (such as: expression, if, else, switch, while, do-while, for, break, continue, return and goto), and countless library functions (like printf, malloc and fopen).


Four basic instruction in C language?

They are 'statements' to speak strictly, they are: , , if, else, while, for, do, return, switch, break, continue, gotoNote: is zero or more statements between '{' and '}'


How to make decision making statements in c plus plus?

Decision making statements make use of conditional expressions. In C++ there are three possibilities: if/else, switch/case and the ternary operator (?:).


Write a c program to exit?

Not sure what you mean by this question - using the exit call will exit a C program:exit (0) ;


How do you terminate statements in C?

All statements must be terminated with a semi-colon in C.


What is the purpose of Continue and break statement in C?

The break statement is frequently used to terminate the processing of a particular case within a switch statement. Lack of an enclosing iterative or switch statement generates an error.Within nested statements, the break statement terminates only the do, for, switch, or whilestatement that immediately encloses it. You can use a returnor goto statement to transfer control elsewhere out of the nested structure.This example illustrates the break statement:#include int main() { char c; for(;;) { printf_s( "\nPress any key, Q to quit: " ); // Convert to character value scanf_s("%c", &c); if (c == 'Q') break; } } // Loop exits only when 'Q' is pressed