Skip to main content
Engineering LibreTexts

6.10: Branching Control Structures

  • Page ID
    29066
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Discussion

    The branching control structures allow the flow of execution to jump to a different part of the program. The common branching control structures that are used with other control structures are: break, and continue. These control structures should be used with great care. Usually there is a better way to write your code than to have to rely on one of these. The break statement is valid, and NEEDS to be used in the switch statement that we discussed in the last page. There is one other branching control structure that is often not viewed as branching control structure. It is: return; which is used with functions. Thus, there are two commonly used branching control reserved words used in C++; break and return. Additionally, we will add to our list of branching items a pre-defined function commonly used in the C++ programming language of: exit; that is part of the C standard library (cstdlib).

    Examples

    break

    The following is not a good use of break in a loop and it gives the appearance that the loop will execute 8 times, but the break statement causes it to stop during the fifth iteration.

    C++ source code
    counter = 0;
    while(counter < 8)
    {
      cout << counter << endl;
      if (counter == 4)
      {
          break;
       }
      counter++;
    } 
    
    C++ source code
    counter = 0;
    while((counter < 8) && (count != 4))
    {
      cout << counter << endl;
      counter++;
    } 
    

    In this manner, we can tell in the loop statement exactly what is happening, and do not have to search through the rest of the code in the loop to figure out why we are not looping 8 times.

    continue

    The continue statement says, "skip the rest of the loop and go back to the top of the loop for next iteration". 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. There are times when this it totally acceptable, depending on the coding standards of the project you are working on.

    C++ source code
    for(counter = 0; counter < 8; counter++)
      {
      if (counter == 4)
        {
        continue;
        }
      cout << counter << endl;
      } 
    

    return

    The return statement simply says, "go back to where you came from". in the following example (we haven't talked about functions yet) we reach the end of the function get_data() and it is good practice to place a return statement here...and we simply go back to the place in our code where this function was called.

    C++ source code
    //******************************************************
    // get data
    //******************************************************
    
    void get_data(void)
    {
      // Input - Test Data - 5678.9, 5432.1
      cout << "\nEnter the length of the property in feet --->: ";
      cin >> property_length;
      cout << "\nEnter the width of the property in feet ---->: ";
      cin >> property_width;
      return;
    } 
    

    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 pre-maturely stop the execution of the program. Within the C++ programming language 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. In our example the value returned to the operating system is the value of the constant named: EXIT_FAILURE.

    C++ source code
    inData.open(filename);  //Open input file
    if (!inData)            //Test to see if file was opened
    {
      cout << "\n\nError opening file: " << filename << "\n\n";
      pause();              //Pause - user reads message
      exit(EXIT_FAILURE);   //Allows a pre-mature jump to OS
     } 
    

    Definitions

    Branching Control Structures
    Allow the flow of execution to jump to a different part of the program.
    Break
    A branching control structure that terminates the existing structure.
    Continue
    A branching control structure that causes a loop to stop its current iteration and begin the next one.
    Goto (SHOULD NEVER BE USED)
    A branching control structure that causes the logic to jump to a different place in the program.
    Return
    A branching control structure that causes a function to jump back to the function that called it.
    Exit
    A pre-defined function used to prematurely stop a program and jump to the operating system.

    Adapted from:
    "Branching Control Structures" by Kenneth Busbee, Download for free at http://cnx.org/contents/303800f3-07f...93e8948c5@22.2 is licensed under CC BY 4.0 


    This page titled 6.10: Branching Control Structures is shared under a CC BY-SA license and was authored, remixed, and/or curated by Patrick McClanahan.