Program Transfer Control Statements- break, continue, goto, exit
break Statement
The break statement is used:
-
In a switch statement to transfer program control out of the switch after executing a particular case.
-
In loops to immediately terminate the loop when a specific condition is met.
This helps save computation time by stopping unnecessary iterations.
Example: Check whether a number contains digit 0
This program stops checking as soon as the first 0 is found in the number.
Output
continue Statement
The continue statement is used inside loops to:
-
Skip the remaining statements in the current iteration
-
Move immediately to the next iteration of the loop
Example: Print numbers from 1 to 15 except those divisible by 3
When a number is divisible by 3, the loop skips printing it.
Output
Comparison Between break and continue Statement in C
| Feature | break Statement | continue Statement |
|---|---|---|
| Purpose | Used to terminate the loop or switch immediately. | Used to skip the remaining statements in the current iteration and continue with the next iteration. |
| Where it is used | Used in loops (for, while, do-while) and switch statements. | Used in loops only (for, while, do-while). |
| Effect on loop | Stops the entire loop execution. | Does not stop the loop; only skips the current iteration. |
| Control flow | Control moves outside the loop or switch. | Control moves to the beginning of the loop for the next iteration. |
| Use case | When a condition is met and no further looping is needed. | When a condition is met and you want to skip just that iteration. |
| Execution after statement | Statements after break inside the loop are not executed, and the loop ends. | Statements after continue inside the loop are not executed, but the loop continues. |
Simple Example
// break example
goto Statement in C
Definition
A goto statement in C programming provides an unconditional jump from the goto statement to a labeled statement within the same function.
Note
-
The use of the goto statement is highly discouraged in any programming language.
-
It makes the control flow of a program difficult to trace, causing the program to become:
-
Hard to understand
-
Hard to modify
-
-
Code can quickly become confusing when it jumps arbitrarily from one place to another.
-
Any program that uses goto can usually be rewritten without it.
Syntax of goto Statement
Explanation
-
A label is an identifier followed by a colon (
:). -
When the program encounters a
goto label;, control immediately jumps to the labeled statement. -
The label can be placed:
-
Above the
gotostatement, or -
Below the
gotostatement.
-
Example: Using goto to Sum Numbers
The following program finds the sum of numbers entered by the user until a negative number is entered.
-
When a negative number is entered, program control jumps to the label
end. -
Otherwise, the number is added to
sum, and control jumps back tobeginto continue input.
exit() Function in C
The exit() function is used to terminate a program immediately, especially when an error prevents normal execution.
Prototype
Explanation
-
The
statusvalue is returned to the Operating System. -
It indicates whether the program ended:
-
Successfully →
EXIT_SUCCESS(value0) -
With failure/error →
EXIT_FAILURE(value1)
-
-
The
exit()function is declared in thestdlib.hheader file.
Example: Using exit() to Stop Program for Negative Input
The following program finds the square root of a number.
If the number entered is negative, the program prints a message and terminates using exit(1).



Comments
Post a Comment