Part 2: Programming
Lecture1
Conditional Statements
A conditional statement is a command that allows MATLAB to
make a decision of whether to execute a group of commands
that follow the conditional statement, or to skip these
commands. In a conditional statement a conditional expression
is stated. If the expression is true, a group of commands that
follow the statement are executed. If the expression is false, the
computer skips the group. The basic form of a conditional
statement is:
2
The structure of the if-end conditional statement
if condition
action
end
The condition is any boolean expression
3
4
Relational Operators
5
Logical Operators:
The resulting type is logical 1 for true or 0 for false
6
7
8
The structure of the if-else-end conditional statement
if condition
action1
else
action2
end
• One and only one action is executed; which one depends
on the value of the condition (action1 if it is logical true or
action2 if it is false)
9
The structure of the if-elseif-else-end conditional statement
if cond1
action1
elseif cond2
action2
elseif cond3 % cond1 and cond2 False, cond3 True
action3
…
else % if no other conditions met
default_action
end
10
The structure of the if-elseif-else-end conditional statement
11
The structure of the nested if-else-end conditional statement
if cond1
action1
else
if cond2
action2
else
if cond3 % cond1 and cond2 False, cond3 True
action3
…
else
actionN
end
end
12
end
'
'
Ex 1:
height = input(‘Enter the height:’)
if height >170
disp(‘tall’)
elseif height<150
disp(‘small’)
else
disp(‘average’)
end 13
Ex 2:
Suppose that y = ln(x) for x > 10,
y = sqrt(x) for 0 <= x <= 10, and y = exp(x) - 1 for
x < 0. The following statements will compute y if x already
has a scalar value.
if x > 10
y = log(x);
elseif x >= 0
y = sqrt(x)
else
y = exp(x) - 1
end
14
Ex 3:
Determine if a value is nonzero.
15
Ex 4:
Determine if a value falls within a specified range
16
Ex 5:
Write a program to make a discount of 20% if the no. of books are
greater than 5
clc, clear;
Books = input(‘Enter the no of Books: ‘);
Cost=Books*25;
if Books > 5
Cost = (1 - 20/100)*Cost; % 20% discount
disp (Cost)
end
17
Ex 6: Write a MATLAB program to input the values of x,y then
print 'y is greater than x‘ if y is greater and 'x is greater than y’ if
x is greater and ‘x =y’ if they are equal.
clear, clc;
x=input(‘enter the value x’);
y=input(‘enter the value y’);
if (x<y)
disp ('y is greater than x’);
elseif (x>y)
disp (‘x is greater than y’);
else
disp (‘x is =y’);
end 18
Ex 7: Write program to find grade from the mark
s= input(‘Enter the Mark’);
if s>= 90
disp (‘Grade: A’);
elseif s>=80
disp (‘Grade: B’);
elseif s>=70
disp (‘Grade: C’);
elseif s>=60
disp ('Grade: D');
else
disp ('Grade: F’);
end 19
Ex 7: A worker is paid according to his hourly wage up to 40
hours, and 50% more for overtime. Write a program in a script file
that calculates the pay to a worker. The program asks the user to
enter the number of hours and the hourly wage. The program then
displays the pay.
t=input(‘Please enter the number of hours worked’);
h=input(‘Please enter the hourly wage in $’);
Pay=t*h;
if t>40
Pay=Pay+(t-40)*0.5*h;
end
fprintf(‘The pay is $ %5.2f’,Pay) 20
The switch-case statement
The switch-case statement is another method that can be used to
direct the flow of a program. It provides a means for choosing
one group of commands for execution out of several possible
groups. The structure of the statement is shown in the following
Figure.
21
switch var
case case1 % var == case1
action1;
case case2 % var == case2
action2;
case {case3,case4} % var == case3 || var == case4
action3;
…
otherwise % var doesn’t match any case
default_action;
end
22
Write a program in a script file that converts a quantity of
energy (work) given in units of either joule, ft-lb, cal, or eV to
the equivalent quantity in different units specified by the user.
The program asks the user to enter the quantity of energy, its
current units, and the desired new units. The output is the
quantity of energy in the new units.
The conversion factors are:
23
24
25
26
27
28