Upload
giovanna-abbott
View
21
Download
2
Embed Size (px)
DESCRIPTION
04 – Conditional Execution. 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 InterDev 6.0 - PowerPoint PPT Presentation
Citation preview
Mark Dixon, SoCCE SOFT 131 Page 1
04 – Conditional Execution
Mark Dixon, SoCCE SOFT 131 Page 2
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 InterDev 6.0• Visual C++ 6.0
Mark Dixon, SoCCE SOFT 131 Page 3
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, SoCCE SOFT 131 Page 4
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, SoCCE SOFT 131 Page 5
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
Mark Dixon, SoCCE SOFT 131 Page 6
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, SoCCE SOFT 131 Page 7
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:picMain.Left = 2300 (picMain.Left = 2300) (true) (picMain.Left = 2309 (false) (picMain.Left <> 189 (true) (picMain.Left > 1900 (true)
Mark Dixon, SoCCE SOFT 131 Page 8
Logical Operators
• And True when both items are True(picMain.Top > 5) AND (picMain.Top < 35) (true)(picMain.Top < 10) AND (picMain.Top > 55) (false)(picMain.Top > 6) AND (picMain.Top < 23) (false)(picMain.Top >=6) AND (picMain.Top <= 23) (true)
• Or True when either item is True(picMain.Top = 23) OR (picMain.Top = 11) (true)(picMain.Top < 10) OR (picMain.Top > 55) (false)
• Not True when item is FalseNot (picMain.Top = 23) (false)
Use to join conditions (assume picMain.Top is 23):
Mark Dixon, SoCCE SOFT 131 Page 9
Exercise: Conditions• What is the result of (picMain.Left is 5589):
(picMain.Left > 4400)
• What is the result of (txtAge.Text is 19, txtSalary.Text is 10787):
(txtAge.Text < 21) AND (txtSalary.Text < 10787)
• Write an expression to:
check if picMain.height is larger than 167.78
• Write an expression to:
check if picMain.Top is larger than picBall.Top
true
false
(picMain.Height > 167.78)
(picMain.Top > picBall.Top)
Mark Dixon, SoCCE SOFT 131 Page 10
If Then statements• Use the following syntax:
If condition Then [statementblock]End If
• For example:If txtAge.Text < 21 Then lblResult.BackColor = vbRedEnd If
Mark Dixon, SoCCE SOFT 131 Page 11
If Then Else statements• Use the following syntax:
If condition1 Then [statementblock-1][Else [statementblock-2]]End If
• For example:If txtAge.Text Then lblResult.BackColor = vbRedElse lblResult.BackColor = vbBlueEnd If
Mark Dixon, SoCCE SOFT 131 Page 12
Example: DeliveryOption Explicit
Private Sub btnCalc_Click() If txtDist.Text <= 5 Then lblDel.Caption = 0 Else If txtCost.Text >= 10 Then lblDel.Caption = 1.5 Else lblDel.Caption = 3 End If End IfEnd Sub Delivery
lblDelbtnCalc
txtDist txtCost
Mark Dixon, SoCCE SOFT 131 Page 13
Example: Tax Calculator v1
Inland Revenue
Private Sub btnCalc_Click()
lblTaxableIncome.Caption = "Taxable Income: " & txtGrossSalary.Text - txtAllowance.Text
End Sub
Salary v1
Mark Dixon, SoCCE SOFT 131 Page 14
Example: 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 IfEnd Sub
Salary v2
Mark Dixon, SoCCE SOFT 131 Page 15
Example: 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 IfEnd Sub
Salary v3
Mark Dixon, SoCCE SOFT 131 Page 16
Example: BallChar v3
Option Explicit
Private Sub tmrLeft_Timer() ' You need to work this out!End Sub
Private Sub tmrRight_Timer() picBallChar.Left = picBallChar.Left + 100 If picBallChar.Left >= Me.ScaleWidth Then tmrRight.Enabled = False tmrLeft.Enabled = True End IfEnd Sub
BallChar v3
tmrLeft
picBallChar
tmrRight
Mark Dixon, SoCCE SOFT 131 Page 17
• Check Box - give user on/off, yes/no choice– Value:
• 1 (vbChecked) if selected• 0 (vbUnchecked) if not selected
• Option Box - Used as a group to give usermultiple 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
Selection/Decision controls
Mark Dixon, SoCCE SOFT 131 Page 18
Example: Face v2Private 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 IfEnd Sub
Face