2.6. Control Constructs

Reading Assignment

Please read sections 4.1 through 4.4 of Physical Modeling in MATLAB, by Allen B. Downey [DOWNEY11].

Here we continue to present MATLAB control constructs. Whereas, the for loop is considered a counting loop because the number of loop executions is predetermined; the constructs here are conditional. The evaluation of a logical (Boolean) expression determines which code block to execute and, in the case of while loops, the number of executions. A logical expression is a statement that evaluates to either true (1) or false (0), which is often displayed in MATLAB (1) for true and (0) for false. To write logical expressions, we need relational and logical operators.

2.6.1. Relational Operators

Relational operators compare the relationship between two items and return either a true or false verdict. For example, using variables x and y we might write a logical expression to see if x is greater than y as x > y. We usually think of relational operators as comparing numeric values, but this is not always the case. For example, the alphabetical order of text strings could be compared as in "Bob" > "Bill". The following table lists the relational operators.

Operator Meaning
== Equal to each other (note: x == y NOT x = y)
~= Not equal
> Greater than
< Less than
<= Less than or equal
>= Greater than or equal

2.6.2. Logical Operators

Logical operators allow us to combine other logical expressions to form a composite expression. Here we are using the symbols a and b to represent individual logical expressions. Symbol a, for example, might represent x > y and b might represent x > z, where x, y, and z are variables.

Operator Meaning Example
&& AND a && b
|| OR a || b
~ NOT ~a

Parenthesis, ( ), may also be used to control the order in which logical expressions are evaluated.

Note that as soon as MATLAB determines if a statement is true or false, it stops evaluating. For example, if the logical expression is a && b and a is found to be false, then b is not evaluated because the overall statement will be false. Similarly, if the expression is a || b, b is not evaluated if a is true. This behavior is commonly called short circuit evaluation.

Note: In addition to the && and || operators for AND and OR, MATLAB also has (& and |) operators. These are used with Logical Vectors, not control constructs.

2.6.3. Selection Statements

Selection statements determine which, if any, code will run.

2.6.3.1. If Construct

If the logical condition of an if statement is true, the code block runs. If the condition is false, the code block is skipped.

if condition
    code block
end

2.6.4. If - Else

Add and an else statement to the if construct when there is an alternate code block that should run when the condition is false.

if condition
    code block 1
else
    code block 2
end

2.6.4.1. Eleseif

Selection between multiple code blocks is achieve with any number of elseif statements.

The final else statement is optional. Its code block runs when all of the other logical condition statements are false.

if condition1
    code block 1
elseif condition2
    code block 2
elseif condition3
    code block 3
else
    code block 4
end

Note

Only one code block will run, even if multiple conditions are true. What will be the output of the following code?

a = 1;
if a < 2
    disp(2)
elseif a < 3
    disp(3)
elseif a < 4
    disp(4)
else
    disp(1)
end

2.6.4.2. Switch - Case Construct

Switch is a multi branching selection statement. The switching value can be a variable, expression, or function that evaluates to either a scalar (usually integer values) or a character vector. The code below the matching case statement is ran. To match any of multiple values a cell array is used. The code following the otherwise statement is run when none of the case statements match the switching value.

% switchExample.m, Example of a switch statement
for x = 0:5
    switch x
        case 1
            disp('x = 1')
        case 2
            disp('x = 2')
        case {3, 4}
            disp('x = 3 or 4')
        otherwise
            disp('x is not between 1 and 4')
    end
end

The output of this script is as follows.

>> switchExample
x is not between 1 and 4
x = 1
x = 2
x = 3 or 4
x = 3 or 4
x is not between 1 and 4

2.6.5. Example Selection Statements

The following example illustrates nested if constructs. It also demonstrates the input and error functions that are used for interacting with the user of the program.

loan = input('Enter the loan amount: ');

if loan >= 1e6
    error('Loans must be less than one million dollars')
else
    if loan < 100
        rate = 0.08;
    elseif loan < 1000
        rate = 0.06;
    elseif loan < 10000
        rate = 0.05;
    elseif loan < 100000
        rate = 0.04;
    else
        rate = 0.03;
    end
    disp(['The interest rate is ', num2str(rate*100), '%.'])
end

2.6.6. While Loop

The while loop can execute a code block multiple times. But the continued running of the loop depends on a logical condition. While loops should be used instead of a for whenever the number of loop executions can not be pre-determined. This might be because an algorithm needs to run a different number of times depending on the value of a variable, or because of interactions from users.

The loop will evaluate the condition and run if it is true. Each time after running the code block, the condition is re-evaluated for a possible additional run. The loop stops whenever the condition is false. while loops run zero or more times.

while condition
    code block
end

2.6.7. Example Control Constructs - sinc

MATLAB has an built-in constant called eps, which is the smallest, positive, non-zero value that can be noticed with addition. A fun illustration of a while loop is to find this value manually.

Then we will test an application of eps for preventing divide by zero errors. If done correctly, our sinc function should plot a smooth curve at sinc(0) = 1.

%% Find myeps, our manual find of eps

epsilon = 1;
while (1 + epsilon) ~= 1
    myeps = epsilon;
    epsilon = epsilon / 2;
end
fprintf('1 + %9.5g is the same as 1\n', epsilon)
fprintf('myeps = %9.5g\n', myeps);

%% Test myeps with a sinc function sinc(x) = sin(x)/x

t = -10:0.1:10;
y = t;              % just create y array for efficiency sake
% This could be vectorized, but this code illustrates a for
% loop and a selection statement.
for k = 1:length(t)
    if t(k) == 0
        x = myeps;  % prevent a divide by zero error
    else
        x = t(k);
    end
    y(k) = sin(x)/x;
end
plot(t, y)
title('Sinc Function')

Round-off Errors

The topic of eps, also called machine epsilon, brings up the important topic of round-off errors and numerical accuracy. We saw here that if a number smaller than eps is added to one, the result is still one. We can think of the difference between 1 and 1 + eps as the round-off error of the digital numbers between 1 and 2. Because floating point numbers are stored in the computer using a binary scientific notation, the round-off error doubles for each increment of the exponent.

(-1)^{sign} \times 1.N \times 2^{exponent}

Numbers smaller than 2 \times eps are lost in addition when added to numbers between 2 and 4. Similarly any number less than 4 \times eps are lost in addition when added to numbers between 4 and 8. So round-off errors occur when we add or subtract large numbers and small numbers together. Algorithms that are numerically stable avoid addition and subtraction between large and small numbers. We will pick up this topic again in Numerical Stability.

2.6.8. Continue and Break

MATLAB, as well as most other programming languages, have two addition commands related to looping control constructs. These commands provide mechanisms for altering the execution flow in the middle of a loop. Both of these commands are normally placed inside an if construct that is there to catch special conditions.

2.6.8.1. Continue

The continue keyword causes execution of the current loop iteration to skip past the remainder of the loop’s code block. Control returns to the beginning of the loop where the loop condition is evaluated again to either advance to the next loop iteration or exit the loop.

In the following pseudocode example, if the special_condition is true, code block 2 is skipped and control moves back to evaluating loop_condition.

while loop_condition

    code block 1

    if special_condition
        continue
    end

    code block 2

end

2.6.8.2. Break

The break keyword cause execution of the current loop to stop. Control advances to the next code after the loop.

In the following pseudocode example, if the special_condition is true, code block 2 is skipped and the loop is finished.

while loop_condition

    code block 1

    if special_condition
        break
    end

    code block 2

end

2.6.8.3. Continue and Break Example

Here is a short program using continue and break statements. The program prompts the user to enter numbers one at a time. The user chooses how many numbers to enter. Since we don’t know how many numbers will be entered, the loop is programmed with a while true statement to run until the user enters a value of zero. When zero is entered, the break statement stops the loop. If the user makes a mistake and enters a negative number, we don’t want to include that number in the average, so a continue statement skips the rest of the loop’s code and the user is prompted again.

% Prompt the user for the length of items and calculate the average

disp('Enter item lengths in inches, enter zero when finished')
sum = 0;
n = 0;
while true
    item_len = input("Enter the length of the next item: ");
    if item_len == 0
        break;
    elseif item_len < 0
        disp('Items must have a length greater than 0.')
        continue;
    end
    n = n + 1;
    sum = sum + item_len;
end
average = sum/n;
disp(['The average length is ', num2str(average)])

Note

Now take a look at the Save Your Money homework, which we will discuss in class.