if logicalExpression statements end |
% Generate a random number a = randi(100, 1); % If it is even, divide by 2 if rem(a, 2) == 0 disp('a is even') b = a/2; end |
reply = input('Would you like to see an echo? (y/n): ','s'); if strcmp(reply,'y') disp(reply) end |
x = 42; if exist('myfunction.m','file') && (myfunction(x) >= pi) disp('Expressions are true') end |
if logicalExpression statements else catch-all statements end |
% Generate a random number a = randi(100, 1); % If it is even, divide by 2 % Otherwise, multiply by 3 if rem(a, 2) == 0 disp('a is even') b = a/2 else disp('a is odd') b = a*3 end |
reply = input('Do you like fruit? (y/n): ','s'); if strcmp(reply,'y') disp('Great, here is an apple') else disp('Ok, here is an orange') end |
if logicalExpression statements elseif alternative statements 1 elseif alternative statements 2 elseif alternative statements 3 end |
% Generate a random number a = randi(100, 1); % If it is even and greater than 20, divide by 2 % If it is odd and less than 70, multiply by 3 if rem(a, 2) == 0 && a > 20 disp('a is even and greater than 20') b = a/2 elseif rem(a, 2) == 1 && a < 70 disp('a is odd and less than 70') b = a*3 end |
reply = input('Choose a fruit? (a=apple, b=banana, o=orange): ','s'); if strcmp(reply,'a') disp('Great, here is an apple') elseif strcmp(reply,'b') disp('Banana...great choice!') elseif strcmp(reply,'o') disp('Ok, here is an orange') end |
if logicalExpression statements elseif alternative statements else catch-all statements end |
% Generate a random number a = randi(100, 1); % If it is even and greater than 20, divide by 2 % If it is odd and less than 70, multiply by 3 % Otherwise, set = 0 if rem(a, 2) == 0 && a > 20 disp('a is even and greater than 20') b = a/2 elseif rem(a, 2) == 1 && a < 70 disp('a is odd and less than 70') b = a*3 else disp('a is odd and greater than or equal to 70, or a is even and less then or equal to 20') b = 0 end |
reply = input('Choose a fruit? (a=apple, b=banana, o=orange): ','s'); if strcmp(reply,'a') disp('Great, here is an apple') elseif strcmp(reply,'b') disp('Banana...great choice!') elseif strcmp(reply,'o') disp('Ok, here is an orange') else disp('You did not choose fruit from our list.') end |
A = nan(20,1); for i=10:-1:-10 if(i <= 0) break; end A(i) = i; end A(:) |
A = nan(20,1); for i=1:20 if(mod(i,2) == 0) continue; else A(i) = i; end end A |
switch switch_expression case case_expression statements case case_expression statements ... otherwise statements end |
n = input('Enter a number: '); switch n case -1 disp('negative one') case 0 disp('zero') case 1 disp('positive one') otherwise disp('other value') end |
x = [12 64 24]; plottype = input('What type of plot would you like? (pie, pie3, bar)'); switch plottype case 'bar' bar(x) title('Bar Graph') case {'pie','pie3'} pie3(x) title('Pie Chart') otherwise warning('Unexpected plot type. No plot created.') end |
"In my sentences I go where no man has gone before." |
"Incredibly, while these 18 to 20 year-olds
cannot legally buy a beer, cannot purchase a bottle of wine and cannot order a drink
in a bar, right now they can walk into any gun shop, any pawn shop, any gun show,
anywhere in America and buy a handgun."
|