Case Statement

Back arrow to return.

Demo 1: Case Statement

% file cmdcase.m
% command loop
not_done = 1;  % boolean
while not_done
    arg1 = input('arg1: ');
    arg2 = input('arg2: ');
    need_command = 1;
    while need_command
    command = lower(input('cmd: ', 's'));
    switch command(1)    % string indexes like vec
        case 'a'
           arg1+arg2
           break;     % one way out
        case 's'
           arg1-arg2
           need_command = 0;  % another way out
        case {'q', 'x', 'e'}
             not_done = 0;
             break;
            otherwise
            disp('a(dd),s(ub),q,e,x (quit)');
        end %switch
    end % while need_command
end % while not-done

Interaction is like:


>> cmdcase
arg1: 56
arg2: 3
cmd: addit
ans =
    59
arg1: 6
arg2: 8
cmd: subtr
ans =
    -2
arg1: 0 
arg2: -1
cmd: what
a(dd),s(ub),q,e,x (quit)
cmd: q
>> 

Back arrow to return.

---

Last update: 04/20/2011: RN