if "relation"
"statement"
end
G = 10;
if G<50
disp('G is a small value equal to:')
disp(G);
end
if "relation"
"statement1"
else
"statement2"
end
A = [1 3 6; 2 8 4];
B = [2 1 0; 3 7 1];
if size(A,2) == size(B,1)
result = A*B
else
disp('The number of columns of the first matrix must equal the number of rows of the second matrix')
end
x = input('Enter a value of x greater than 0: ');
Enter a value of x greater than 0: 4
if x>0
y = log(x)
else
beep
disp('The input to the log function must be positive')
end
y =
1.3863
if "relation1"
"statement1"
elseif "relation2"
"statement2"
else
"statement3"
end
age = 17;
if age<16
disp('Sorry - you will have to wait')
elseif age<18
disp('You may have a youth license')
elseif age<70
disp('You may have a standard license')
else
disp('Drivers over 70 require a special license')
end
switch "variable"
case "option1"
"code to be executed if variable is equal to option 1"
case "option2"
"code to be executed if variable is equal to option 2"
.
.
.
case "optionN"
"code to be executed if variable is equal to option N"
end
city = input('Enter the name of a city in single quotes: ');
Enter the name of a city in single quotes: 'Boston'
switch city
case 'Boston'
disp('$345')
case 'Denver'
disp('$150')
case 'Honolulu'
disp ('Stay home and study')
otherwise
disp('Not on file')
end
$345
menu('mtitle','option1','option2',. . .,'optionN')
: Creates a multiple-choice dialog boxcity = menu('Select a city from the menu: ', 'Boston','Denver','Honolulu');
If you choose "Denver".
switch city
case 1
disp('$345')
case 2
disp('$150')
case 3
disp('Stay home and study')
otherwise
disp('Not on file')
end
for "index = [matrix]"
"commands to be executed"
end
for k = [1,3,7]
k
end
for x = 1:3
a = exp(x)*2
end
for k = 1:5
a(k) = k^2
end
scores = [76, 45, 98, 97, 93];
count = 0;
for k = 1:length(scores)
if scores (k) > 90
count = count + 1
end
end
while "criterion"
"commands to be executed"
end
k = 0;
while k < 3
k = k + 1
end
k = 0;
while k < 3
k = k + 1;
b(k) = 5^k
end
a = 2;
while a < 12
a = a + 3
end
scores = [76, 45, 98, 97];
count = 0;
k = 0;
while k < length(scores)
k = k+1;
if scores(k) > 90
count = count + 1
end
end
x = input('Enter a positive value of x ')
while x <= 0
disp('log(x) is not defined for negative numbers')
x = input('Enter a positive value of x ')
end
y = log10(x);
fprintf('The log base 10 of %4.2f is %5.2f nn', x, y)
Enter a positive value of x -2
x =
-2
log(x) is not defined for negative numbers
Enter a positive value of x 2
x =
2
The log base 10 of 2.00 is 0.30
x = [1 2 6 3; 4 8 2 1; 12 18 3 5; 6 4 2 13];
[rows, cols] = size(x);
for k = 1:cols
maximum(k) = x(1,k);
for j = 1:rows
if x(j,k) > maximum(k)
maximum(k) = x(j,k);
end
end
end
maximum
break
: Terminates execution of a for
or while
loop.
n= 0;
while n<10
n = n + 1;
a = input('Enter a value greater than 0: ')
if a<=0
disp('You must enter a positive number')
disp('This program will terminate')
break
end
disp('The natural logarithm of that number is ')
disp(log(a))
end
Enter a value greater than 0: -3
a =
-3
You must enter a positive number
This program will terminate
n= 0;
while n<10
n = n + 1;
a = input('Enter a value greater than 0: ')
if a<=0
disp('You must enter a positive number')
disp('This program will terminate')
break
end
disp('The natural logarithm of that number is ')
disp(log(a))
end
Enter a value greater than 0: 2
a =
2
The natural logarithm of that number is
0.6931
Enter a value greater than 0:
Tip: In order to break a loop in the command window you can use ctrl+C.
continue
: Passes control to the next iteration of a for
or while
loop.
n = 0;
while n<10
n = n + 1;
a = input('Enter a value greater than 0: ')
if a <= 0
disp('You must enter a positive number')
disp('Try again')
continue
end
disp('The natural logarithm of that number is ')
disp(log(a))
end
Enter a value greater than 0: -3
a =
-3
You must enter a positive number
Try again
Enter a value greater than 0: 2
a =
2
The natural logarithm of that number is
0.6931
Enter a value greater than 0: 4
a =
4
The natural logarithm of that number is
1.3863
Enter a value greater than 0:
< | less than |
<= | less than or equal to |
> | greater than |
>= | greater than or equal to |
== | equal to |
~= | not equal to |
& | and |
~ | not |
$\mid$ | or |
xor | exclusive or |