04 Conditional Execution Mark Dixon So CCE SOFT

  • Slides: 18
Download presentation
04 – Conditional Execution Mark Dixon, So. CCE SOFT 131 Page 1

04 – Conditional Execution Mark Dixon, So. CCE SOFT 131 Page 1

Admin • Technicians (Babbage 205) can provide you with free copies of (bring your

Admin • Technicians (Babbage 205) can provide you with free copies of (bring your own blank CDs): – MS Windows XP Professional (1 CD), includes • MS Internet Information Services (term 2) – MS Visual Studio 6. 0 (4 CDs), includes • Visual BASIC 6. 0 • Visual Inter. Dev 6. 0 • Visual C++ 6. 0 Mark Dixon, So. CCE SOFT 131 Page 2

Session Aims & Objectives • Aims – to introduce the main concepts involved in

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 Mark Dixon, So. CCE SOFT 131 Page 3

Adaptive Behaviour • So far – every statement always executed in sequence • Often

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. Mark Dixon, So. CCE SOFT 131 Page 4

Decision Trees • Natural language – ambiguous & difficult to follow • Decision trees

Decision Trees • Natural language – ambiguous & difficult to follow • Decision trees – express same information clearly <= 5 miles Delivery Fee > 5 miles Mark Dixon, So. CCE SOFT 131 Free >= £ 10 £ 1. 50 < £ 10 £ 3. 00 Page 5

Conditions & Relational Operators • Conditions – expression, evaluates to: – true (stored as

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 less than is greater than or equal to is less than or equal to is not equal to Mark Dixon, So. CCE SOFT 131 Page 6

Examples: Conditions • Using literals: (34 = 34) (evaluates to true) (34 = 12)

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 controls' properties: pic. Main. Left = 2300 (pic. Main. Left = 2300) (pic. Main. Left = 2309 (pic. Main. Left <> 189 (pic. Main. Left > 1900 Mark Dixon, So. CCE SOFT 131 (true) (false) (true) Page 7

Logical Operators Use to join conditions (assume pic. Main. Top is 23): • And

Logical Operators Use to join conditions (assume pic. Main. Top is 23): • And True when both items are True (pic. Main. Top > 5) AND (pic. Main. Top < 35) (true) (pic. Main. Top < 10) AND (pic. Main. Top > 55) (false) (pic. Main. Top > 6) AND (pic. Main. Top < 23) (false) (pic. Main. Top >=6) AND (pic. Main. Top <= 23) (true) • Or True when either item is True (pic. Main. Top = 23) OR (pic. Main. Top = 11) (pic. Main. Top < 10) OR (pic. Main. Top > 55) (true) (false) • Not True when item is False Not (pic. Main. Top = 23) (false) Mark Dixon, So. CCE SOFT 131 Page 8

Exercise: Conditions • What is the result of (pic. Main. Left is 5589): (pic.

Exercise: Conditions • What is the result of (pic. Main. Left is 5589): (pic. Main. Left > 4400) true • What is the result of (txt. Age. Text is 19, txt. Salary. Text is 10787): (txt. Age. Text < 21) AND (txt. Salary. Text < 10787) false • Write an expression to: check if pic. Main. height is larger than 167. 78 (pic. Main. Height > 167. 78) • Write an expression to: check if pic. Main. Top is larger than pic. Ball. Top (pic. Main. Top > pic. Ball. Top) Mark Dixon, So. CCE SOFT 131 Page 9

If Then statements • Use the following syntax: If condition Then [statementblock] End If

If Then statements • Use the following syntax: If condition Then [statementblock] End If • For example: If txt. Age. Text < 21 Then lbl. Result. Back. Color = vb. Red End If Mark Dixon, So. CCE SOFT 131 Page 10

If Then Else statements • Use the following syntax: If condition 1 Then [statementblock-1]

If Then Else statements • Use the following syntax: If condition 1 Then [statementblock-1] [Else [statementblock-2]] End If • For example: If txt. Age. Text Then lbl. Result. Back. Color = vb. Red Else lbl. Result. Back. Color = vb. Blue End If Mark Dixon, So. CCE SOFT 131 Page 11

Example: Delivery Option Explicit txt. Dist Private Sub btn. Calc_Click() If txt. Dist. Text

Example: Delivery Option Explicit txt. Dist Private Sub btn. Calc_Click() If txt. Dist. Text <= 5 Then lbl. Del. Caption = 0 Else If txt. Cost. Text >= 10 Then lbl. Del. Caption = 1. 5 Else lbl. Del. Caption = 3 End If btn. Calc End If End Sub Mark Dixon, So. CCE SOFT 131 txt. Cost lbl. Delivery Page 12

Example: Tax Calculator v 1 Private Sub btn. Calc_Click() lbl. Taxable. Income. Caption =

Example: Tax Calculator v 1 Private Sub btn. Calc_Click() lbl. Taxable. Income. Caption = "Taxable Income: " & txt. Gross. Salary. Text - txt. A End Sub Salary v 1 Inland Revenue Mark Dixon, So. CCE SOFT 131 Page 13

Example: Tax Calculator v 2 Private Sub btn. Calc_Click() If txt. Gross. Salary. Text

Example: Tax Calculator v 2 Private Sub btn. Calc_Click() If txt. Gross. Salary. Text > txt. Allowance. Text Then lbl. Taxable. Income. Caption = "Taxable Income: £" & txt. Gross. Salary. Text - txt. Allow Else lbl. Taxable. Income. Caption = "Taxable Income: £ 0" End If End Sub Salary v 2 Mark Dixon, So. CCE SOFT 131 Page 14

Example: Tax Calculator v 3 Private Sub btn. Calc_Click() If Val(txt. Gross. Salary. Text)

Example: Tax Calculator v 3 Private Sub btn. Calc_Click() If Val(txt. Gross. Salary. Text) > Val(txt. Allowance. Text) Then lbl. Taxable. Income. Caption = "Taxable Income: £" & txt. Gross. Salary. Text - txt. Allow Else lbl. Taxable. Income. Caption = "Taxable Income: £ 0" End If End Sub Salary v 3 Mark Dixon, So. CCE SOFT 131 Page 15

Example: Ball. Char v 3 tmr. Left tmr. Right Option Explicit Private Sub tmr.

Example: Ball. Char v 3 tmr. Left tmr. Right Option Explicit Private Sub tmr. Left_Timer() ' You need to work this out! End Sub pic. Ball. Char Private Sub tmr. Right_Timer() pic. Ball. Char. Left = pic. Ball. Char. Left + If pic. Ball. Char. Left >= Me. Scale. Width tmr. Right. Enabled = False tmr. Left. Enabled = True End If End Sub Ball. Char v 3 Mark Dixon, So. CCE SOFT 131 Page 16

Selection/Decision controls • Check Box - give user on/off, yes/no choice – Value: •

Selection/Decision controls • Check Box - give user on/off, yes/no choice – Value: • 1 (vb. Checked) if selected • 0 (vb. Unchecked) if not selected • Option Box - Used as a group to give user multiple options (only 1 item selected at a time) – Value: • -1 (True) if selected • 0 (False) if not selected • Can place option boxes in Frame control: – groups option boxes Mark Dixon, So. CCE SOFT 131 Page 17

Example: Face v 2 Private Sub btn. Draw_Click() pic. Face. Cls pic. Face. Circle

Example: Face v 2 Private Sub btn. Draw_Click() pic. Face. Cls pic. Face. Circle (2400, 2400), 2000 If chk. Nose. Value = vb. Checked Then pic. Face. Line (2400, 2200)-Step(0, 600) End If If opt. Open. Value = True Then pic. Face. Circle (1600, 1600), 500 pic. Face. Circle (3200, 1600), 500 Else pic. Face. Line (1100, 1600)Step(1000, 0) pic. Face. Line (2700, 1600)Step(1000, 0) End If Face Mark Dixon, So. CCE If opt. Happy. Value = True Then pic. Face. Circle (2400, 2400), 1200, , 3. 4, 6 Else SOFT 131 Page 18