What Is Branching?
There are a couple of different techniques that are commonly referred to as branching. strictly speaking, branching is using a test to decide between alternative sequences. Those sequences may be calling a different procedure or function, or setting variables to different values.
Notice the test condition for hours. If hours is less than or equal to 40, one sequence of
statements is implemented. However, if hours is greater than 40, another sequence of statements is executed. Using flowcharts can help you determine where branches occur in your program.
Let's look at a slightly different example. The most common type of branching uses a test
condition to determine a course of action (as per our definition), but then calls a different
procedure or function to perform that sequence of commands. Thus, you "branch" out of the current routine and into another. For example:
hour up to 40 hours. That statement written in C, might look like the following:
#define kNormalPay 10
#define kNormalWeek 40
float overtimePay;
if (hours > 40)
overtimePay = CalculateOvertime(hours, kNormalPay *
1.5);
else
overtimePay = 0.0;
In this example, if an employee worked overtime (more than 40 hours) the code
"branches" into a function to calculate the overtime pay. Often, if you need to perform
many commands as a result of a branch, you use a procedure or function. If, however,
you only need to perform a few commands (such as a variable assignment) you can place those commands in the branch instead of in a separate routine.