Coupon Accepted Successfully!


Unconditional Control Transfer Statements

C/C++ has four statements that perform an unconditional control transfer. They are return, goto, break and continue. Of these, return is used only in functions. The goto and return may be used anywhere in the program but continue and break statements may be used only in conjunction with a loop statement. The break is used most frequently in switch case.

The goto statement

This statement does not require any condition. This statement passes control anywhere in the program without considering any condition. The general format for this statement is shown in Figure.

Here, a label is any valid label either before or after goto. The ‘label’ must start with any character and can be constructed with rules used for forming identifiers. Avoid using the goto statement.

The goto statement


5.12 Write a program to demonstrate the use of goto statement.


Explanation: In the above program a number is entered. The number is checked to see if it is even or odd with modules division operator. When the number is even, the goto statement transfers the control to the label even. Similarly, when the number is odd, the goto statement transfers the control to the label odd and the respective message will be displayed.

The break Statement

The break statement allows the programmer to terminate the loop. The break skips from the loop or the block in which it is defined. The control then automatically passes on to the first statement after the loop or the block. The break can be associated with all the conditional statements (especially switch case). We can also use break statements in the nested loops. If we use break statement in the innermost loop, then the control of the program is terminated from that loop only and resumes at the next statement following that loop. The widest use of this statement is in the switch case where it is used to avoid flow of control from one ‘case’ to the other. Programming examples on break are given in switch topic.

The continue Statement

The continue statement works somewhat like the break statement. Instead of forcing the control to the end of the loop (as it is in case of break), the continue case causes the control to pass on to the beginning of the block/loop. In the case of for loop, the continue case initiates the testing condition and increment on steps has to be executed (while rest of the statement following the continue are neglected). For while and do-while, the continue case causes control to pass on to conditional tests. It is useful in a programming situation where it is required that particular iterations occur only up to some extent or when some part of the code has to be neglected. The programs on continue are explained in the control loop chapter.

Test Your Skills Now!
Take a Quiz now
Reviewer Name