1 / 23

Introduction to Engineering MATLAB - 13

Introduction to Engineering MATLAB - 13. Agenda Conditional statements If – end If – else – end if – elseif – else - end. CONDITIONAL STATEMENTS. Conditional statements enable MATLAB to make decisions. The process is similar to the way we (humans) make decisions.

cianna
Télécharger la présentation

Introduction to Engineering MATLAB - 13

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. Introduction to EngineeringMATLAB - 13 Agenda Conditional statements If – end If – else – end if – elseif – else - end

  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.

  3. Conditional Statements • NEED AN EXAMPLE OF HOW “IF…ELSE” STATEMENTS RUN IN A PRORGAM; E.G. MACHINERY IN A FACTORY

  4. THE FORM OF A CONDITIONAL STATEMENT if variableRelational operatorvariable or a number Relational operatorMeaning < Less than. <= Less than or equal to. > Greater than. >= Greater then or equal to. == Equal to. ~= Not equal to.

  5. EXAMPLES OF CONDITIONAL STATEMENTS Variable previously defined or a number variable If a < b If c >= 5 If a == b If a ~= 0 Relational operator

  6. Logical OperatorNameMeaning & AND True if both conditional statements are true. | OR True if either or both conditional statements are true. ~ NOT True if the conditional statement is false ADDING LOGICAL OPERATORS TO CONDITIONAL STATEMENTS

  7. EXAMPLES OF LOGICAL OPERATORS IN CONDITIONAL STATEMENTS if (d < h) & (x >= 7) True if d is smaller than h and x is equal or larger than 7. if (x ~= 13) | (y < 0) True if x is not equal to 13 and/or if y is smaller than 0. If ~(x == y) True if x is not equal to y.

  8. THE THREE FORMS OF THE if STATEMENT Ifconditional statement commands end ifconditional statement 1 command group 1 elseifconditional statement 2 command group 2 else command group 3 end ifconditional statement command group 1 else command group 2 end

  9. if weight ≥ 11.75 oz. False If weight ≥ 11.75 oz. Put can in package end True Put can in package end Example: if-end STATEMENT • In a soft drink factory, every can is weighed, and if it weighs 11.750 oz. or more, it is put in a package

  10. if False CONDITIONAL STATEMENT True COMMANDS end THE if – end STATEMENT Flowchart of the if – end statement: Ifconditional statement commands end

  11. EXAMPLE OF USING THE if – end STATEMENT % A script file that demonstrates the use of the if-end statement. % The user is asked to enter three grades. % The program calculates the average of the grades. % If the average is less than 60, a massage: % The student did not pass the course. is printed. score = input('Enter (as a vector) the scores of the three tests '); ave_grade = (score(1) + score(2) + score(3))/3; disp('The average grade is:') disp(ave_grade) if ave_grade < 60 disp('The student did not pass the course.') end

  12. EXAMPLE OF USING THE if – end STATEMENT Executing the script file of the previous slide in the Command Window: >> Lecture8Example1 Enter (as a vector) the scores of the three tests [78 61 85] The average grade is: 74.6667 >> Lecture8Example1 Enter (as a vector) the scores of the three tests [60 38 55] The average grade is: 51 The student did not pass the course.

  13. THE if – else - end STATEMENT Flowchart of the if – else – end statement: if False ifconditional statement command group 1 else command group 2 end CONDITIONAL STATEMENT True COMMAND GROUP 1 COMMAND GROUP 2 end

  14. EXAMPLE OF USING THE if – else - end STATEMENT % A script file that demonstrates the use of the if-else-end statement. % The user is asked to enter three grades. The program calculates % the average of the grades. If the average is less than 60, a % massage: The student did not pass the course. is printed. % Otherwise, a massage: The student passed the course. is printed. score = input('Enter (as a vector) the scores of the three tests '); ave_grade = (score(1) + score(2) + score(3))/3; disp('The average grade is:') disp(ave_grade) if ave_grade < 60 disp('The student did not pass the course.') else disp('The student passed the course.') end

  15. EXAMPLE OF USING THE if - else – end STATEMENT Executing the script file of the previous slide in the Command Window: >> Lecture8Example2 Enter (as a vector) the scores of the three tests [65 80 83] The average grade is: 76 The student passed the course. >> Lecture8Example2 Enter (as a vector) the scores of the three tests [60 40 55] The average grade is: 51.6667 The student did not pass the course.

  16. THE if – elseif – else – end STATEMENT ifconditional statement 1 command group 1 elseifconditional statement 2 command group 2 else command group 3 end

  17. FLOWCHART OF THE if – elseif – else – end STATEMENT if False CONDITIONAL STATEMENT 1 elseif False CONDITIONAL STATEMENT 2 True COMMAND GROUP 1 True COMMAND GROUP 3 COMMAND GROUP 2 end

  18. In-Class Exercise • Create a real-life situation that uses “if-else or if-else-if statements • Create a flow-chart showing the following: • The logic for the sorter and your team’s conveyor system • The logic for your team’s counter

  19. EXAMPLE OF 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 clear tip (The file continues on the next slide)

  20. (Continuation from the previous slide) bill = input('Enter the amount of the bill (in dollars): '); if bill <=0 disp('Error. The amount can not be zero or negative') elseif (bill > 0) & (bill <= 10) tip = 1.8; disp('The tip is (in dollars):') disp(tip) elseif (bill > 10) & (bill <= 60) tip = bill*0.18; disp('The tip is (in dollars):') disp(tip) else tip = bill*0.2; disp('The tip is (in dollars):') disp(tip) end

  21. EXECUTING THE SCRIPT FILE OF THE RESTAURAT TIP CALCULATION >> 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): -21 Error. The amount can not be zero or negative >> 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

  22. COMMENTS ABOUT if STATEMENTS • For every if command a computer program 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.

  23. Assignment #9 Problem 25 page 61 in the textbook. • Submit a printout of the script file and a printout of the command window when the script file was used for the different values of x. Problem 14 page 217 in the textbook. • Submit a printout of the function file and a printout of the command window when the function was used for the different values of W.

More Related