Mark Dixon, SoCCE SOFT 136Page 1 05 – Conditional Execution.
Post on 19-Dec-2015
235 Views
Preview:
Transcript
Mark Dixon, SoCCE SOFT 136 Page 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
Mark Dixon, SoCCE SOFT 136 Page 3
Adaptive Behaviour• So far
– every statement always executed in sequence
• Often necessary for software to– change behaviour under different circumstances
Mark Dixon, SoCCE SOFT 136 Page 4
Example: Multiplication TestSPECIFICATION
• 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-functionalshould be interesting, colourful, and easy to use
Mark Dixon, SoCCE SOFT 136 Page 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 IfEnd Sub
lblMessage
btnCheck
txtAns
Mark Dixon, SoCCE SOFT 136 Page 6
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 136 Page 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 = vbRedElse lblResult.BackColor = vbBlueEnd If
Mark Dixon, SoCCE SOFT 136 Page 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 less than>= is greater than or equal to<= is less than or equal to<> is not equal to
Mark Dixon, SoCCE SOFT 136 Page 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)
Mark Dixon, SoCCE SOFT 136 Page 10
Example: Multiplication Test• Problem
– if user enters non-numeric data, then an error occurs
Mark Dixon, SoCCE SOFT 136 Page 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 IfEnd Sub
Private Sub txtAns_KeyPress(KeyAscii As Integer) If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0 End IfEnd Sub
code of key thatwas pressed
executed every timekey is pressed
cancels key press
Mark Dixon, SoCCE SOFT 136 Page 12
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 136 Page 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)
Mark Dixon, SoCCE SOFT 136 Page 14
Example: Student Loan v1
Option Explicit
Private Sub btnCalc_Click() lblResult.Caption = (txtIncome.Text - 15000) * 0.09End Sub
SLC
Mark Dixon, SoCCE SOFT 136 Page 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 IfEnd Sub
Mark Dixon, SoCCE SOFT 136 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 136 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 136 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
Mark Dixon, SoCCE SOFT 136 Page 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
Mark Dixon, SoCCE SOFT 136 Page 20
Example: Pizza 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 136 Page 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 automaticallyHINT: 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
Mark Dixon, SoCCE SOFT 136 Page 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).
Mark Dixon, SoCCE SOFT 136 Page 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
Mark Dixon, SoCCE SOFT 136 Page 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 creativeHINT: 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)
Mark Dixon, SoCCE SOFT 136 Page 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.
Mark Dixon, SoCCE SOFT 136 Page 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.
top related