Presentation is loading. Please wait.

Presentation is loading. Please wait.

ENG 1181 College of Engineering Engineering Education Innovation Center MAT – Conditional Statements Topics: 1.Conditional statements if-end if-else-end.

Similar presentations


Presentation on theme: "ENG 1181 College of Engineering Engineering Education Innovation Center MAT – Conditional Statements Topics: 1.Conditional statements if-end if-else-end."— Presentation transcript:

1 ENG 1181 College of Engineering Engineering Education Innovation Center MAT – Conditional Statements Topics: 1.Conditional statements if-end if-else-end if-elseif-else-end Programming in MATLAB Chapter 6 (4th Edition)

2 ENG 1181 % % This program finds the sum of n^2 for % integers from 1 to 10 % x = 1 : 10 ; y = x.^2; Result = sum(y) An example of a simple MatLab program: In this program the commands are executed in the order they are typed, one at a time, from first to last. There are many situations in which programs require much more complex logic. In order to do this, we use "conditional statements", "loops" and "program flow control". Conditional Logic and Program Flow Control

3 ENG 1181 Program Flow Control MATLAB provides commands to allow logic to be added to programs. Today we will learn if-end statement structures. In the next basics lecture, we will learn for-end loops. There are other program flow control structures you can learn from the textbook.

4 ENG 1181 Conditional Statements Conditional statements let MATLAB make decisions In the program a condition is specified: If the condition is met, one set of actions is taken. If the condition is not met, a different set of actions is taken. Example: "Did I win the Lottery?" If the statement is true: I will quit college, buy a new car, and go fishing. If the statement is false: I will study harder so I can get a good job.

5 ENG 1181 The "if-end" conditional statement structure if a < b or if c >= 5 if conditional expression... MatLab statements... end relational and/or logical operators Examples: a, b and c must have assigned values.

6 ENG 1181 The "if–end" and "if–else–end" structures if conditional statement command group 1 else command group 2 end if conditional statement group of Matlab commands end executable statements

7 ENG 1181 The "if–elseif–else–end" structure if conditional expression 1 elseif conditional expression 2 else conditional expression 3 end Group 1 commands MATLAB statements Group 2 commands Group 3 commands MATLAB statements The final "else" is optional

8 ENG 1181 Bill less than $10 Tip is $1.80 Bill between $10 and $60 Tip is %18 Bill above $60 Tip is %20 The if–elseif–else–end structure: example Write program to calculate a tip based on the amount of a restaurant bill using the following rules: If bill < $ 10 tip = $ 1.80 ElseIf $ 10 < bill < $ 60 tip = bill * 0.20 tip = bill * 0.18 False True Else End

9 ENG 1181 bill = input('Enter the amount of the bill (in dollars): '); if (bill <= 10) tip = 1.8;% suppress output elseif (bill > 10) & (bill <= 60) tip = bill*0.18; else tip = bill*0.2; end format bank disp('The tip is:') disp(tip) The if-elseif-else-end structure: example

10 ENG 1181 >> bill Enter the amount of the bill (in dollars): 15 The tip is (in dollars): 2.70 >> bill Enter the amount of the bill (in dollars): 6 The tip is (in dollars): 1.80 >> bill Enter the amount of the bill (in dollars): 100 The tip is (in dollars): 20.00 The if-elseif-else-end structure: output

11 ENG 1181 Rules about if–end Structures Every if command must have an end. A program can have many if … end statements following each other. A 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 if-elseif-else- end statements. The else condition is not required. When else is used, a conditional statement is NOT added.


Download ppt "ENG 1181 College of Engineering Engineering Education Innovation Center MAT – Conditional Statements Topics: 1.Conditional statements if-end if-else-end."

Similar presentations


Ads by Google