Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mark Dixon, SoCCE SOFT 131Page 1 05 – Conditional Execution.

Similar presentations


Presentation on theme: "Mark Dixon, SoCCE SOFT 131Page 1 05 – Conditional Execution."— Presentation transcript:

1 Mark Dixon, SoCCE SOFT 131Page 1 05 – Conditional Execution

2 Mark Dixon, SoCCE SOFT 131Page 2 Session Aims & Objectives Aims –to introduce the main concepts involved in getting the computer to act differently under different circumstances Objectives, by end of this week’s sessions, you should be able to: –evaluate conditional expressions, and –implement decision trees in code

3 Mark Dixon, SoCCE SOFT 131Page 3 Adaptive Behaviour So far –every statement always executed in sequence Often necessary for software to –change behaviour under different circumstances Example: A Pizza shop provides a delivery service. If the delivery is within five miles of the shop, then no delivery fee is charged. If the cost of the goods is less than £10 then a £3 delivery fee is charged, otherwise a £1.50 delivery fee is charged.

4 Mark Dixon, SoCCE SOFT 131Page 4 Decision Trees Natural language –ambiguous & difficult to follow Decision trees –express same information clearly Delivery Fee <= 5 miles > 5 miles >= £10 < £10 Free £1.50 £3.00

5 Mark Dixon, SoCCE SOFT 131Page 5 Conditions & Relational Operators Conditions – expression, evaluates to: –true(stored as –1) –false(stored as 0) contain relational operators: =is equal to >is greater than =is greater than or equal to is not equal to

6 Mark Dixon, SoCCE SOFT 131Page 6 Examples: Conditions Using literals: 34 = 34(evaluates to true) 34 = 12(evaluates to false) 34 > 4(evaluates to true) 18 <=18(evaluates to true) Using variables: Dim x As Integer x = 23 x = 23(evaluates to true) x <> 12(evaluates to true) x > 5(evaluates to true)

7 Mark Dixon, SoCCE SOFT 131Page 7 Logical Operators AndTrue when both items are True OrTrue when either item is True NotTrue when item is False

8 Mark Dixon, SoCCE SOFT 131Page 8 Examples: Logical Operators (x is 23, from previous slide): x > 5 AND x 55(evaluates to false) x 55(evaluates to false) x > 6 AND x =6 AND x <= 23(evaluates to true) Not (x = 23)(evaluates to false)

9 Mark Dixon, SoCCE SOFT 131Page 9 Exercise 1: Conditions What is the result of (salary is 5589): salary > 4400 What is the result of (age is 19, salary is 10787): age < 21 AND salary < 10787 Write an expression to: check if height is larger than 167.78 Write an expression to: check is the first two letters of surname are "Mr" true false height > 167.78 Left$(surname, 2) = "Mr"

10 Mark Dixon, SoCCE SOFT 131Page 10 If Then statements Use the following syntax: If condition Then [statementblock] End If For example: If age < 21 Then lblResult.BackColor = vbRed End If

11 Mark Dixon, SoCCE SOFT 131Page 11 If Then Else statements Use the following syntax: If condition1 Then [statementblock-1] [Else [statementblock-2]] End If For example: If age < 21 Then lblResult.BackColor = vbRed Else lblResult.BackColor = vbBlue End If

12 Mark Dixon, SoCCE SOFT 131Page 12 Example 1: Delivery Private Sub btnCalc_Click() Dim DeliveryFee As Double If txtDist.Text <= 5 Then DeliveryFee = 0 Else If txtCost.Text >= 10 Then DeliveryFee = 1.5 Else DeliveryFee = 3 End If lblResult.Caption = "Delivery charge: " & DeliveryFee End Sub Delivery

13 Mark Dixon, SoCCE SOFT 131Page 13 Immediate If function  Used in variable assignment  Replaces If... Then statement  Syntax: IIf( expr, truepart, falsepart)  Example: lblResult.BackColor = IIf(age>21, vbRed, vbBlue)

14 Mark Dixon, SoCCE SOFT 131Page 14 Example 2: Tax Calculator v1 Inland Revenue Private Sub btnCalc_Click() lblTaxableIncome.Caption = "Taxable Income: " & txtGrossSalary.Text - txtAllowance.Text End Sub Salary v1

15 Mark Dixon, SoCCE SOFT 131Page 15 Example 3: Tax Calculator v2 Private Sub btnCalc_Click() If txtGrossSalary.Text > txtAllowance.Text Then lblTaxableIncome.Caption = "Taxable Income: £" & txtGrossSalary.Text - txtAllowance.Text Else lblTaxableIncome.Caption = "Taxable Income: £0" End If End Sub Salary v2

16 Mark Dixon, SoCCE SOFT 131Page 16 Example 4: Tax Calculator v3 Private Sub btnCalc_Click() If Val( txtGrossSalary.Text ) > Val( txtAllowance.Text ) Then lblTaxableIncome.Caption = "Taxable Income: £" & txtGrossSalary.Text - txtAllowance.Text Else lblTaxableIncome.Caption = "Taxable Income: £0" End If End Sub Salary v3

17 Mark Dixon, SoCCE SOFT 131Page 17 Example 5: Tax Calculator v4 Private Sub btnCalc_Click() Dim GrossSal As Double Dim TaxAllow As Double Dim TaxIncom As Double GrossSal = Val(txtGrossSalary.Text) TaxAllow = Val(txtAllowance.Text) If GrossSal > TaxAllow Then TaxIncom = GrossSal - TaxAllow Else TaxIncom = 0 End If lblTaxableIncome.Caption = "Taxable Income: £" & TaxIncom End Sub Salary v4 Input Data Process Output Data

18 Mark Dixon, SoCCE SOFT 131Page 18 Check box control Used to give user on/off, yes/no choice Properties –Value 1 (vbChecked) if selected 0 (vbUnchecked) if not selected

19 Mark Dixon, SoCCE SOFT 131Page 19 Option box control Used to give user multiple options –only 1 item selected at a time Properties –Value -1 (True) if selected 0 (False) if not selected Can place in Frame control: –groups option boxes

20 Mark Dixon, SoCCE SOFT 131Page 20 Example 6: Face v2 Private Sub btnDraw_Click() picFace.Cls picFace.Circle (2400, 2400), 2000 If chkNose.Value = vbChecked Then picFace.Line (2400, 2200)-Step(0, 600) End If If optOpen.Value = True Then picFace.Circle (1600, 1600), 500 picFace.Circle (3200, 1600), 500 Else picFace.Line (1100, 1600)-Step(1000, 0) picFace.Line (2700, 1600)-Step(1000, 0) End If If optHappy.Value = True Then picFace.Circle (2400, 2400), 1200,, 3.4, 6 Else picFace.Circle (2400, 4400), 1200,, 0.6, 2.5 End If End Sub Face


Download ppt "Mark Dixon, SoCCE SOFT 131Page 1 05 – Conditional Execution."

Similar presentations


Ads by Google