Branching Logic


Coding Techniques; Software Development; Computer Science


Branching logic is used in computer programming when different sections of code need to be executed depending on one or more conditions. Each possible code pathway creates another branch. There is no limit to the number of branches that can be used to implement complex logic.


Branching logic is a form of decision making in which a computer program follows different sets of instructions depending on whether or not certain conditions are met during the program's execution. Each set of instructions represents a different branch. Which branch of code is executed depends on the values assigned to the parameters of the branching procedure. The values of these parameters may be input by the user, or they may be generated by the output from a previous procedure.

A common form of branching logic is the if-then-else statement. Such statements generally take the following form:

Branching Logic

In this statement, Boolean logic is used to test a given condition (condition 1), which can be found to be either true or false. If condition 1 evaluates to true, then code is executed to create outcome A. If it evaluates to false, then code is executed to create outcome B.

The exact syntax of the if-then-else statement varies among programming languages, but the logic remains the same. In some programming languages, the “else” statement is optional. In these cases, if the conditional statement evaluates to false, the program will continue with the instructions that come after the “end if” statement.

Multiple branches can be used to test multiple conditions. An “else if” statement can be added to the above example to test other conditions:

Branching Logic

With the above structure, three conditions are tested and four outcomes are possible.

Most applications do not execute their code linearly, without variations. Rather, each time an application is run, different inputs can lead to different results. For example, in a word-processing application, pressing the same letter key on a computer keyboard will result in different outcomes depending on whether certain other keys are pressed at the same time. If a user presses the letter a, the application must evaluate whether or not that user also pressed either the shift key or the control key before it can determine if the letter should be displayed as a capital A or a lowercase a, or if some other code should be executed instead. The decision structure for this output might look something like the following:

Branching Logic

A model of branching logic will have multiple sequences of events. Which set of instructions is followed depends on the outcomes along specific nodes in the path. After script A is complete, if the output matches an expected outcome, then script B

A model of branching logic will have multiple sequences of events. Which set of instructions is followed depends on the outcomes along specific nodes in the path. After script A is complete, if the output matches an expected outcome, then script B will run; if not, then script C will run.

In this example, the Boolean statements being evaluated are “shift key is also pressed” and “control key is also pressed.” The keys pressed by the user are stored as parameters and then passed to the branching procedure, which uses those parameters to evaluate whether or not the statements are true. The user's actions can result in a new decision being created each time the same section of code is executed.

It is imperative that computer programs and applications be able to execute different pieces of code depending on the input they receive. For example, branching logic is often used in surveys so that participants receive only questions that apply to them. If a survey contains questions that only apply to one sex or another, the first survey question may ask for the sex of the participant. The participant's response will be stored as a parameter that will then determine which additional questions they will be presented with. If every participant had to read through all of the possible questions to determine whether or not each one applied to them, the survey would be much more difficult and time consuming, and it would result in more errors.

Branching logic is also used in workflows in fields such as marketing. If a customer views one page of a website, certain marketing materials are sent to that customer. If a different page is viewed, different materials are sent.

The nesting structure of branching logic can be confusing to plan if there are many conditions to check. Programmers can use a script tool to create a form structure for users to input the parameters that will be used to evaluate the conditional statements. This technique reduces errors, because restrictions can be placed on what data is entered. The script tool can also be used to provide the user with information calculated from input parameters.

There are some minor drawbacks to using branching logic. For example, the nesting structure of the “else if” statement creates added difficulty for some compilers, and conditional statements can slow the processing of code. Faster structures have been developed to overcome these difficulties.

Branching logic is used by computer programs in many fields. In online retail stores, for example, some items only require the user to make one decision—to buy or not to buy—while other items require the user to choose a color and a size. If the color is chosen first, the store will only list the sizes available in that color, and vice versa. This is much more convenient, and much less frustrating to users, than it would be if the store presented users with choices that did not exist and did not inform them until much later in the process. A listing for an item that is available in three colors in sizes small and medium, four colors in size large, and one color in size extra large may follow the following decision structure:

Branching Logic

Many computer programs require decision making, and branching logic is one way of making decisions. It is so common in computer programming that many languages feature alternate structures that can execute branching procedures in much fewer lines of code. For example, conditional operators can produce the same results as if-then-else statements using just one line of code. A switch statement is another, more complicated form of branching logic that uses a table to check many values against the given condition. Branching logic is used extensively in modern programming and has led to many developments as programs have become more complex.

—Maura Valentino, MSLIS

Friedman, Daniel P., and Mitchell Wand. Essentials of Programming Languages. 3rd ed., MIT P, 2008.

Haverbeke, Marijn. Eloquent JavaScript: A Modern Introduction to Programming. 2nd ed., No Starch Press, 2015.

MacLennan, Bruce J. Principles of Programming Languages: Design, Evaluation, and Implementation. 3rd ed., Oxford UP, 1999.

Scott, Michael L. Programming Language Pragmatics. 4th ed., Elsevier, 2016.

Schneider, David I. An Introduction to Programming Using Visual Basic. 10th ed., Pearson, 2017.

Van Roy, Peter, and Seif Haridi. Concepts, Techniques, and Models of Computer Programming. MIT P, 2004.