Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 Mark Dixon, SoCCE SOFT 131Page 1 03 – 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 InterDev 6.0 Visual BASIC 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

5 Mark Dixon, SoCCE SOFT 131Page 5 Example: Puppy Puppy's web page Sub btnGuest_OnClick() document.title = "Puppy (large image)" picFace.src = "FaceLarge.jpg" End Sub Welcome, Puppy's web page.

6 Mark Dixon, SoCCE SOFT 131Page 6 Example: Puppy (version 2) Freya's web page Sub btnFreya_OnClick() If btnFreya.value = "Large" Then picFace.src = "FaceLarge.jpg" btnFreya.value = "Small" Else picFace.src = "Face.jpg" btnFreya.value = "Large" End If End Sub Freya's web page. Animation

7 Mark Dixon, SoCCE SOFT 131Page 7 If Then statements Use the following syntax: If condition Then [statementblock] End If For example: If txtAge.value < 21 Then document.bgColor = "Red" End If

8 Mark Dixon, SoCCE SOFT 131Page 8 If Then Else statements Use the following syntax: If condition1 Then [statementblock-1] [Else [statementblock-2]] End If For example: If txtAge.value Then document.bgColor = "Red" Else document.bgColor = "Blue" End If

9 Mark Dixon, SoCCE SOFT 131Page 9 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

10 Mark Dixon, SoCCE SOFT 131Page 10 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: Assume that picMain.hSpace is 2300 (picMain.hSpace = 2300) (true) (picMain.hSpace = 2309 (false) (picMain.hSpace <> 189 (true) (picMain.hSpace > 1900 (true)

11 Mark Dixon, SoCCE SOFT 131Page 11 Logical Operators AndTrue when both items are True (picMain.vSpace > 5) AND (picMain.vSpace 55) (false) (picMain.vSpace > 6) AND (picMain.vSpace =6) AND (picMain.vSpace <= 23) (true) OrTrue when either item is True (picMain.vSpace = 23) OR (picMain.vSpace = 11) (true) (picMain.vSpace 55) (false) NotTrue when item is False Not (picMain.vSpace = 23) (false) Use to join conditions (assume picMain.vSpace is 23):

12 Mark Dixon, SoCCE SOFT 131Page 12 Exercise: Conditions What is the result of (picMain.hSpace is 5589): (picMain.hSpace > 4400) What is the result (txtAge.value is 19, txtSalary.value is 10787): (txtAge.Value < 21) AND (txtSalary.Value < 10787) Write an expression to check if: picMain.hSpace is larger than 167 Write an expression to check if: picMain.vSpace is larger than picBall.vSpace true false (picMain.hSpace > 167) (picMain.vSpace > picBall.vSpace)

13 Mark Dixon, SoCCE SOFT 131Page 13 Example: Multiply Multiply Sub btnAns_OnClick() If txtAns.Value = 15 Then document.bgcolor = "yellow" lblComment.innertext = "Correct, well done!" sndFanfare.Play Else document.bgcolor = "cyan" lblComment.innertext = "Sorry, try again" sndFart.Play End If End Sub What is 5 times 3?

14 Mark Dixon, SoCCE SOFT 131Page 14 Example: Student Loan Student Loan Repayment Calculator Sub btnCalc_OnClick() lblPayment.innertext = (txtIncome.value - 15000) * 0.09 End Sub Student Loan Repayment Calculator SLC

15 Mark Dixon, SoCCE SOFT 131Page 15 Example: Student Loan (v2) Student Loan Repayment Calculator Sub btnCalc_OnClick() If txtIncome.value > 15000 Then lblPayment.innertext = "£" & (txtIncome.value - 15000) * 0.09 Else lblPayment.innertext = "You pay nothing (£0.00)!" End If End Sub Student Loan Repayment Calculator

16 Mark Dixon, SoCCE SOFT 131Page 16 Example: Student Loan (v3)

17 Mark Dixon, SoCCE SOFT 131Page 17 Example: Ball Char Functional Decomposition Incremental Development Get ball char to bounce horizontally: –get ball char to appear on left of page –get ball char to move right on page (user click) –get ball char to move right on page automatically –get ball char to stop at end –get ball char to change direction

18 Mark Dixon, SoCCE SOFT 131Page 18 Example: Ball Char (v2) Test Sub window_OnLoad () window.SetInterval "MoveBallRight", 50 End Sub Sub MoveBallRight () picBall.hspace = picBall.hspace + 5 End Sub

19 Mark Dixon, SoCCE SOFT 131Page 19 Example: Ball Char (v2.1) Test Sub Window_OnLoad() Window.SetInterval "MoveBallRight", 50 End Sub Sub MoveBallRight() If picBall.hSpace < Document.Body.ClientWidth Then picBall.hSpace = picBall.hSpace + 5 End If End Sub

20 Mark Dixon, SoCCE SOFT 131Page 20 Example: Ball Char (v2.2) Test Sub Window_OnLoad() Window.SetInterval "MoveBallRight", 50 End Sub Sub MoveBallRight() If picBall.hspace < (document.body.clientwidth - picBall.Width) Then picBall.hspace = picBall.hspace + 5 End If End Sub

21 Mark Dixon, SoCCE SOFT 131Page 21 Example: Ball Char (v2.3) Test Sub Window_OnLoad() Window.SetInterval "MoveBallRight", 50 End Sub Sub MoveBallRight() If (picBall.hspace + 5) < (document.body.clientwidth - picBall.Width) Then picBall.hspace = picBall.hspace + 5 End If End Sub

22 Mark Dixon, SoCCE SOFT 131Page 22 Example: Ball Char (v2.4) Test Sub Window_OnLoad() Window.SetInterval "MoveBallRight", 50 End Sub Sub MoveBallRight() If (picBall.hspace + 5) < (document.body.clientwidth - picBall.Width) Then picBall.hspace = picBall.hspace + 5 Else Window.SetInterval "MoveBallLeft", 50 End If End Sub Sub MoveBallLeft () picBall.hspace = picBall.hspace - 5 End Sub >

23 Mark Dixon, SoCCE SOFT 131Page 23 Example: Ball Char (v2.5) Bounce from side to side, with sound:

24 Mark Dixon, SoCCE SOFT 131Page 24 Example: Pizza Delivery 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.

25 Mark Dixon, SoCCE SOFT 131Page 25 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

26 Mark Dixon, SoCCE SOFT 131Page 26 Example: Pizza Delivery Delivery Sub btnCalc_OnClick() If txtDist.Value <= 5 Then lblCharge.InnerText = "Delivery Charge: £0.00" Else If txtCost.Value >= 10 Then lblCharge.InnerText = "Delivery Charge: £1.50" Else lblCharge.InnerText = "Delivery Charge: £3.00" End If End Sub Distance: Cost:

27 Mark Dixon, SoCCE SOFT 131Page 27 Example: Garden (v2) To teach children names of garden animals:


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

Similar presentations


Ads by Google