Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mark Dixon, SoCCE SOFT 136Page 1 05 – Conditional Execution.

Similar presentations


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

1 Mark Dixon, SoCCE SOFT 136Page 1 05 – Conditional Execution

2 Mark Dixon, SoCCE SOFT 136Page 2 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

3 Mark Dixon, SoCCE SOFT 136Page 3 Adaptive Behaviour So far –every statement always executed in sequence Often necessary for software to –change behaviour under different circumstances

4 Mark Dixon, SoCCE SOFT 136Page 4 Example: Multiplication Test SPECIFICATION User Requirements –A primary school teacher wants to test the multiplication skills of her children. Software Requirements –Functional: –display a multiplication question –allow the user to type a response –check the response and provide feedback –Non-functional should be interesting, colourful, and easy to use

5 Mark Dixon, SoCCE SOFT 136Page 5 Example: Multiplication Test v1 Option Explicit Private Sub btnCheck_Click() If txtAns.Text = 15 Then lblMessage.Caption = "Correct, well done!" Me.BackColor = vbYellow Else lblMessage.Caption = "Sorry, 5 times 3 is 15" Me.BackColor = vbRed End If End Sub lblMessage btnCheck txtAns

6 Mark Dixon, SoCCE SOFT 136Page 6 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

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

8 Mark Dixon, SoCCE SOFT 136Page 8 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

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

10 Mark Dixon, SoCCE SOFT 136Page 10 Example: Multiplication Test Problem –if user enters non-numeric data, then an error occurs

11 Mark Dixon, SoCCE SOFT 136Page 11 Example: Multiplication Test v2 Option Explicit Private Sub btnCheck_Click() If txtAns.Text = 15 Then lblMessage.Caption = "Correct, well done!" Me.BackColor = vbYellow Else lblMessage.Caption = "Sorry, 5 times 3 is 15" Me.BackColor = vbRed End If End Sub Private Sub txtAns_KeyPress(KeyAscii As Integer) If KeyAscii 57 Then KeyAscii = 0 End If End Sub code of key that was pressed executed every time key is pressed cancels key press

12 Mark Dixon, SoCCE SOFT 136Page 12 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):

13 Mark Dixon, SoCCE SOFT 136Page 13 Questions: 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)

14 Mark Dixon, SoCCE SOFT 136Page 14 Example: Student Loan v1 Option Explicit Private Sub btnCalc_Click() lblResult.Caption = (txtIncome.Text - 15000) * 0.09 End Sub SLC

15 Mark Dixon, SoCCE SOFT 136Page 15 Example: Student Loan v2 Option Explicit Private Sub btnCalc_Click() If txtIncome.Text > 15000 Then lblResult.Caption = "£" & (txtIncome.Text - 15000) * 0.09 Else lblResult.Caption = "You pay nothing (£0.00)!" End If End Sub

16 Mark Dixon, SoCCE SOFT 136Page 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 136Page 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 136Page 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

19 Mark Dixon, SoCCE SOFT 136Page 19 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

20 Mark Dixon, SoCCE SOFT 136Page 20 Example: Pizza 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

21 Mark Dixon, SoCCE SOFT 136Page 21 Tutorial Exercises: Multiply LEARNING OBJECTIVE: use if statement to perform conditional execution Task 1: Get the Multiply v1 and v2 examples (from the lecture) working. Task 2: Modify your program so that the keyboard cursor is put in the text box automatically HINT: use the SetFocus method Task 3: Modify your program so that the text box is disabled after the answer is checked Task 4: Modify your program so that a happy/sad face is drawn depending on whether the answer was correct. Task 5: Modify your program so that it makes a suitable sound when the user gets the answer right/wrong. Sound files are in the resources section of the web-site

22 Mark Dixon, SoCCE SOFT 136Page 22 Tutorial Exercises: Student Loan LEARNING OBJECTIVE: use if statement to perform conditional execution Task 1: Get the Student Loan v1 and v2 examples (from the lecture) working. Task 2: Modify your program so that it calculates and displays monthly income and repayment amounts (as well an annual).

23 Mark Dixon, SoCCE SOFT 136Page 23 Tutorial Exercises: BallChar LEARNING OBJECTIVE: use if statement to perform conditional execution Task 1: Get the BallChar example (from the lecture) working. Task 2: Modify your program so that the Ball Character blinks when it is clicked Task 3: Modify your program to play a sound when the ball character is clicked

24 Mark Dixon, SoCCE SOFT 136Page 24 Tutorial Exercises: Face LEARNING OBJECTIVE: use if statement with selection controls Task 1: Get the Face example (from the lecture) working. Task 2: Modify your program so that the face is coloured in (e.g. eyes white, face pink) – be creative HINT: use the FillColour and FillStyle properties (described in previous lecture) Task 3: Modify your program to use sound (happy sound for a happy face, etc)

25 Mark Dixon, SoCCE SOFT 136Page 25 Tutorial Exercises: Pizza Delivery LEARNING OBJECTIVE: use nested if statements (one inside another) Task 1: Get the Pizza Delivery examples (from the lecture) working. Task 2: Modify your program to calculate the total cost of the order, as well as the delivery cost.

26 Mark Dixon, SoCCE SOFT 136Page 26 Tutorial Exercises: Music Player LEARNING OBJECTIVE: use if statements Task 1: Modify your Music Player (from previous week) to automatically move to the first track (file) in the current folder, when the last track finishes playing. Hint: You will need to use the File List Box’s ListCount property.


Download ppt "Mark Dixon, SoCCE SOFT 136Page 1 05 – Conditional Execution."

Similar presentations


Ads by Google