Skip to main content
Logo image

Section 7.5 The if-elseif-else-end command

Of course there is also a way to account for multiple else cases. Here is the general syntax of the if-elseif-else-end command syntax:
if condition
    command
    command
    ...
elseif
    command
    command
    ...
else
    command
    command
    ...
end
Back to our example! Here is the latest version of our script:
answer = input('How much chocolate have you had today (1-10)? ');
if answer<5
   disp('Oh no! Be sure to eat more chocolate!');
elseif answer<8
   disp('Good for you. But have some more anyway!');
else
   disp('You are doing great! Maybe have one more piece?');
end
At this point it’s probably rather clear to you what the output of this script file is, depending on the user’s input. If not, just try it out! Or have some chocolate. Or both!