Lecture 4

Chapter 4 - Selection Statements

Synopsis
In this class, we will discuss the contents of Chapter 4: Selection Statements. The topics that will be covered include if statements, if-else statements, nested if-else statements, and if-elseif statements.


Daily Quiz

Quiz 5


if Statements

Until now, our MATLAB scripts and programs have been sequences of expressions performed one after the other. This does not allow us much flexibility when it comes to the functionality of our programs. What if we want to perform different calculations depending on the input? Have no fear, selection statements are here. Selection statements, or conditional statements, permit the execution of if-then logic. If the user enters a negative number, then perform calculation x. We will begin by looking at the form of a typical if statement.

1
2
3
4
if condition
    action

end

if statements are enclosed by the if and end keywords. When the MATLAB interpreter gets to the beginning of the if statement, it checks the condition. In general, the condition is a boolean or expression that results in a boolean. We've seen booleans before in the section on logic. Booleans are the result of casting, relational expressions, logical expressions, or a combination of each. If MATLAB determines that the condition is not false, it will perform the defined action. The action can be any number of statements. If MATLAB determines that the condition is false, the action is skipped, and the interpreter moves on to any code following the if statement.

Suppose we'd like to convert a user-defined character from uppercase to lowercase or from lowercase to uppercase--depending on what the user enters as input. Let's assume the value entered by the used is stored in the variable mychar. We can use if statements to perform the proper conversion depending on the input. To do so, we need to use character encoding to check if mychar is lowercase (i.e. between 97 and 122) or uppercase (i.e. between 65 and 90). For this, we can use two if statements.

1
2
3
4
5
6
7
8
9
10
11
%check if lowercase
if (mychar >= 97) && (mychar <=122)
    newchar = char(mychar-32);
end

%check if uppercase
if (mychar >= 65) && (mychar <=90)
    newchar = char(muchar+32);
end

disp(newchar)

That looks like it should work well enough, right? The majority of the time, the function should work fine. But what happens if a user enters the character !? Well, it will not satisfy either of the the if statements, and the variable newchar will never be created. This means that we will get an error when we try to print the value of newchar in line 11. A good rule of thumb is never to create a variable inside a conditional statement.

So how do we solve our problem? Let's take a look at a different type of conditional statement.

if-else Statements

if-else statements are very similar to _if_statements, but they add an extra clause to be executed in cases where the condition results to false. They take the following form.

1
2
3
4
5
if condition
    action1
else
    action2
end

By adding the else clause, we are provide a piece of code (action2) to be executed if action1 is skipped. In if-else statements, only one action (action1 or action2) will be performed. If the condition is true, then action1 is executed. If the condition is false, then action2 is executed. Maybe this is the type of conditional statement we need to convert the case of a user-defined letter.

1
2
3
4
5
6
7
8
%check if lowercase, and if not assume uppercase
if (mychar >= 97) && (mychar <=122)
    newchar = char(mychar-32);
else
    newchar = char(muchar+32);
end

disp(newchar)

Great, with this code we ensure that the variable newchar is create. It will either be created if the letter is lowercase, or if the letter is not lowercase. Unfortunately, we will still have the issue of dealing with non-letter characters. The else clause assumed that all non-lowercase letters are uppercase, and this is not a valid assumption. However, we can check if the letter is uppercase within the else clause. This is called a nested if-else statement.

Nested if-else Statements

There is nothing that tells us that the actions of an if-else statement can't be another if-else statement. By nesting one if-else statement within another if-else statement, we can check multiple conditions. There is no limit to the number of nested statements we can have, but readability of our code typically prevents us from using more than three nested statements.

As we mentioned above, we can check for uppercase letters within the first else clause.

1
2
3
4
5
6
7
8
9
10
11
12
13
%check if lowercase
if (mychar >= 97) && (mychar <=122)
    newchar = char(mychar-32);
else
    %check if uppercase
    if (mychar >= 65) && (mychar <=90)
        newchar = char(muchar+32);
    else
        newchar = 'not a letter'
    end
end

disp(newchar)

With the above code, we are first checking if the character is lowercase. If not, then we are checking if the character is uppercase. If both of these conditions are false, then we assign newchar the string 'not a letter' to inform the user that they did not follow the rules. This code perfectly accomplishes our goals, but it can be a bit difficult to read. Fortunately, there are alternatives to nested if-else statements.

if-elseif Statements

When checking multiple conditions using nested statements, we can easily end up with many levels of confusing indented code. The elseif clause is one way to avoid the mess of nested statements. Using elseif combines the else and if clauses into one statement that allows us to check a new condition in the event that the previous is false.

1
2
3
4
5
if condition1
    action1
elseif condition2
    action2
end

The above format allows us to check multiple conditions, but it does not perform any code if both conditions are false. We can add an else clause to the end of this if-elseif to account for any conditions that were not previously checked.

1
2
3
4
5
6
7
if condition1
    action1
elseif condition2
    action2
else
    action3
end

Let's simplify our nested code from before using the if-elseif-else statement format.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
%check if lowercase
if (mychar >= 97) && (mychar <=122)
    newchar = char(mychar-32);

%check if uppercase
elseif (mychar >= 65) && (mychar <=90)
    newchar = char(muchar+32);

%it must not be a letter
else
    newchar = 'not a letter'
end

disp(newchar)

That's better! Our code performs the same function, but is much easier to read. Each clause is clearly handling one of the three different cases--input is lowercase, uppercase, or not a letter.


Final Words

Selection statements, or conditional statements, are our first glimpse at a larger group of control statements that alter the how code is executed. They will allow us to create more dynamic programs able to handle a wider array of uses. In the next class, we will be examining other conditional statements useful functions for defining conditions.