Lab 3: Selection Statements - Wednesday, 2016-02-10

This lab will focus on selection statements and MATLAB's menu function. When you have completed this exercise, please upload your published work to your cs130r directory.

Contents

Getting Started

Let's assume that a user has input the value below into the variable ui. Run this section to initialize the variable.

ui = 23;

if Statement

Now, write two if statements. The first should check if the variable ui is greater than 15, and the second should check if the variable ui is less than or equal to 15. Each statement should have an action that prints the value of ui, and indicates which if statement was executed.

if ui > 15
    fprintf('%d is greater than 15\n', ui)
end

if ui <= 15
    fprintf('%d is less than or equal to 15\n', ui)
end
23 is greater than 15

if-else Statement

Let's assume that a user has input the value below into the variable ui. Write an if-else statement that tells the user if the character is before or after the letter 'm' in the alphabet. You can assume that the user will only enter a lowercase letter.

ui = 't';

if ui < int8('m')
    fprintf('%c is before the letter m\n', ui)
else
    fprintf('%c is after the letter m\n', ui)
end
t is after the letter m

Nested if-else Statements

We would like to check if the user has entered the character a, b, or c. Write a nested if-else statement to check the variable ui, and tell the user which letter has been entered. Your statement should also indicate if neither a, b, nor c is entered.

ui = 'c';

if ui == 'a'
    disp('you entered an a')
else
    if ui == 'b'
        disp('you entered a b')
    else
        if ui == 'c'
            disp('you entered a c')
        else
            disp('you did not enter a, b, or c')
        end
    end
end
you entered a c

if-elseif Statement

Now, convert the previous nested if-else statement into an if-elseif statement.

ui = 'b';

if ui == 'a'
    disp('you entered an a')
elseif ui == 'b'
    disp('you entered a b')
elseif ui == 'c'
    disp('you entered a c')
else
    disp('you did not enter a, b, or c')
end
you entered a b

switch Statement

We can rewrite our if-elseif statement above as a switch statement. This provides a convenient way to perform a specific action when a variable matches one of several conditions. Check out the documentation on switch statements to find the syntax. Then, rewrite the if-elseif statement from above using this syntax.

ui = 'c';

switch ui
    case 'a'
        disp('you entered an a')
    case 'b'
        disp('you entered a b')
    case 'c'
        disp('you entered a c')
    otherwise
        disp('you did not enter a, b, or c')
end
you entered a c

menu Function

The built-in menu function provides a convenient way to get user input. Run the code below to see the graphic prompt that it creates. Click a menu button and observe the value assigned to the variable ui. Now, write a switch statement to print out the selection made by the user.

ui = menu('Pick a fruit', 'apples', 'blueberries', 'cantaloupes');

switch ui
    case 1
        disp('you picked apples')
    case 2
        disp('you picked blueberries')
    case 3
        disp('you picked cantaloupes')
    otherwise
        disp('you did not select a fruit')
end
you picked apples