1 / 13

MAT - Conditional Statements

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. 174- 179. CONDITIONAL STATEMENTS. Conditional statements enable MATLAB to make decisions.

ravi
Download Presentation

MAT - Conditional Statements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 P. 1

  2. 174- 179 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. P. 2

  3. 174 THE FORM OF A CONDITIONAL STATEMENT if Conditional expression consisting of relational and/or logical operators Examples: 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. P. 3

  4. 174- 179 FORMS OF THE if STATEMENT if conditional statement 1 command group 1 elseifconditional statement 2 command group 2 … (Optional) elseifconditional statement # command group # … (Optional) else %no conditional statement command group n+1 end if conditional statement command group end if conditional statement command group 1 else command group 2 end P. 4

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

  6. USING THE if–elseif-else-end STATEMENT Write program that calculates tip based on amount of bill, using the following rules • Bill less than $10 • Tip is $1.80 • Bill between $10 and $60 • Tip is %18 • Bill above $60 • Tip is %20 P. 6

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

  8. Note how tip’s value is control by ; and the disp command 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) P. 8

  9. 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 P. 9

  10. 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 P. 10

  11. 180- 184 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) P. 11

  12. 180- 184 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 P. 12

  13. 180- 184 THE switch-case STATEMENT 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 P. 13

More Related