1 / 18

04 – Conditional Execution

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

Download Presentation

04 – Conditional Execution

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 04 – Conditional Execution

  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. 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. 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. Decision Trees • Natural language • ambiguous & difficult to follow • Decision trees • express same information clearly <= 5 miles Free Delivery Fee >= £10 £1.50 > 5 miles < £10 £3.00

  6. Conditions & Relational Operators • Conditions – expression, evaluates to: • true (stored as –1) • false (stored as 0) • contain relationaloperators:= is equal to> is greater than< is less than>= is greater thanor equal to<= is less thanor equal to<> is not equal to

  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. Logical Operators Use to join conditions (assume picMain.Top is 23): • And True when both itemsare 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 itemis True(picMain.Top = 23) OR (picMain.Top = 11) (true)(picMain.Top < 10) OR (picMain.Top > 55) (false) • Not True whenitemis FalseNot (picMain.Top = 23) (false)

  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. If Then statements • Use the following syntax:IfconditionThen [statementblock]EndIf • For example:If txtAge.Text < 21 Then lblResult.BackColor = vbRed End If

  11. If Then Else statements • Use the following syntax:Ifcondition1Then [statementblock-1] [Else [statementblock-2]]EndIf • For example:If txtAge.Text Then lblResult.BackColor = vbRed Else lblResult.BackColor = vbBlue End If

  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 If End Sub txtDist txtCost btnCalc lblDel Delivery

  13. Example: Tax Calculator v1 Private Sub btnCalc_Click() lblTaxableIncome.Caption = "Taxable Income: " & txtGrossSalary.Text - txtAllowance.Text End Sub Salary v1 Inland Revenue

  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. 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. Example: BallChar v3 tmrRight tmrLeft 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 picBallChar BallChar v3

  17. Selection/Decision controls • 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

  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

More Related