Transcript
Page 1: MAT - Conditional Statements

• ENG 1181

College of EngineeringEngineering Education Innovation Center

P. 1

MAT - Conditional Statements

Topics Covered:

1. if based conditional statements.

if–end

if-else–end

if-elseif-else–end

2. switch-case

Programming in MATLAB / Chapter 6

Page 2: MAT - Conditional Statements

• ENG 1181

P. 2

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.

174-179

Page 3: MAT - Conditional Statements

• ENG 1181

P. 3

THE FORM OF A CONDITIONAL STATEMENT

if a < b

if c >= 5

if a == b

if a ~= 0

if (d < h) & (x > 7)

if (x ~= 13) | (y < 0)

if Conditional expression

consisting of relational and/or logical operatorsExamples:

All variables must have assigned values.

174

Page 4: MAT - Conditional Statements

• ENG 1181

P. 4

FORMS OF THE if STATEMENT174-179

if conditional statement 1

command group 1

elseif conditional statement 2

command group 2

(Optional)elseif conditional statement # command group #

(Optional)

else %no conditional statement

command group n+1

end

if conditional statement

command group 1

else

command group 2

end

if conditional statement

command group

end

Page 5: MAT - Conditional Statements

• ENG 1181

P. 5

THE if–elseif-else-end STATEMENT

178-179

......

......if conditional expression

........

........

........elseif conditional expression

........

........

........else

........

........

........end......

Group 1 of MATLABcommands.

MATLAB program.

Group 2 of MATLABcommands.

Group 3 of MATLABcommands.

False

ElseIfconditional

True

False

TrueCommandGroup 1

End

Ifconditional

CommandGroup 2

CommandGroup 3

Examples of if-end and if-else-end can be found on pages 174-179 of the text

MATLAB program.

Page 6: MAT - Conditional Statements

• ENG 1181

P. 6

• Bill less than $10• Tip is $1.80

• Bill between $10 and $60• Tip is %18

• Bill above $60• Tip is %20

USING THE if–elseif-else-end STATEMENTWrite program that calculates tip based on amount of bill, using the following rules

False

ElseIf$10 < bill < $60

True

False

Truetip = $1.80

End

Ifbill < $10

tip = bill * 0.18

tip = bill * 0.20

Else

Page 7: MAT - Conditional Statements

• ENG 1181

P. 7

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)

Page 8: MAT - Conditional Statements

• ENG 1181

P. 8

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)

USING THE if–elseif-else-end STATEMENT

Note how tip’s valueis control by ; and the disp command

Page 9: MAT - Conditional Statements

• ENG 1181

P. 9

>> Lecture8Example3Enter the amount of the bill (in dollars): 15The tip is (in dollars): 2.70

>> Lecture8Example3Enter the amount of the bill (in dollars): 6The tip is (in dollars): 1.80

>> Lecture8Example3Enter the amount of the bill (in dollars): 100The tip is (in dollars): 20.00

USING THE if–elseif-else-end STATEMENT

Page 10: MAT - Conditional Statements

• ENG 1181

P. 10

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

174-179

Page 11: MAT - Conditional Statements

• ENG 1181

P. 11

THE switch-case STATEMENT switch-case is similar to if-

elseif-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)

180- 184

Page 12: MAT - Conditional Statements

• ENG 1181

P. 12

THE switch-case STATEMENT

180- 184

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

Page 13: MAT - Conditional Statements

• ENG 1181

P. 13

THE switch-case STATEMENT

180- 184

x = input (‘Purchase Class: ‘,’s’);x = upper(x) %change to upper case

switch x case ‘A’

rate = 2.00; case ‘B’

rate = 3.00; case ‘C’

rate = 3.50; case ‘D’

rate = 4.00; otherwise

rate = 0; end