17. Keyword break, continue, return and exit [Fundamental Control Structure Statements in C. #Part-4]

Table of Contents
< All Topics
Print

17. Keyword break, continue, return and exit [Fundamental Control Structure Statements in C. #Part-4]

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 statement:

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.

Syntax:

break;

Image 19
Image 24
KEY POINTS
  • A break statement can appear only inside, or as a body of, a switch statement or a loop.
  • The break inside a loop/switch will terminates the loop and the program control is transferred to the next statement after the loop/ /switch.
  • The break inside a nested loop, only terminates the execution of the nearest enclosing loop.
  • The execution resumes with the statement present next to the terminated loop.
  • There is no constraint about the number of break statements that can be present inside a loop.

The continue Statement:

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.

Syntax:

continue;

Image 26
Image 25
KEY POINTS
  • A continue statement can appear only inside, or as the body of, a loop.
  • A continue statement terminates the current iteration of the loop.
  • When continue statement present inside a nested loop is executed, it only terminates the current iteration of the nearest enclosing loop.
  • On the execution of continue statement, the program control is immediately transferred to the header of the loop.
  • There is no constraint about the number of continue statements that can be present inside a loop.

Difference between break and continue

BreakContinue
Used to terminate a loopUsed to skip an iteration
Execution of the loop is terminated immediatelyExecution of the current iteration is terminated
Control is transferred to the statement immediately after the loopControl is transferred to the beginning of the loop for the next iteration
Can be used in any loop, switch statement, or nested loopCan only be used in loops
Used to exit the loop when a certain condition is metUsed 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 Statement:

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.

Syntax:

return; <–Form 1

return expression; <–Form 2

return (expression); <–Form 3

KEY POINTS

  • A return statement without an expression (i.e. Form 1) can appear only in a function whose return type is void.
  • A return statement with an expression (i.e. Form 2 or Form 3) should not appear in a function whose return type is void.
  • A return statement terminates the execution of a function and returns the control to the calling function.

The exit() Function

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.

Conclusion

  1. The break keyword: The break keyword is utilized to prematurely exit a loop. Once the break keyword is encountered within a loop (such as a for, while, or do-while loop), the loop will be immediately terminated and program control will be passed to the next statement following the loop.
  2. The continue keyword: The continue keyword is utilized to bypass the remaining code within a loop and proceed to the next iteration of the loop. When the continue keyword is found within a loop, the current iteration is halted, and program control returns to the beginning of the loop to initiate the next iteration.
  3. The return keyword: The return statement is used to exit a function and send a value back to the calling code. When the return statement is reached within a function, the function is instantly ended, and the specified value is sent back to the calling code.
  4. The exit() function: The exit() function is used to terminate the entire program. It is defined in the stdlib.h header file. When the exit() function is called, the program is terminated, and any open files or streams are closed. The function takes an integer argument that represents the exit status of the program, with a value of 0 typically indicating successful termination and non-zero values indicating an error or abnormal termination.