Presentation is loading. Please wait.

Presentation is loading. Please wait.

ENG 1181 College of Engineering Engineering Education Innovation Center P. 1 MAT - Conditional Statements Topics Covered: 1. if based conditional statements.

Similar presentations


Presentation on theme: "ENG 1181 College of Engineering Engineering Education Innovation Center P. 1 MAT - Conditional Statements Topics Covered: 1. if based conditional statements."— Presentation transcript:

1 ENG 1181 College of Engineering Engineering 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

2 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

3 ENG 1181 P. 3 THE FORM OF A CONDITIONAL STATEMENT if a < b if c >= 5 if a == b if a ~= 0 if (d 7) if (x ~= 13) | (y < 0) if Conditional expression consisting of relational and/or logical operators Examples: All variables must have assigned values. 174

4 ENG 1181 P. 4 FORMS OF THE if STATEMENT 174- 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

5 ENG 1181 P. 5 THE if–elseif-else-end STATEMENT 178- 179...... if conditional expression........ elseif conditional expression........ else........ end...... Group 1 of MATLAB commands. MATLAB program. Group 2 of MATLAB commands. Group 3 of MATLAB commands. Examples of if-end and if-else-end can be found on pages 174-179 of the text MATLAB program.

6 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 STATEMENT Write program that calculates tip based on amount of bill, using the following rules

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

8 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 value is control by ; and the disp command

9 ENG 1181 P. 9 >> 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 USING THE if–elseif-else-end STATEMENT

10 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

11 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

12 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

13 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


Download ppt "ENG 1181 College of Engineering Engineering Education Innovation Center P. 1 MAT - Conditional Statements Topics Covered: 1. if based conditional statements."

Similar presentations


Ads by Google