Branching Statements

Kenneth Leroy Busbee

Overview

A branch is an instruction in a computer program that can cause a computer to begin executing a different instruction sequence and thus deviate from its default behavior of executing instructions in order.[1] Common branching statements include break, continue, return, and goto.

Discussion

Branching statements allow the flow of execution to jump to a different part of the program. The common branching statements used within other control structures include: break, continue, return, and goto. The goto is rarely used in modular structured programming. Additionally, we will add to our list of branching items a pre-defined function commonly used in programming languages of: exit.

Examples

break

The break is used in one of two ways; with a switch to make it act like a case structure or as part of a looping process to break out of the loop. The following gives the appearance that the loop will execute 8 times, but the break statement causes it to stop during the fifth iteration.

counter = 0;
While counter < 8
    Output counter
    If counter == 4
        break
    counter += 1

continue

The following gives the appearance that the loop will print to the monitor 8 times, but the continue statement causes it not to print number 4.

For counter = 0, counter < 8, counter += 1
    If counter == 4
        continue
    Output counter

return

The return statement exits a function and returns to the statement where the function was called.

Function DoSometing
    statements
Return <optional return value>

goto

The goto structure is typically not accepted in good structured programming. However, some programming languages allow you to create a label with an identifier name followed by a colon. You use the command word goto followed by the label.

some lines of code; 
goto label;                // jumps to the label
some lines of code; 
some lines of code; 
some lines of code; 
label: some statement;     // Declared label
some lines of code;

exit

Although exit is technically a pre-defined function, it is covered here because of its common usage in programming. A good example is the opening a file and then testing to see if the file was actually opened. If not, we have an error that usually indicates that we want to prematurely stop the execution of the program. The exit function terminates the running of the program and in the process returns an integer value back to the operating system. It fits the definition of branching which is to jump to some other place in the program.

Key Terms

branching statements
Allow the flow of execution to jump to a different part of the program.
break
A branching statement that terminates the existing structure.
continue
A branching statement that causes a loop to stop its current iteration and begin the next one.
exit
A predefined function used to prematurely stop a program and return to the operating system.
goto
An unstructured branching statement that causes the logic to jump to a different place in the program.
return
A branching statement that causes a function to jump back to the function that called it.

References


License

Icon for the Creative Commons Attribution-ShareAlike 4.0 International License

Programming Fundamentals Copyright © 2018 by Kenneth Leroy Busbee is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License, except where otherwise noted.

Share This Book