<< Chapter < Page Chapter >> Page >
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

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
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.

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.

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."; }

Got questions? Get instant answers now!

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.

C++ source code

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

Got questions? Get instant answers now!

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.

C++ source code

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

Got questions? Get instant answers now!

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.

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;

Got questions? Get instant answers now!

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.

C++ source code

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

Got questions? Get instant answers now!

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 messageexit(EXIT_FAILURE); //Allows a pre-mature jump to OS }

Got questions? Get instant answers now!

Questions & Answers

A golfer on a fairway is 70 m away from the green, which sits below the level of the fairway by 20 m. If the golfer hits the ball at an angle of 40° with an initial speed of 20 m/s, how close to the green does she come?
Aislinn Reply
cm
tijani
what is titration
John Reply
what is physics
Siyaka Reply
A mouse of mass 200 g falls 100 m down a vertical mine shaft and lands at the bottom with a speed of 8.0 m/s. During its fall, how much work is done on the mouse by air resistance
Jude Reply
Can you compute that for me. Ty
Jude
what is the dimension formula of energy?
David Reply
what is viscosity?
David
what is inorganic
emma Reply
what is chemistry
Youesf Reply
what is inorganic
emma
Chemistry is a branch of science that deals with the study of matter,it composition,it structure and the changes it undergoes
Adjei
please, I'm a physics student and I need help in physics
Adjanou
chemistry could also be understood like the sexual attraction/repulsion of the male and female elements. the reaction varies depending on the energy differences of each given gender. + masculine -female.
Pedro
A ball is thrown straight up.it passes a 2.0m high window 7.50 m off the ground on it path up and takes 1.30 s to go past the window.what was the ball initial velocity
Krampah Reply
2. A sled plus passenger with total mass 50 kg is pulled 20 m across the snow (0.20) at constant velocity by a force directed 25° above the horizontal. Calculate (a) the work of the applied force, (b) the work of friction, and (c) the total work.
Sahid Reply
you have been hired as an espert witness in a court case involving an automobile accident. the accident involved car A of mass 1500kg which crashed into stationary car B of mass 1100kg. the driver of car A applied his brakes 15 m before he skidded and crashed into car B. after the collision, car A s
Samuel Reply
can someone explain to me, an ignorant high school student, why the trend of the graph doesn't follow the fact that the higher frequency a sound wave is, the more power it is, hence, making me think the phons output would follow this general trend?
Joseph Reply
Nevermind i just realied that the graph is the phons output for a person with normal hearing and not just the phons output of the sound waves power, I should read the entire thing next time
Joseph
Follow up question, does anyone know where I can find a graph that accuretly depicts the actual relative "power" output of sound over its frequency instead of just humans hearing
Joseph
"Generation of electrical energy from sound energy | IEEE Conference Publication | IEEE Xplore" ***ieeexplore.ieee.org/document/7150687?reload=true
Ryan
what's motion
Maurice Reply
what are the types of wave
Maurice
answer
Magreth
progressive wave
Magreth
hello friend how are you
Muhammad Reply
fine, how about you?
Mohammed
hi
Mujahid
A string is 3.00 m long with a mass of 5.00 g. The string is held taut with a tension of 500.00 N applied to the string. A pulse is sent down the string. How long does it take the pulse to travel the 3.00 m of the string?
yasuo Reply
Who can show me the full solution in this problem?
Reofrir Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Programming fundamentals - a modular structured approach using c++. OpenStax CNX. Jan 10, 2013 Download for free at http://cnx.org/content/col10621/1.22
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Programming fundamentals - a modular structured approach using c++' conversation and receive update notifications?

Ask