if– elseif – else – end

advertisement
•
ENG 1181
MAT - Conditional Statements
Programming in MATLAB / Chapter 6
Topics Covered:
1. if based conditional statements.
if–end
if-else–end
if-elseif-else–end
2. switch-case
College of Engineering
Engineering Education Innovation Center
•
ENG 1181
174-
CONDITIONAL STATEMENTS
 Conditional statements enable MATLAB to make
decisions.
 The process is similar to the way we (humans)
make decisions.
 A condition stated. If the condition is met, one set of
actions is taken. If the condition is not met, either
nothing is done, or a second set of actions is taken.
Example:
If I win the Lottery,
•I will quit college, buy a new car, and go fishing.
If I do not win the Lottery,
•I will study harder so that I can get a better job.
179
•
ENG 1181
174
THE FORM OF A
CONDITIONAL STATEMENT
if
Conditional expression
Examples:
consisting of relational
and/or logical operators
if a < b
if c >= 5
if a == b
if a ~= 0
if (d < h) & (x > 7)
if (x ~= 13) | (y < 0)
All variables must
have assigned values.
•
ENG 1181
174-
FORMS OF THE if STATEMENT
if conditional statement 1
if conditional statement
command group
command group 1
elseif conditional statement 2
end
command group 2
…
if conditional statement
command group 1
else
command group 2
end
(Optional)
elseif conditional statement #
command group #
…
(Optional)
else %no conditional statement
command group n+1
end
179
•
THE if–elseif-else-end
STATEMENT
......
If
conditional
False
True
ElseIf
conditional
Command
Group 1
True
Command
Group 2
End
False
Command
Group 3
ENG 1181
178-
179
MATLAB program.
......
if conditional expression
........
........ Group 1 of MATLAB
........ commands.
elseif conditional expression
........
........ Group 2 of MATLAB
........ commands.
else
........
........ Group 3 of MATLAB
........ commands.
end
......
MATLAB program.
Examples of if-end and if-else-end can be
found on pages 174-179 of the text
•
ENG 1181
USING THE if–elseif-else-end STATEMENT
Write program that calculates tip based on amount of bill, using
the following rules
If
• Bill less than $10
bill < $10
False
• Tip is $1.80
True
• Bill between $10 and $60
ElseIf
• Tip is %18
$10 < bill < $60
• Bill above $60
True
tip = $1.80
• Tip is %20
tip =
bill * 0.18
End
False
Else
tip =
bill * 0.20
•
ENG 1181
USING THE if–elseif-else-end STATEMENT
% A script file that demonstrates the use of the
% if-elseif-else-end statement.
% The program calculates the tip in a restaurant
% according to the amount of the bill.
%
% If the bill is less than $10 the tip is $1.80.
% Between $10 and $60 the tip is 18% of the bill.
% Above $60 the tip is 20% of the bill.
format bank
(The file continues on the next slide)
•
ENG 1181
USING THE if–elseif-else-end STATEMENT
bill = input('Enter the amount of the bill (in dollars): ');
if (bill <= 10)
tip = 1.8;
elseif (bill > 10) & (bill <= 60)
tip = bill*0.18;
else
tip = bill*0.2;
end
disp('The tip is (in dollars):')
disp(tip)
Note how tip’s value
is control by ; and
the disp command
•
ENG 1181
USING THE if–elseif-else-end STATEMENT
>> Lecture8Example3
Enter the amount of the bill (in dollars): 15
The tip is (in dollars):
2.70
>> Lecture8Example3
Enter the amount of the bill (in dollars): 6
The tip is (in dollars):
1.80
>> Lecture8Example3
Enter the amount of the bill (in dollars): 100
The tip is (in dollars):
20.00
•
ENG 1181
174-
179
COMMENTS ABOUT if–end STATEMENTS
 For every if command must have an end command.
 A program can have many if … end statements
following each other.
 A computer program can perform the same task
using different combinations of if - end, if – else –
end, and if– elseif – else – end statements.
 Multiple elseif conditions are allowed within an if–
elseif – else – end statement.
 An else condition is not required.
When else is used, a conditional statement is
NOT added
•
ENG 1181
180-
184
THE switch-case STATEMENT
 switch-case is similar to ifelseif-end, except must be
an exact match.
You cannot
use
switch-case
with
a
range, such as <0.
 switch on scalar or string
 otherwise == else
(optional)
•
ENG 1181
180-
THE switch-case STATEMENT
x = input (‘Resistance: ‘);
switch x
case 100
out= x;
case 200
out= 2*x;
case 300
out= 3*x;
case 400
out= 4*x;
otherwise
out = 0;
end
184
•
ENG 1181
180-
THE switch-case STATEMENT
x = input (‘Purchase Class: ‘,’s’);
x = upper(x) %change to upper case
switch x
case ‘A’
rate =
case ‘B’
rate =
case ‘C’
rate =
case ‘D’
rate =
otherwise
rate =
end
2.00;
3.00;
3.50;
4.00;
0;
184
Download