Skip to content Skip to navigation

OpenStax_CNX

You are here: Home » Content » Branching Control Structures

Navigation

Lenses

What is a lens?

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

This content is ...

Endorsed by Endorsed (What does "Endorsed by" mean?)

This content has been endorsed by the organizations listed. Click each link for a list of all content endorsed by the organization.
  • CCQ display tagshide tags

    This module is included in aLens by: Community College of QatarAs a part of collection: "Programming Fundamentals - A Modular Structured Approach using C++"

    Comments:

    "Used in the Computer Programming Fundamentals I course."

    Click the "CCQ" link to see all content they endorse.

    Click the tag icon tag icon to display tags associated with this content.

Affiliated with (What does "Affiliated with" mean?)

This content is either by members of the organizations listed or about topics related to the organizations listed. Click each link to see a list of all content affiliated with the organization.
  • OrangeGrove display tagshide tags

    This module is included inLens: Florida Orange Grove Textbooks
    By: Florida Orange GroveAs a part of collection: "Programming Fundamentals - A Modular Structured Approach using C++"

    Click the "OrangeGrove" link to see all content affiliated with them.

    Click the tag icon tag icon to display tags associated with this content.

  • Houston Community College display tagshide tags

    This module is included in aLens by: Houston Community CollegeAs a part of collection: "Programming Fundamentals - A Modular Structured Approach using C++"

    Comments:

    "COSC1436 Programming Funaamentals I"

    Click the "Houston Community College" link to see all content affiliated with them.

    Click the tag icon tag icon to display tags associated with this content.

  • Featured Content display tagshide tags

    This module is included inLens: Connexions Featured Content
    By: ConnexionsAs a part of collection: "Programming Fundamentals - A Modular Structured Approach using C++"

    Comments:

    "Programming Fundamentals - A Modular Structured Approach Using C++ is a new course written by Kenneth Leroy Busbee, a faculty member at Houston Community College. This text introduces students to […]"

    Click the "Featured Content" link to see all content affiliated with them.

    Click the tag icon tag icon to display tags associated with this content.

Also in these lenses

  • Busbee's Compter Science display tagshide tags

    This module is included inLens: Busbee's Computer Science Lens
    By: Kenneth Leroy BusbeeAs a part of collection: "Programming Fundamentals - A Modular Structured Approach using C++"

    Comments:

    "Texas Common Course Numbering: COSC1336 or COSC1436"

    Click the "Busbee's Compter Science" link to see all content selected in this lens.

    Click the tag icon tag icon to display tags associated with this content.

  • Lens for Engineering

    This module is included inLens: Lens for Engineering
    By: Sidney Burrus

    Click the "Lens for Engineering" link to see all content selected in this lens.

  • eScience, eResearch and Computational Problem Solving

    This module is included inLens: eScience, eResearch and Computational Problem Solving
    By: Jan E. OdegardAs a part of collection: "Programming Fundamentals - A Modular Structured Approach using C++"

    Click the "eScience, eResearch and Computational Problem Solving" link to see all content selected in this lens.

Recently Viewed

This feature requires Javascript to be enabled.

Tags

(What is a tag?)

These tags come from the endorsement, affiliation, and other lenses that include this content.
 

Branching Control Structures

Module by: Kenneth Leroy Busbee. E-mail the author

Summary: An introduction to the common types of branching control structures and which ones are allowed in good structured programming.

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, continue and goto. These are rarely used in modular structured programming with one exception. That exception is in relation to creating the case within the selection category of control structures. Within C++ the break is used with the switch to create a structure that acts like the traditional case structure. 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). Some definitions:

Definitions

Definition 1: branching control structures
Allow the flow of execution to jump to a different part of the program.
Definition 2: break
A branching control structure that terminates the existing structure.
Definition 3: continue
A branching control structure that causes a loop to stop its current iteration and begin the next one.
Definition 4: goto
A branching control structure that causes the logic to jump to a different place in the program.
Definition 5: return
A branching control structure that causes a function to jump back to the function that called it.
Definition 6: exit
A pre-defined function used to prematurely stop a program and jump to the operating system.

We will discuss each item indicating which ones are allowed or not allowed within good structured programming practices.

Examples

break

The break is used in one of two ways; with the switch (a C++ programming structure) to make it act like a case structure (it's more common name within most programming languages) or as part of a looping process to break out of the loop. The first usage is allowed in good structured programming and the second is not allowed in good structured programming.

Example 1: C++ source code


switch (age)
  {
  case 18: cout << "\nYou can vote.";
           break;
  case 39: cout << "\nYou are middle aged.";
           break;
  case 65: cout << "\nYou are at retirement age.";
           break;
  default: cout << "\nYour current age is not important.";
  } 

The following is an unauthorized 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.

Example 2: C++ source code


counter = 0;
while(counter < 8)
  {
  cout << counter << endl;
  if (counter == 4)
    {
    break;
    }
  counter++;
  } 

continue

The continue structure is not allowed in good structured programming. 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.

Example 3: C++ source code


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

goto

The goto structure is not allowed in good structured programming. It is with a certain amount of hesitancy that we even show it. Many textbooks do not cover the goto. Within the C++ programming language you create a label with an identifier name followed by a colon. You use the command word goto followed by the label. A label can be used before it is declared.

Example 4: C++ source code


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

return

The return is allowed in good structured programming, but only at the end of a function. A function should not pre-maturely end by having the logic of the function have it terminate by jumping back to the function that called it.

Example 5: 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.

Example 6: 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
  } 

Content actions

Download module as:

Add module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks