05 Conditional Execution Mark Dixon Page 1 Admin

  • Slides: 35
Download presentation
05 – Conditional Execution Mark Dixon Page 1

05 – Conditional Execution Mark Dixon Page 1

Admin: Test (next week) • In class test – teaching week 6 • 50

Admin: Test (next week) • In class test – teaching week 6 • 50 minutes • short answer (5 - 6 words max) • 25% of coursework mark Mark Dixon Page 2

Questions: Data Types a) What is the result of: Mid("George Boole", 5, 4) b)

Questions: Data Types a) What is the result of: Mid("George Boole", 5, 4) b) What is put in lbl. Res? ge B txt. Pet. Name. value = "George" lbl. Res. inner. Text = Right(txt. Pet. Name. value, 3) rge c) What is put in lbl. Res? txt. Pet. Name. value = "George" lbl. Res. inner. Text = Right("txt. Pet. Name", 3) ame Mark Dixon Page 3

Session Aims & Objectives • Aims – to introduce the main concepts involved in

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 Mark Dixon Page 4

Adaptive Behaviour • So far – every statement always executed in sequence • Often

Adaptive Behaviour • So far – every statement always executed in sequence • Often necessary for software to – change behaviour under different circumstances Mark Dixon Page 5

Example: Multiplication Test SPECIFICATION • User Requirements – A primary school teacher wants to

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 Mark Dixon Page 6

Example: Multiplication Test v 1 <html> <head><title>Multiply</title></head> <body> <p>What is 5 times 3? </p>

Example: Multiplication Test v 1 <html> <head><title>Multiply</title></head> <body> <p>What is 5 times 3? </p> <input id="txt. Ans" type="text" /> <input id="btn. Ans" type="button" value="Check" /> <p id="lbl. Comment"></p> </body> </html> <script language="vbscript"> Sub btn. Ans_On. Click() If txt. Ans. Value = 15 Then document. bgcolor = "yellow" lbl. Comment. innertext = "Correct, well done!" Else document. bgcolor = "cyan" lbl. Comment. innertext = "Sorry, try again" End If End Sub </script> Mark Dixon Page 7

Example: Multiplication Test v 1 Mark Dixon Page 8

Example: Multiplication Test v 1 Mark Dixon Page 8

Example: Multiplication Test v 1 Mark Dixon Page 9

Example: Multiplication Test v 1 Mark Dixon Page 9

George Boole • 1815 (Lincoln, UK) – 1864 • Invented Boolean algebra – key

George Boole • 1815 (Lincoln, UK) – 1864 • Invented Boolean algebra – key concept in computing – boolean datatype: • 0 • 1 false true • Condition – expression, evaluates to: – true – false Mark Dixon (stored as – 1) (stored as 0) Page 10

Conditions: Relational Operators • conditions contain relational operators: Mark Dixon = is equal to

Conditions: Relational Operators • conditions contain relational operators: Mark Dixon = 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 11

Conditions: Examples (literal) • Using literals: 34 = 34 Mark Dixon true 34 =

Conditions: Examples (literal) • Using literals: 34 = 34 Mark Dixon true 34 = 12 false 34 > 4 true 18 <= 18 true Page 12

Conditions: Examples (symbolic) • Using symbols (controls' properties): Assume that: pic. Main. style. pixel.

Conditions: Examples (symbolic) • Using symbols (controls' properties): Assume that: pic. Main. style. pixel. Left is 2300 pic. Main. style. pixel. Left = 2300 true pic. Main. style. pixel. Left = 2309 false pic. Main. style. pixel. Left <> 189 true pic. Main. style. pixel. Left > 1900 true Mark Dixon Page 13

Conditions: Errors • Are the following valid: – 23 > 30 – 66 15

Conditions: Errors • Are the following valid: – 23 > 30 – 66 15 – 23 < missing (relational) operator missing data – pic. Bat. style. pixel. Left > 1000 – < pic. Bat. style. pixel. Top missing data Mark Dixon Page 14

Questions: Conditions • What is the result of (pic. Main. style. pixel. Left is

Questions: Conditions • What is the result of (pic. Main. style. pixel. Left is 5589): pic. Main. style. pixel. Left > 4400 true • What is the result (txt. Age. value is 19, txt. Salary. value is 10787): txt. Age. Value < 21 AND txt. Salary. Value < 10787 false • Write an expression to check if: the pixel. Left of pic. Main is larger than 167 pic. Main. style. pixel. Left > 167 • Write an expression to check if: pic. Main pixel. Top is more than pic. Ball pixel. Top pic. Main. style. pixel. Top > pic. Ball. style. pixel. Top Mark Dixon Page 15

If Then statements • Use the following syntax (pattern): If condition Then statementblock End

If Then statements • Use the following syntax (pattern): If condition Then statementblock End If • For example: If txt. Age. value < 18 Then document. bg. Color = "Red" End If Mark Dixon Page 16

If Then Else statements • Use the following syntax (pattern): If condition Then statementblock-1

If Then Else statements • Use the following syntax (pattern): If condition Then statementblock-1 Else statementblock-2 End If • For example: If txt. Age. value < 18 Then document. bg. Color = "Red" Else document. bg. Color = "Blue" End If Mark Dixon Page 17

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>

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="txt. Income" type="text" /> <input id="btn. Calc" type="button" value="Calculate" /> <p id="lbl. Payment"></p> </body> </html> <script language="vbscript"> Sub btn. Calc_On. Click() lbl. Payment. innertext = (txt. Income. value - 15000) * 0. 09 End Sub </script> Mark Dixon Page 18

Example: Student Loan (v 2) <html> <head><title>Student Loan Repayment Calculator</title></head> <body> <center><font size="+2"><b>Student Loan

Example: Student Loan (v 2) <html> <head><title>Student Loan Repayment Calculator</title></head> <body> <center><font size="+2"><b>Student Loan Repayment Calculator</b></font></center> <input id="txt. Income" type="text" /> <input id="btn. Calc" type="button" value="Calculate" /> <p id="lbl. Payment"></p> </body> </html> <script language="vbscript"> Sub btn. Calc_On. Click() If txt. Income. value > 15000 Then lbl. Payment. innertext = "£" & ((txt. Income. value - 15000) * 0. 09) Else lbl. Payment. innertext = "You pay nothing (£ 0. 00)!" End If End Sub Mark Dixon </script> Page 19

Example: Ball Char • Functional Decomposition • Incremental Development • Get ball char to

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 Mark Dixon Page 20

Example: Ball Char (v 2) <html> <head><title>Ball Char</title></head> <body style="background-color: Lime; "> <img id="pic.

Example: Ball Char (v 2) <html> <head><title>Ball Char</title></head> <body style="background-color: Lime; "> <img id="pic. Ball" src="Ball. Char. gif" style="position: absolute; " /> </body> </html> <script language="vbscript"> Sub window_On. Load() window. set. Interval "Move. Ball. Right", 50 End Sub Move. Ball. Right() pic. Ball. style. pixel. Left = pic. Ball. style. pixel. Left + 5 End Sub </script> Mark Dixon Page 21

Example: Ball Char (v 2. 1) <html> <head><title>Ball Char</title></head> <body style="background-color: Lime; "> <img

Example: Ball Char (v 2. 1) <html> <head><title>Ball Char</title></head> <body style="background-color: Lime; "> <img id="pic. Ball" src="Ball. Char. gif" style="position: absolute; " /> </body> </html> <script language="vbscript"> Sub window_On. Load() window. set. Interval "Move. Ball. Right", 50 End Sub Move. Ball. Right() If pic. Ball. style. pixel. Left < document. body. client. Width Then pic. Ball. style. pixel. Left = pic. Ball. style. pixel. Left + 5 End If End Sub Mark Dixon Page 22

Example: Ball Char (v 2. 2) <html> <head><title>Ball Char</title></head> <body style="background-color: Lime; "> <img

Example: Ball Char (v 2. 2) <html> <head><title>Ball Char</title></head> <body style="background-color: Lime; "> <img id="pic. Ball" src="Ball. Char. gif" style="position: absolute; " /> </body> </html> <script language="vbscript"> Sub window_On. Load() window. set. Interval "Move. Ball. Right", 50 End Sub Move. Ball. Right() If (pic. Ball. style. pixel. Left + pic. Ball. width) < document. body. client. Width Then pic. Ball. style. pixel. Left = pic. Ball. style. pixel. Left + 5 End If End Sub </script> Mark Dixon Page 23

Example: Ball Char (v 2. 3) <html> <head><title>Ball Char</title></head> <body style="background-color: Lime; "> <img

Example: Ball Char (v 2. 3) <html> <head><title>Ball Char</title></head> <body style="background-color: Lime; "> <img id="pic. Ball" src="Ball. Char. gif" style="position: absolute; " /> </body> </html> <script language="vbscript"> Sub window_On. Load() window. set. Interval "Move. Ball. Right", 50 End Sub Move. Ball. Right() If (pic. Ball. style. pixel. Left + pic. Ball. width + 5) < document. body. client. Width Then pic. Ball. style. pixel. Left = pic. Ball. style. pixel. Left + 5 End If End Sub </script> Mark Dixon Page 24

Example: Ball Char (v 2. 4) <html> <head><title>Ball Char</title></head> <body style="background-color: Lime; "> <img

Example: Ball Char (v 2. 4) <html> <head><title>Ball Char</title></head> <body style="background-color: Lime; "> <img id="pic. Ball" src="Ball. Char. gif" style="position: absolute; " /> </body> </html> <script language="vbscript"> Sub window_On. Load() window. set. Interval "Move. Ball. Right", 50 End Sub Move. Ball. Right() If (pic. Ball. style. pixel. Left + pic. Ball. width + 5) < document. body. client. Width Then pic. Ball. style. pixel. Left = pic. Ball. style. pixel. Left + 5 Else window. set. Interval "Move. Ball. Left", 50 End If End Sub Move. Ball. Left() pic. Ball. style. pixel. Left = pic. Ball. style. pixel. Left - 5 End Sub </script> Mark Dixon Page 25

Example: Ball Char (v 2. 5) • Bounce from side to side, with sound:

Example: Ball Char (v 2. 5) • Bounce from side to side, with sound: Mark Dixon Page 26

Example: Pizza Delivery A Pizza shop provides a delivery service. If the delivery is

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. Mark Dixon Page 27

Decision Trees • Natural language – ambiguous & difficult to follow • Decision trees

Decision Trees • Natural language – ambiguous & difficult to follow • Decision trees – express same information clearly Free distance <= 5 miles Y N Mark Dixon value >= £ 10 Y N £ 1. 50 £ 3. 00 Page 28

Example: Pizza Delivery • Nested If statements – one if inside another if Mark

Example: Pizza Delivery • Nested If statements – one if inside another if Mark Dixon <html> <head><title>Delivery</title></head> <body> <p>Distance: <input type="text" id="txt. Dist" /> Cost: <input type="text" id="txt. Cost" /> <input type="button" id="btn. Calc" value="Calc" /> </p> <p id="lbl. Charge"></p> </body> </html> <script language="vbscript"> Sub btn. Calc_On. Click() If txt. Dist. value <= 5 Then lbl. Charge. inner. Text = "Delivery Charge: £ 0. 00" Else If txt. Cost. value >= 10 Then lbl. Charge. inner. Text = "Delivery Charge: £ 1. 50" Else lbl. Charge. inner. Text = "Delivery Charge: £ 3. 00" End If End Sub Page 29 </script>

If statements: Errors If pic. Man. width > 5 document. bg. Color = "red"

If statements: Errors If pic. Man. width > 5 document. bg. Color = "red" End If missing Then keyword If txt. Num. value > 5 Then If txt. Num. value = 4 Then document. bg. Color = "green" End If missing End If Mark Dixon Page 30

Logical Operators Use to join conditions (pic. Main. v. Space is 23): • And

Logical Operators Use to join conditions (pic. Main. v. Space is 23): • And True when both items are True pic. Main. v. Space > 5 AND pic. Main. v. Space < 35 pic. Main. v. Space < 10 AND pic. Main. v. Space > 55 pic. Main. v. Space > 6 AND pic. Main. v. Space < 23 pic. Main. v. Space >= 6 AND pic. Main. v. Space <= 23 true false true • Or True when either item is True pic. Main. v. Space = 23 OR pic. Main. v. Space = 11 pic. Main. v. Space < 10 OR pic. Main. v. Space > 55 true false • Not True when item is False Not (pic. Main. v. Space = 23) false Mark Dixon Page 31

Tutorial Exercises: Multiplication • LEARNING OBJECTIVE: use if statement to perform conditional execution •

Tutorial Exercises: Multiplication • LEARNING OBJECTIVE: use if statement to perform conditional execution • Task 1: Get the Multiplication v 1 and v 1. 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 Mark Dixon Page 32

Tutorial Exercises: Student Loan • LEARNING OBJECTIVE: use if statement to perform conditional execution

Tutorial Exercises: Student Loan • LEARNING OBJECTIVE: use if statement to perform conditional execution • Task 1: Get the Student Loan v 1 and v 2 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 Page 33

Tutorial Exercises: Ball. Char • LEARNING OBJECTIVE: use if statement to perform conditional execution

Tutorial Exercises: Ball. Char • LEARNING OBJECTIVE: use if statement to perform conditional execution • Task 1: Get the Ball. Char example (from the lecture) working. You will need to work out the code for v 2. 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 Mark Dixon Page 34

Tutorial Exercises: Pizza Delivery • LEARNING OBJECTIVE: use nested if statements to perform conditional

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. Mark Dixon Page 35