All these keywords are used in controlling the program flow. Once they encountered in a program, they work differently as, when a break is observed it skips the further execution of the loop or a switch…case block, whereas when continue is observed it skips the part of the loop and continues the next iteration of the loop with next successive loop control variable-value.
Understanding these keywords and the exit() function is important for controlling the flow of a program, especially within loops and functions. They allow for more precise control over when and how a program should terminate or continue executing.
The break keyword is used to exit a loop prematurely. When the break keyword is encountered within a loop (such as a for, while, or do-while loop), the loop is immediately terminated, and program control is passed to the next statement after the loop.
The continue keyword is used to skip the remaining code within a loop and move to the next iteration of the loop. When the continue keyword is encountered within a loop, the current iteration is terminated, and program control goes back to the beginning of the loop to start the next iteration.
Break | Continue |
---|---|
Used to terminate a loop | Used to skip an iteration |
Execution of the loop is terminated immediately | Execution of the current iteration is terminated |
Control is transferred to the statement immediately after the loop | Control is transferred to the beginning of the loop for the next iteration |
Can be used in any loop, switch statement, or nested loop | Can only be used in loops |
Used to exit the loop when a certain condition is met | Used to skip certain iterations when a certain condition is met |
//Example of break :
for (i=0; i<10; i++)
{
if (i == 5)
{ break; }
printf("%d ", i);
}
Output: 0 1 2 3 4
//Example of continue :
for (i=0; i<10; i++)
{
if (i == 5)
{ continue; }
printf("%d ", i);
}
Output: 0 1 2 3 4 6 7 8 9
The return keyword is used to exit a function and return a value to the calling code. When the return keyword is encountered within a function, the function is immediately terminated, and the specified value is returned to the calling code.
KEY POINTS
1.include the stdlib.h header file while using the exit () function.
2.It is used to terminate the normal execution of the program while encountered the exit () function.
3.We can use the exit() function to flush or clean all open stream data like read or write with unwritten buffered data.
4.It closses all opened files linked with a parent or another function or file and can remove all files created by the tmpfile function.
5.The program’s behaviour is undefined if the user calls the exit function more than one time.
6.The exit function is categorized into two parts: exit(0) and exit(1).
7.In exit() function the pre defined values for 0 and 1 are EXIT_SUCCESS and EXIT_FAILURE.
All about java servlets Part-2 What is a website? Website is a collection of related…
Let's train the machine. Machine learning is a growing technology which enables computers to learn…
“Unless you try to do something beyond what you have already mastered, you will never…
Whenever you decide to start coding and you want to become budding coder or programmer…
C components are essential elements of a C program, including comments, preprocessor directives, functions, statements,…
All about java servlets Part-2 What is a website? Website is a collection of related…