ENG 1181

advertisement
•
ENG 1181
MAT – Conditional Statements
Topics:
1. Conditional statements
if-end
if-else-end
if-elseif-else-end
College of Engineering
Engineering Education Innovation Center
Programming in MATLAB
Chapter 6 (4th Edition)
•
ENG 1181
Conditional Logic and Program Flow Control
An example of a simple MatLab program:
%
% This program finds the sum of n^2 for
% integers from 1 to 10
%
x = 1 : 10 ;
y = x.^2;
Result = sum(y)
•
In this program the commands are executed in the order
they are typed, one at a time, from first to last.
•
There are many situations in which programs require
much more complex logic.
•
In order to do this, we use "conditional statements",
"loops" and "program flow control".
•
ENG 1181
Program Flow Control
• MATLAB provides commands to allow logic to be added
to programs.
• Today we will learn if-end statement structures.
• In the next basics lecture, we will learn for-end loops.
• There are other program flow control structures you can
learn from the textbook.
•
ENG 1181
Conditional Statements
Conditional statements let MATLAB make decisions
In the program a condition is specified:
• If the condition is met, one set of actions is taken.
• If the condition is not met, a different set of actions
is taken.
Example: "Did I win the Lottery?"
If the statement is true: I will quit college, buy a new car, and go fishing.
If the statement is false: I will study harder so I can get a good job.
•
ENG 1181
The "if-end" conditional statement structure
if conditional expression
...
MatLab statements
...
end
Examples:
relational and/or
logical operators
if a < b
or
if c >= 5
a, b and c must have
assigned values.
•
ENG 1181
The "if–end" and "if–else–end" structures
if conditional statement
group of Matlab commands
end
if conditional statement
command group 1
else
command group 2
end
executable
statements
•
ENG 1181
The "if–elseif–else–end" structure
MATLAB statements
if conditional expression 1
If
conditional
Group 1 commands
elseif conditional expression 2
Group 2 commands
False
True
ElseIf
conditional
Command
Group 1
False
True
Command
Group 2
else conditional expression 3
End
Group 3 commands
end
The final "else" is optional
MATLAB statements
Command
Group 3
•
ENG 1181
The if–elseif–else–end structure: example
Write program to calculate
a tip based on the amount
of a restaurant bill using
the following rules:
If
bill < $ 10
True
tip = $ 1.80
• Bill less than $10
• Tip is $1.80
• Bill between $10 and $60
• Tip is %18
• Bill above $60
• Tip is %20
False
ElseIf
$ 10 < bill < $ 60
Else
True
tip =
bill * 0.18
End
False
tip =
bill * 0.20
•
ENG 1181
The if-elseif-else-end structure: example
bill = input('Enter the amount of the bill (in dollars): ');
if (bill <= 10)
tip = 1.8; % suppress output
elseif (bill > 10) & (bill <= 60)
tip = bill*0.18;
If
bill < $10
else
True
ElseIf
$10 < bill < $60
tip = bill*0.2;
end
tip = $1.80
format bank
End
False
Else
True
tip =
bill * 0.18
disp('The tip is:')
disp(tip)
False
tip =
bill * 0.20
•
ENG 1181
The if-elseif-else-end structure: output
>> bill
Enter the amount of the bill (in dollars): 15
The tip is (in dollars):
2.70
>> bill
Enter the amount of the bill (in dollars): 6
The tip is (in dollars):
1.80
>> bill
Enter the amount of the bill (in dollars): 100
The tip is (in dollars):
20.00
If
bill < $10
False
True
ElseIf
$10 < bill < $60
tip = $1.80
Else
True
tip =
bill * 0.18
End
False
tip =
bill * 0.20
•
ENG 1181
Rules about if–end Structures
• Every if command must have an end.
• A program can have many if … end statements following
•
•
•
•
each other.
A 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 if-elseif-elseend statements.
The else condition is not required.
When else is used, a conditional statement is NOT added.
Download