Main Content

Use .NET Enumerations to Test for Conditions

With relational operators, you can use enumeration members in if and switch statements and other functions that test for equality.

Using Switch Statements

The following Reminder function displays a message depending on the day of the week:

function Reminder(day)
% day = System.DayOfWeek enumeration value
% Add error checking here
switch(day)
    case System.DayOfWeek.Monday
        disp("Department meeting at 10:00")
    case System.DayOfWeek.Tuesday
        disp("Meeting Free Day!")
    case {System.DayOfWeek.Wednesday System.DayOfWeek.Friday}
        disp("Team meeting at 2:00")
    case System.DayOfWeek.Thursday
        disp("Volley ball night")
end
end

For example, type:

today = System.DayOfWeek.Wednesday;
Reminder(today)
ans =
Team meeting at 2:00

Using Relational Operations

Create the following function to display a message:

function VolleyballMessage(day)
% day = System.DayOfWeek enumeration value
if gt(day,System.DayOfWeek.Thursday)
    disp("See you next week at volleyball.")
else
    disp("See you Thursday!")
end
end

For a day before Thursday:

myDay = System.DayOfWeek.Monday;
VolleyballMessage(myDay)
See you Thursday!

For a day after Thursday:

myDay = System.DayOfWeek.Friday;
VolleyballMessage(myDay)
See you next week at volleyball.