Presentation is loading. Please wait.

Presentation is loading. Please wait.

ME 142 Engineering Computation I Condition Statements.

Similar presentations


Presentation on theme: "ME 142 Engineering Computation I Condition Statements."— Presentation transcript:

1 ME 142 Engineering Computation I Condition Statements

2 Key Concepts Relational Operators Logical Operators If-Then-Else Statement Select Case Statement GoTo Statement

3 Relational & Logical Operators

4 Relational Operators  Relational or comparison operators are used in expressions to perform a test.  They are most commonly used in conjunction with an IF statement.

5 Relational Operators  Common relational operators include: <less than <=less than or equal to >greater than >=greater than or equal to <>not equal to =equal to

6 Logical Operators  Logical operators are used in complex expressions to perform a test. Used to check for the validity of multiple conditions being met.  Common logical operators include AND, OR, and NOT:

7 Logical Operators AND returns true if all parts of the expression are true, otherwise returns false OR returns true if any part of the expression is true, otherwise returns false NOT returns true if expression is false, returns false if expression is true

8 If-Then-Else Statement

9  If statements are used to execute a statement or block of statements when a condition is true.  Logical and relational operators are used to form expressions which are evaluated as either true or false.

10 If-Then-Else Statement Syntax for the If statement is as follows: If condition Then [statements…] [ElseIf condition Then] [statements…] [Else] [statements…] End If

11 If-Then-Else Statement  When a condition is true, VBA executes the corresponding block of statements.  Execution of the program is then transferred to the end of the If structure.

12 If-Then-Else Statement  A one line version of the statement is also available for cases where a single statement is to be executed: If condition Then statement

13  Develop a function to give discounts based on the quantity purchased: Less than 25: no discount Greater than/equal to 25:10% discount Greater than/equal to 50:20% discount Greater than/equal to 75:25% discount Example Problem

14 Function Discount(Qty) If Qty >= 25 And Qty < 50 Then Discount = 0.1 ElseIf Qty >= 50 And Qty < 75 Then Discount = 0.2 ElseIf Qty >= 75 Then Discount = 0.25 Else Discount = 0 End If QuantityDiscount 950.25 700.2 450.1 150 Can you develop an alternative IF statement that will yield the same results? Example Problem Soln

15 Select Case

16 Select Case Statement  The Select Case structure is useful for decisions that involve three or more options.  It is best suited to situations where a block of statements is executed on the basis of the value of a single variable.

17 Select Case Statement Syntax for the Select Case statement is as follows: Select Case condition [Case expressionlist1] [statements…] [Case expressionlist2] [statements…] [Case Else] [statements…] End Select

18 Select Case Example Function Discount(Qty) Select Case Qty Case Is >= 75 Discount = 0.25 Case 50 To 74 Discount = 0.2 Case 25 To 49 Discount = 0.1 Case Else Discount = 0 End Select End Function

19 GoTo Statement

20  A GoTo statement offers a straightforward means of changing the program flow. GoTo Label  This statement transfers program control to a new statement, identified by a label.  A label is a text string followed by a colon. ExampleLabel:

21 GoTo Statement  In general avoid the use of GoTo Use it only when you have no other way to perform an action. The only time you really need to use a GoTo statement if for error trapping.

22 Example Problem  Create a truth table for the following expression: (X Z) and X < 3  Use VBA to evaluate the expression above as either True or False, given X = 1, Y=2, and Z=3 (Hint: try using VBA’s Immediate Window along with the Print command).

23 Example Problem Soln  Create a truth table for the following expression: (X Z) and X < 3 X<=YY>ZX<3X Z(X Z) and X < 3 TTTTT TTFTF TFTTT TFFTF FTTTT FTFTF FFTFF FFFFF

24 Example Problem Soln  Use VBA to evaluate the expression above as either True or False, given X = 1, Y=2, and Z=3 (Hint: try using VBA’s Immediate Window along with the Print command).

25 Review Questions

26 I-Clicker Question Relational & Logical Operators Given, A=10 and B=8, what does the following expression return: NOT(A>B) A.True B.False C.None of the Above D.Cannot tell from information given

27 I-Clicker Question If-Then-Else Statement Given, perform=3 and sal=100, what does the following function return: A.0 B.3 C.7 D.100 E.None of the Above Function Bonus(perform, sal) If perform = 1 Then Bonus = sal * 0.1 ElseIf perform = 2 Then Bonus = sal * 0.09 ElseIf perform = 3 Then Bonus = sal * 0.07 Else Bonus = 0 End If End Function

28 Homework Help ‘n Hints


Download ppt "ME 142 Engineering Computation I Condition Statements."

Similar presentations


Ads by Google