Jump to content

Programming Fundamentals/Branching Statements

From Wikibooks, open books for an open world

Overview

[edit | edit source]

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

[edit | edit source]

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

[edit | edit source]

break

[edit | edit source]

Break terminates the existing structure. 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

[edit | edit source]

Continue causes the loop to stop its current iteration and begin the next one. 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

[edit | edit source]

Return causes the function to jump back to the function that called it. The return statement exits a function and returns to the statement where the function was called.

Function DoSometing
    statements
Return <optional return value>

Goto causes logic to jump to a different place. 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 is a pre-defined function which prematurely stops the execution of the program and causes it to jump to another place in the program. 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.

Key Terms

[edit | edit source]
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

[edit | edit source]