Download presentation
Presentation is loading. Please wait.
Published byBernice Marion Marshall Modified over 9 years ago
1
Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution
2
Mark Dixon, SoCCE SOFT 131Page 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
3
Mark Dixon, SoCCE SOFT 131Page 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
4
Mark Dixon, SoCCE SOFT 131Page 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.
5
Mark Dixon, SoCCE SOFT 131Page 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
6
Mark Dixon, SoCCE SOFT 131Page 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 greater than or equal to is not equal to
7
Mark Dixon, SoCCE SOFT 131Page 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)
8
Mark Dixon, SoCCE SOFT 131Page 8 Logical Operators AndTrue when both items are True (picMain.Top > 5) AND (picMain.Top 55) (false) (picMain.Top > 6) AND (picMain.Top =6) AND (picMain.Top <= 23) (true) OrTrue when either item is True (picMain.Top = 23) OR (picMain.Top = 11) (true) (picMain.Top 55) (false) NotTrue when item is False Not (picMain.Top = 23) (false) Use to join conditions (assume picMain.Top is 23):
9
Mark Dixon, SoCCE SOFT 131Page 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)
10
Mark Dixon, SoCCE SOFT 131Page 10 If Then statements Use the following syntax: If condition Then [statementblock] End If For example: If txtAge.Text < 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 txtAge.Text Then lblResult.BackColor = vbRed Else lblResult.BackColor = vbBlue End If
12
Mark Dixon, SoCCE SOFT 131Page 12 Example: Delivery Option 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 Sub Delivery lblDelbtnCalc txtDisttxtCost
13
Mark Dixon, SoCCE SOFT 131Page 13 Example: Tax Calculator v1 Inland Revenue Private Sub btnCalc_Click() lblTaxableIncome.Caption = "Taxable Income: " & txtGrossSalary.Text - txtAllowance.Text End Sub Salary v1
14
Mark Dixon, SoCCE SOFT 131Page 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 If End Sub Salary v2
15
Mark Dixon, SoCCE SOFT 131Page 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 If End Sub Salary v3
16
Mark Dixon, SoCCE SOFT 131Page 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 If End Sub BallChar v3 tmrLeft picBallChar tmrRight
17
Mark Dixon, SoCCE SOFT 131Page 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 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 Selection/Decision controls
18
Mark Dixon, SoCCE SOFT 131Page 18 Example: 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.