05 Conditional Execution Mark Dixon 1 Admin Test

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

05 – Conditional Execution Mark Dixon 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) • currently 10% of overall mark Mark Dixon 2

Admin Test • Current • Proposed – Coursework • C 1 25% • C

Admin Test • Current • Proposed – Coursework • C 1 25% • C 2 25% – Test • T 1 10% • T 2 10% – Test • T 1 25% • T 2 25% – Examination 30% - Felt that exam duplicated other assessment - looking to change now – student feedback has influence (but outcome is not certain) - email: no response taken as No Mark Dixon 3

Can't touch this • Student work from previous year: based on work done by

Can't touch this • Student work from previous year: based on work done by Daniel Thornton & David Orrock Mark Dixon 4

Questions • name: event, object, property, event handler, operator, function, expression <script language="vbscript"> Sub

Questions • name: event, object, property, event handler, operator, function, expression <script language="vbscript"> Sub window_on. Load() img. Hammer. style. pos. Left = 500 img. Hammer. style. pos. Top = 100 img. Hammer. style. height = 100 img. Hammer. style. width = 75 snd. Player. src = "Hammer. wav" End Sub img. Hammer_on. Mouse. Over() img. Hammer. style. pos. Left = Rnd() * (document. body. client. Width - img. Hammer. style. pos. Top = Rnd() * (document. body. client. Height - img. Ham End Sub </script> Mark Dixon 5

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 par. Res? ge B txt. Pet. Name. value = "George" par. Res. inner. Text = Right(txt. Pet. Name. value, 3) rge c) What is put in par. Res? txt. Pet. Name. value = "George" par. Res. inner. Text = Right("txt. Pet. Name", 3) ame Mark Dixon 6

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 (If) to make your code more adaptable Mark Dixon 7

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 8

Example: Student Loan <html> <head> <title>Student Loan Repayment Calculator</title> <meta http-equiv="x-ua-compatible" content="IE=10" /> </head>

Example: Student Loan <html> <head> <title>Student Loan Repayment Calculator</title> <meta http-equiv="x-ua-compatible" content="IE=10" /> </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="par. Payment"></p> </body> </html> <script language="vbscript"> Sub btn. Calc_on. Click() par. Payment. inner. Text = (txt. Income. value - 21000) * 0. 09 End Sub Problem: if salary < 21000 gives negative </script> Mark Dixon payment 9

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 10

Example: Multiplication Test v 1 <html> <head> <title>Multiply</title> <meta http-equiv="x-ua-compatible" content="IE=10" /> </head> <body>

Example: Multiplication Test v 1 <html> <head> <title>Multiply</title> <meta http-equiv="x-ua-compatible" content="IE=10" /> </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="par. Comment"></p> </body> </html> <script language="vbscript"> Sub btn. Ans_On. Click() If txt. Ans. value = 15 Then document. bg. Color = "yellow" par. Comment. inner. Text = "Correct, well done!" Else document. bg. Color = "cyan" par. Comment. inner. Text = "Sorry, try again" End If End Sub </script> Mark Dixon 11

Example: Multiplication Test v 1 Mark Dixon 12

Example: Multiplication Test v 1 Mark Dixon 12

Example: Multiplication Test v 1 Mark Dixon 13

Example: Multiplication Test v 1 Mark Dixon 13

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 14

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 15

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: • false • true (stored as 0) (stored as 1 or -1) • Condition – Boolean expression, evaluates to true or false Mark Dixon 16

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 17

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 "hello" > "zoo" false 18

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

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

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. pos. Left > 1000 – < pic. Bat. style. pos. Top missing data Mark Dixon 20

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

Questions: Conditions • What is the result of (pic. Main. style. pos. Left is 5589): pic. Main. style. pos. Left > 4400 true • Write an expression to check if: the pos. Left of pic. Main is larger than 167 pic. Main. style. pos. Left > 167 • Write an expression to check if: pic. Main pos. Top is more than pic. Ball pos. Top pic. Main. style. pos. Top > pic. Ball. style. pos. Top Mark Dixon 21

Example: Student Loan (v 2) <html> <head> <title>Student Loan Repayment Calculator</title> <meta http-equiv="x-ua-compatible" content="IE=10"

Example: Student Loan (v 2) <html> <head> <title>Student Loan Repayment Calculator</title> <meta http-equiv="x-ua-compatible" content="IE=10" /> </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="par. Payment"></p> </body> </html> <script language="vbscript"> Sub btn. Calc_on. Click() If txt. Income. value > 21000 Then par. Payment. inner. Text = "£" & ((txt. Income. value - 21000) * 0. 09) Else par. Payment. inner. Text = "You pay nothing (£ 0. 00)!" End If End Sub </script> Mark Dixon 22

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 23

Example: Ball Char (v 2) • Functional Decomposition • Incremental Development • Get ball

Example: Ball Char (v 2) • Functional Decomposition • Incremental Development • Get ball char to move automatically: – 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 Mark Dixon 24

Example: Ball Char (v 2) <html> <head> <title>Ball Char</title> <meta http-equiv="x-ua-compatible" content="IE=10" /> </head>

Example: Ball Char (v 2) <html> <head> <title>Ball Char</title> <meta http-equiv="x-ua-compatible" content="IE=10" /> </head> <body> <img id="pic. Ball" src="Ball. Char. gif" style="position: absolute; " /> Procedure </body> </html> name Interval (in milliseconds: 1000 = 1 s) <script language="vbscript"> Sub window_on. Load() window. set. Interval "Move. Ball. Right()", 500 pic. Ball. style. pos. Left = 0 pic. Ball. style. pos. Top = 60 End Sub Move. Ball. Right() pic. Ball. style. pos. Left = pic. Ball. style. pos. Left + 5 End Sub Mark </script> Dixon 25

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 26

Example: Ball Char (v 2. 1) <html> <head> <title>Ball Char</title> <meta http-equiv="x-ua-compatible" content="IE=10" />

Example: Ball Char (v 2. 1) <html> <head> <title>Ball Char</title> <meta http-equiv="x-ua-compatible" content="IE=10" /> </head> <body style="background-color: Lime; "> <img id="pic. Ball" src="Ball. Char. jpg" style="position: absolute; " /> </body> </html> <script language="vbscript"> Sub window_on. Load() window. set. Interval "Move. Ball()", 500 pic. Ball. style. pos. Left = 0 pic. Ball. style. pos. Top = 60 End Sub Move. Ball() If pic. Ball. style. pos. Left < document. body. clientwidth Then pic. Ball. style. pos. Left = pic. Ball. style. pos. Left + 5 End If End Sub </script> Mark Dixon 27

Example: Ball Char (v 2. 2) <html> <head> <title>Ball Char</title> <meta http-equiv="x-ua-compatible" content="IE=10" />

Example: Ball Char (v 2. 2) <html> <head> <title>Ball Char</title> <meta http-equiv="x-ua-compatible" content="IE=10" /> </head> <body style="background-color: Lime; "> <img id="pic. Ball" src="Ball. Char. jpg" style="position: absolute; " /> </body> </html> <script language="vbscript"> Sub window_on. Load() window. set. Interval "Move. Ball()", 500 pic. Ball. style. pos. Left = 0 pic. Ball. style. pos. Top = 60 End Sub Move. Ball() If (pic. Ball. style. pos. Left + pic. Ball. width) < document. body. client. Width Then pic. Ball. style. pos. Left = pic. Ball. style. pos. Left + 5 End If End Sub </script> Mark Dixon 28

Example: Ball Char (v 2. 3) <html> <head> <title>Ball Char</title> <meta http-equiv="x-ua-compatible" content="IE=10" />

Example: Ball Char (v 2. 3) <html> <head> <title>Ball Char</title> <meta http-equiv="x-ua-compatible" content="IE=10" /> </head> <body style="background-color: Lime; "> <img id="pic. Ball" src="Ball. Char. jpg" style="position: absolute; " /> </body> </html> <script language="vbscript"> Sub window_on. Load() window. set. Interval "Move. Ball()", 500 pic. Ball. style. pos. Left = 0 pic. Ball. style. pos. Top = 60 End Sub Move. Ball() If (pic. Ball. style. pos. Left + pic. Ball. width + 5) < document. body. clientwidth Then pic. Ball. style. pos. Left = pic. Ball. style. pos. Left + 5 End If End Sub </script> Mark Dixon 29

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

Example: Ball Char (v 2. 4) • Bounce from side to side, with sound: Mark Dixon 30

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 31

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 32

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="par. Charge"></p> </body> </html> <script language="vbscript"> Sub btn. Calc_on. Click() If txt. Dist. value <= 5 Then par. Charge. inner. Text = "Delivery Charge: £ 0. 00" Else If txt. Cost. value >= 10 Then par. Charge. inner. Text = "Delivery Charge: £ 1. 50" Else par. Charge. inner. Text = "Delivery Charge: £ 3. 00" End If 33

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 34

Logical Operators: Errors If pic. Man. width > 5 And < 10 Then document.

Logical Operators: Errors If pic. Man. width > 5 And < 10 Then document. bg. Color = "red" End If missing data If pic. Man. width > 5 And pic. Man. width < 10 Then document. bg. Color = "red" End If Mark Dixon 35

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 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 36

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 37

Tutorial Exercises: Ball Char • LEARNING OBJECTIVE: to assign a value to a object's

Tutorial Exercises: Ball Char • LEARNING OBJECTIVE: to assign a value to a object's property, • using combination of literal values, operators, functions, and identifiers • Task 1: get the ball char (v 2) example working • Task 2: add a button that resets the ball char's horizontal position to 0 • Task 3: add a text box that allows the user to control the speed of the ball character. HINT: Currently, the ball char will always move 5 pixels at a time. • Task 4: add a button that stops the ball char moving. HINT: button should put 0 into the text box • Task 5: add two buttons – one for fast and one for slow • Task 6: add two more buttons – one for fast backwards and one for slow backwards • Task 7: make it bounce off both sides of the client area (screen). • Task 8: hide the speed text box HINT: this should happen when the window loads, using style. visibility • Task 9: make the Ball Character blink when the mouse moves over it • Task 10: play a sound when the ball character is clicked Mark Dixon 38

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 39