VT Logo MATLAB Logo

Home

Basics

Variables

Inputs and Outputs

Repetition Structures

Selection Structures

Selection Structure


Also known as a conditional structure, a selection structure is a programming feature that performs different processes based on whether a boolean condition is true or false. Selection structures use relational operators to test conditions. There are different types of selection structures that can be used to achieve different outcomes.

Contents

Selection Structure Types


To get a better idea of how selection structures work, the following three examples demonstrate how if-end, if-else, and elseif structures can be used to test if a variable is above or below a certain valueor values.

if-end

if-end selection structures are used when only one boolean condition is necessary. In if-end structures, a process will be only be performed if the boolean condition is true. For example, if we wanted to know the number of days from a data set where the daily high temperature was above 80 degrees, a programmer could use an if-end statement. The following screenshot shows this example in MATLAB.

if-end Example

When day is above 80, the boolean condition is true and the variable NumAbove80 increases by 1.

NOTE: This is a simplified example. This code will not operate by itself. For full instructions on creating an if-end structure in MATLAB go to the if-end page.

if-else

if-else selection structures are used when only one boolean condition is necessary. In if-else structures, a specific action will be performed if the boolean condition is true and another action, if the condition is false. For example, if we wanted to know the number of days from a data set where the daily high temperature was above and below 80 degrees, a programmer could use an if-else statement. The following screenshot shows this example in MATLAB.

If-else Example

When day is above 80, the boolean condition is true and the variable NumAbove80 increases by 1. However, when day is not above 80, the boolean condition is false and the variable NumBelow 80 increases by 1. (In this case, NumBelow80 is actually the number of days less than or equal to 80.)

NOTE: This is a simplified example. This code will not operate by itself. For full instructions on creating an if-else structure in MATLAB go to the if-else page.

elseif

elseif structures are a way to combine multiple boolean conditions into a single selection structure. Let's say that instead of just temperatures above and below 80 degrees, we want to know the number of days when temperatures were above 80, below 50, and in between. In this case, a programmer would use an elseif structure. The following screenshot show this example in MATLAB.

Elseif Example

When day is above 80, the if boolean condition is true and the variable NumAbove80 increases by 1. If day is not above 80, the if boolean condition is false and the selection structure moves to the elseif condition. Now, if day is below 50, the elseif boolean condition is true and the variable NumBelow50 increases by 1. If the elseif boolean condition is false, then the conditional structure moves to the else condition and the variable NumBtw50and80 increases by 1.

NOTE: This is a simplified example. This code will not operate by itself. For full instructions on creating an elseif structure in MATLAB go to the elseif page.