Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class...
-
date post
19-Dec-2015 -
Category
Documents
-
view
218 -
download
0
Transcript of Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class...
![Page 1: Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d295503460f949fdfe5/html5/thumbnails/1.jpg)
Mark Dixon Page 1
05 – Conditional Execution
![Page 2: Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d295503460f949fdfe5/html5/thumbnails/2.jpg)
Mark Dixon Page 2
Admin: Test (next week)• In class test
– teaching week 6
• 50 minutes
• short answer (5 - 6 words max)
• 25% of coursework mark
![Page 3: Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d295503460f949fdfe5/html5/thumbnails/3.jpg)
Mark Dixon Page 3
Questions: Data Typesa) What is the result of:
Mid("George Boole", 5, 4)
b) What is put in lblRes?txtPetName.value = "George"lblRes.innerText = Right(txtPetName.value, 3)
c) What is put in lblRes?txtPetName.value = "George"lblRes.innerText = Right("txtPetName", 3)
ge B
rge
ame
![Page 4: Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d295503460f949fdfe5/html5/thumbnails/4.jpg)
Mark Dixon Page 4
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 and generate conditional expressions– use conditional statements to make your code
more adaptable
![Page 5: Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d295503460f949fdfe5/html5/thumbnails/5.jpg)
Mark Dixon Page 5
Adaptive Behaviour• So far
– every statement always executed in sequence
• Often necessary for software to– change behaviour under different circumstances
![Page 6: Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d295503460f949fdfe5/html5/thumbnails/6.jpg)
Mark Dixon Page 6
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
![Page 7: Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d295503460f949fdfe5/html5/thumbnails/7.jpg)
Mark Dixon Page 7
Example: Multiplication Test v1<html> <head><title>Multiply</title></head> <body> <p>What is 5 times 3?</p> <input id="txtAns" type="text" /><br /> <input id="btnAns" type="button" value="Check" /> <p id="lblComment"></p> </body></html>
<script language="vbscript"> Sub btnAns_OnClick() If txtAns.Value = 15 Then document.bgcolor = "yellow" lblComment.innertext = "Correct, well done!" Else document.bgcolor = "cyan" lblComment.innertext = "Sorry, try again" End If End Sub</script>
![Page 8: Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d295503460f949fdfe5/html5/thumbnails/8.jpg)
Mark Dixon Page 8
Example: Multiplication Test v1
![Page 9: Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d295503460f949fdfe5/html5/thumbnails/9.jpg)
Mark Dixon Page 9
Example: Multiplication Test v1
![Page 10: Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d295503460f949fdfe5/html5/thumbnails/10.jpg)
Mark Dixon Page 10
George Boole• 1815 (Lincoln, UK) – 1864
• Invented Boolean algebra– key concept in computing– boolean datatype:
• 0 false• 1 true
• Condition – expression, evaluates to:– true (stored as –1)
– false (stored as 0)
![Page 11: Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d295503460f949fdfe5/html5/thumbnails/11.jpg)
Mark Dixon Page 11
Conditions: Relational Operators• conditions 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
![Page 12: Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d295503460f949fdfe5/html5/thumbnails/12.jpg)
Mark Dixon Page 12
Conditions: Examples (literal)• Using literals:
34 = 34
34 = 12
34 > 4
18 <= 18
true
false
true
true
![Page 13: Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d295503460f949fdfe5/html5/thumbnails/13.jpg)
Mark Dixon Page 13
Conditions: Examples (symbolic)• Using symbols (controls' properties):
Assume that:picMain.style.pixelLeft is 2300
picMain.style.pixelLeft = 2300
picMain.style.pixelLeft = 2309
picMain.style.pixelLeft <> 189
picMain.style.pixelLeft > 1900
true
false
true
true
![Page 14: Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d295503460f949fdfe5/html5/thumbnails/14.jpg)
Mark Dixon Page 14
Conditions: Errors• Are the following valid:
– 23 > 30
– 66 15
– 23 <
– picBat.style.pixelLeft > 1000
– < picBat.style.pixelTop
missing (relational) operator
missing data
missing data
![Page 15: Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d295503460f949fdfe5/html5/thumbnails/15.jpg)
Mark Dixon Page 15
Questions: Conditions• What is the result of (picMain.style.pixelLeft is 5589):
picMain.style.pixelLeft > 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:the pixelLeft of picMain is larger than 167
• Write an expression to check if:picMain pixelTop is more than picBall pixelTop
true
false
picMain.style.pixelLeft > 167
picMain.style.pixelTop > picBall.style.pixelTop
![Page 16: Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d295503460f949fdfe5/html5/thumbnails/16.jpg)
Mark Dixon Page 16
If Then statements• Use the following syntax (pattern):
If condition Then statementblockEnd If
• For example:If txtAge.value < 18 Then document.bgColor = "Red"End If
![Page 17: Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d295503460f949fdfe5/html5/thumbnails/17.jpg)
Mark Dixon Page 17
If Then Else statements• Use the following syntax (pattern):
If condition Then statementblock-1Else statementblock-2End If
• For example:If txtAge.value < 18 Then document.bgColor = "Red"Else document.bgColor = "Blue"End If
![Page 18: Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d295503460f949fdfe5/html5/thumbnails/18.jpg)
Mark Dixon Page 18
Example: Student Loan<html> <head><title>Student Loan Repayment Calculator</title></head> <body> <center><font size="+2"><b>Student Loan Repayment Calculator</b></font></center> <input id="txtIncome" type="text" /> <input id="btnCalc" type="button" value="Calculate" /> <p id="lblPayment"></p> </body></html>
<script language="vbscript"> Sub btnCalc_OnClick() lblPayment.innertext = (txtIncome.value - 15000) * 0.09 End Sub</script>
![Page 19: Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d295503460f949fdfe5/html5/thumbnails/19.jpg)
Mark Dixon Page 19
Example: Student Loan (v2)<html> <head><title>Student Loan Repayment Calculator</title></head> <body> <center><font size="+2"><b>Student Loan Repayment Calculator</b></font></center> <input id="txtIncome" type="text" /> <input id="btnCalc" type="button" value="Calculate" /> <p id="lblPayment"></p> </body></html>
<script language="vbscript"> 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</script>
![Page 20: Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d295503460f949fdfe5/html5/thumbnails/20.jpg)
Mark Dixon Page 20
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
![Page 21: Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d295503460f949fdfe5/html5/thumbnails/21.jpg)
Mark Dixon Page 21
Example: Ball Char (v2)<html> <head><title>Ball Char</title></head> <body style="background-color: Lime;"> <img id="picBall" src="BallChar.gif" style="position: absolute;" /> </body></html>
<script language="vbscript"> Sub window_OnLoad() window.setInterval "MoveBallRight", 50 End Sub Sub MoveBallRight() picBall.style.pixelLeft = picBall.style.pixelLeft + 5 End Sub</script>
![Page 22: Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d295503460f949fdfe5/html5/thumbnails/22.jpg)
Mark Dixon Page 22
Example: Ball Char (v2.1)<html> <head><title>Ball Char</title></head> <body style="background-color: Lime;"> <img id="picBall" src="BallChar.gif" style="position: absolute;" /> </body></html>
<script language="vbscript"> Sub window_OnLoad() window.setInterval "MoveBallRight", 50 End Sub Sub MoveBallRight() If picBall.style.pixelLeft < document.body.clientWidth Then picBall.style.pixelLeft = picBall.style.pixelLeft + 5 End If End Sub</script>
![Page 23: Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d295503460f949fdfe5/html5/thumbnails/23.jpg)
Mark Dixon Page 23
Example: Ball Char (v2.2)<html> <head><title>Ball Char</title></head> <body style="background-color: Lime;"> <img id="picBall" src="BallChar.gif" style="position: absolute;" /> </body></html>
<script language="vbscript"> Sub window_OnLoad() window.setInterval "MoveBallRight", 50 End Sub
Sub MoveBallRight() If (picBall.style.pixelLeft + picBall.width) < document.body.clientWidth Then picBall.style.pixelLeft = picBall.style.pixelLeft + 5 End If End Sub</script>
![Page 24: Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d295503460f949fdfe5/html5/thumbnails/24.jpg)
Mark Dixon Page 24
Example: Ball Char (v2.3)<html> <head><title>Ball Char</title></head> <body style="background-color: Lime;"> <img id="picBall" src="BallChar.gif" style="position: absolute;" /> </body></html>
<script language="vbscript"> Sub window_OnLoad() window.setInterval "MoveBallRight", 50 End Sub
Sub MoveBallRight() If (picBall.style.pixelLeft + picBall.width + 5) < document.body.clientWidth Then picBall.style.pixelLeft = picBall.style.pixelLeft + 5 End If End Sub</script>
![Page 25: Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d295503460f949fdfe5/html5/thumbnails/25.jpg)
Mark Dixon Page 25
Example: Ball Char (v2.4)<html> <head><title>Ball Char</title></head> <body style="background-color: Lime;"> <img id="picBall" src="BallChar.gif" style="position: absolute;" /> </body></html>
<script language="vbscript"> Sub window_OnLoad() window.setInterval "MoveBallRight", 50 End Sub
Sub MoveBallRight() If (picBall.style.pixelLeft + picBall.width + 5) < document.body.clientWidth Then picBall.style.pixelLeft = picBall.style.pixelLeft + 5 Else window.setInterval "MoveBallLeft", 50 End If End Sub
Sub MoveBallLeft() picBall.style.pixelLeft = picBall.style.pixelLeft - 5 End Sub</script>
![Page 26: Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d295503460f949fdfe5/html5/thumbnails/26.jpg)
Mark Dixon Page 26
Example: Ball Char (v2.5)• Bounce from side to side, with sound:
![Page 27: Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d295503460f949fdfe5/html5/thumbnails/27.jpg)
Mark Dixon Page 27
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.
![Page 28: Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d295503460f949fdfe5/html5/thumbnails/28.jpg)
Mark Dixon Page 28
Decision Trees• Natural language
– ambiguous & difficult to follow
• Decision trees– express same information clearly
distance <= 5 miles
value >= £10
Free
£1.50
£3.00
Y
N Y
N
![Page 29: Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d295503460f949fdfe5/html5/thumbnails/29.jpg)
Mark Dixon Page 29
Example: Pizza Delivery<html> <head><title>Delivery</title></head> <body> <p>Distance: <input type="text" id="txtDist" /><br /> Cost: <input type="text" id="txtCost" /><br /> <input type="button" id="btnCalc" value="Calc" /> </p> <p id="lblCharge"></p> </body></html>
<script language="vbscript"> 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 If End Sub</script>
• Nested If statements– one if
inside another if
![Page 30: Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d295503460f949fdfe5/html5/thumbnails/30.jpg)
Mark Dixon Page 30
If statements: Errors
If txtNum.value > 5 Then If txtNum.value = 4 Then document.bgColor = "green"
End If
If picMan.width > 5 document.bgColor = "red"End If
missing Then keyword
missing End If
![Page 31: Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d295503460f949fdfe5/html5/thumbnails/31.jpg)
Mark Dixon Page 31
Logical Operators
• And True when both items are TruepicMain.vSpace > 5 AND picMain.vSpace < 35 truepicMain.vSpace < 10 AND picMain.vSpace > 55 falsepicMain.vSpace > 6 AND picMain.vSpace < 23 falsepicMain.vSpace >= 6 AND picMain.vSpace <= 23 true
• Or True when either item is TruepicMain.vSpace = 23 OR picMain.vSpace = 11 truepicMain.vSpace < 10 OR picMain.vSpace > 55 false
• Not True when item is FalseNot (picMain.vSpace = 23) false
Use to join conditions (picMain.vSpace is 23):
![Page 32: Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d295503460f949fdfe5/html5/thumbnails/32.jpg)
Mark Dixon Page 32
Tutorial Exercises: Multiplication• LEARNING OBJECTIVE:
use if statement to perform conditional execution
• Task 1: Get the Multiplication v1 and v1.1 examples (from the lecture) working.
• Task 2: Modify your program so that the text box is disabled after the answer is checked
• Task 3: 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
![Page 33: Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d295503460f949fdfe5/html5/thumbnails/33.jpg)
Mark Dixon Page 33
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).
![Page 34: Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d295503460f949fdfe5/html5/thumbnails/34.jpg)
Mark Dixon Page 34
Tutorial Exercises: BallChar• LEARNING OBJECTIVE:
use if statement to perform conditional execution
• Task 1: Get the BallChar example (from the lecture) working.You will need to work out the code for v2.5 – use the previous code for inspiration.
• Task 2: Modify your program so that the Ball Character blinks when the mouse moves over it
• Task 3: Modify your program to play a sound when the ball character is clicked
![Page 35: Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d295503460f949fdfe5/html5/thumbnails/35.jpg)
Mark Dixon Page 35
Tutorial Exercises: Pizza Delivery• LEARNING OBJECTIVE:
use nested if statements to perform conditional execution
• Task 1: Get the Pizza Delivery example (from the lecture) working.