The Simple if Structure

if "relation"
    "statement"
end
In [1]:
G = 10;
if G<50
    disp('G is a small value equal to:')
    disp(G);
end
G is a small value equal to:
    10


The if/else Structure

if "relation"
    "statement1"
else
    "statement2"
end
In [2]:
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
The number of columns of the first matrix must equal the number of rows of the second matrix

In [ ]:
x = input('Enter a value of x greater than 0: ');
Enter a value of x greater than 0: 4
In [ ]:
if x>0
    y = log(x)
else
    beep
    disp('The input to the log function must be positive')
end
y = 

    1.3863

The if/else Structure

if "relation1"
    "statement1"
elseif "relation2"
    "statement2"
else
    "statement3"
end
In [3]:
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
You may have a youth license

Switch and Case

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
In [ ]:
city = input('Enter the name of a city in single quotes: ');
Enter the name of a city in single quotes: 'Boston'
In [ ]:
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 box
In [4]:
city = menu('Select a city from the menu: ', 'Boston','Denver','Honolulu');

If you choose "Denver".

In [5]:
switch city
case 1
    disp('$345')
case 2
    disp('$150')
case 3
    disp('Stay home and study')
otherwise
    disp('Not on file')
end
$150

For Loops

for "index = [matrix]"
    "commands to be executed"
end
In [6]:
for k = [1,3,7]
    k
end
k =

     1


k =

     3


k =

     7


In [7]:
for x = 1:3
    a = exp(x)*2
end
a =

    5.4366


a =

   14.7781


a =

   40.1711


In [8]:
for k = 1:5
    a(k) = k^2
end
a =

     1


a =

     1     4


a =

     1     4     9


a =

     1     4     9    16


a =

     1     4     9    16    25


In [9]:
scores = [76, 45, 98, 97, 93];
count = 0;
for k = 1:length(scores)
    if scores (k) > 90
        count = count + 1
    end
end
count =

     1


count =

     2


count =

     3


While Loops

while "criterion"
    "commands to be executed"
end
In [10]:
k = 0;
while k < 3
    k = k + 1
end
k =

     1


k =

     2


k =

     3


In [ ]:
k = 0;
while k < 3
    k = k + 1;
    b(k) = 5^k
end
In [11]:
a = 2;
while a < 12
    a = a + 3
end
a =

     5


a =

     8


a =

    11


a =

    14


In [ ]:
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
In [ ]:
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

Nested Loops

In [12]:
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
maximum =

    12    18     6    13


Break

break: Terminates execution of a for or while loop.

In [ ]:
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
In [ ]:
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

continue: Passes control to the next iteration of a for or while loop.

In [ ]:
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:

Recall: Relational and Logical Operators

< 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