Download presentation
Presentation is loading. Please wait.
Published byLynne Fox Modified over 6 years ago
1
Introduction to Programming for Mechanical Engineers (ME 319)
Lecture 6.2
2
Things you should have known so far!
If you didn’t have it before, you should have developed some knowledge of logic design. True-False or 0-1 representation Understand the nature of logical and relational operators. Clearly forming logical and relational operators to represent the case(s) you may have. Correctly arrange combined conditions to represent more complex case(s).
3
Conditional Statements: if-end
Conditional statements are statements designed to allow MATLAB to make decision making. Here, the MATLAB may execute a group of commands or skip them based on the satisfaction of a condition(s). Syntax: if conditional expression(s), logical and relational body of ‘if’ statement end START IF STATEMENT FALSE TRUE Group of commands Example: if n_boys>=0 n_students = n_girls + n_boys; end END
4
Conditional Statements: if-else-end
If-else-end is a two way conditional statement. The statement checks the satisfaction of the condition. If it is satisfied, it chooses command group 1 and executes it. If the condition is false, it chooses group 2 and executes it. START IF STATEMENT FALSE TRUE Example: if GPA>=3.0 DECISION = ‘PASS' else DECISION = ‘FAIL' end Group of commands 2 Group of commands 1 END
5
Conditional Statements: if-elseif-end
If-elseif-end is a multi way conditional statement. The statement checks the satisfaction of the first condition. If it is satisfied, it chooses commands group 1 and executes it. If the condition is false, it goes to the second else-if statement. It performs the same check to choose group 2 or 3. START IF STATEMENT FALSE Example: if GPA>=3.75 EVALUATION = ‘A' elseif GPA<3.75 & GPA>=3.50 EVALUATION = ‘B+' else % GPA<3.50 & GPA>=3.20 EVALUATION = ‘B‘ end ELSE-IF STATEMENT TRUE FALSE Group of commands 1 TRUE Group of commands 3 Group of commands 2 END
6
.. Conditional Statements: switch-case-otherwise-end
switch-case-otherwise-end is a conditional statement that works in parallel. Only one case is selected and the commands that follow it will be executed. START switch .. otherwise case value 3 case value 2 case value 1 END
7
Conditional Statements: switch-case-otherwise-end
passengers = input('Enter number of passengers '); passengers = ceil(passengers); switch passengers case 1 Car_type = 'Coupe' Rate_per_day_USD = 27 case 2 case 3 Car_type = 'Economy' Rate_per_day_USD = 22 case 4 Car_type = 'Sedan' Rate_per_day_USD = 25 case 5 Car_type = 'Full Size' Rate_per_day_USD = 40 case 6 Car_type = 'Minivan' Rate_per_day_USD = 57 otherwise fprintf('We do not hold cars that fit this number of passengers\n\n') end Enter number of passengers 5 Car_type = Full Size Rate_per_day_USD = 40.00
8
Thank you for the attention
Any Questions and Suggestions please…
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.