Top Banner
Mark Dixon, SoCCE SOFT 131 Page 1 04 – Conditional Execution
39

Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 1

04 – Conditional Execution

Page 2: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 2

Questions: Expressionsa) What is the result of:

10 * Int(3.1973265765)

b) How many functions are in the following:

Int(12.472) * Sqr(9)) + 8 / 2

c) How many operators are in the following:

Int(12.472) * Sqr(9)) + 8 / 2

d) Write an expression to:

divide 15 by 3 and multiply the result by 6

30

2

3

(15 / 3) * 6

Page 3: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 3

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

Page 4: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 4

Example: AddNum v1<html> <head> <title></title> <script language="VBScript"> Sub btnAdd_OnClick() lblResult.InnerText = txtNum1.Value + txtNum2.Value End Sub </script> </head> <body> <input type="text" ID=txtNum1> <input type="text" ID=txtNum2> <input type="button" ID=btnAdd value="Add"> <p ID=lblResult> </body></html>

Doesn't work!

Page 5: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 5

Types of Information• Numeric (numbers) 29

(integer/whole) 56.23 (decimal/real)

• String (text) "Hello there!""BOO"

• Pictures (numbers)

• Sound (numbers)

Page 6: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 6

AddNum problem• The + operator works with:

– numbers, and

– text

• Text input boxes store text

23 + 16 39

"23" + "16" "2316"

double quotes enclose text

Page 7: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 7

String Functions CInt("63") convert to integer result is 63

Left("boo",2) left string result is "bo"

Right("hello",3) right string result is "llo"

Mid("hello",2,2) middle string result is "el"

Len("S Smith") length result is 7

Space(5) spaces result is " "

Page 8: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 8

String Expressions

Page 9: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 9

String Expressions & Errors "What is " & txtN1.Value & " times "

"What is twice " txtN1.Value & "?"

"What is 6 minus " & & txtN1.Value & "?"

dataoperator

data data

operator

ERROR! missing data

ERROR! missing operator

Page 10: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 10

Questions: String Expressionsa) What is the result of:

Mid("what is the time?", 3, 4)

b) What is the result of: "23" & "18" + Left("bob",1) + Right("sal",2)

c) Write an expression to:convert "16" to a number

d) Write an expression to:give the first two letters of "Mr John Smith"

"at i"

"2318bal"

CInt("16")

Left("Mr John Smith", 2)

Page 11: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 11

Example: AddNum v2<html> <head> <title></title> <script language="VBScript"> Sub btnAdd_OnClick()

lblResult.InnerText = CInt(txtNum1.Value) + CInt(txtNum2.Value) End Sub </script> </head>

<body> <input type="text" ID=txtNum1> <input type="text" ID=txtNum2> <input type="button" ID=btnAdd value="Add"> <p ID=lblResult> </body></html>

Page 12: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 12

Example: Text Shift<html> <head> <title>Text Shift</title> <script language="VBScript"> Sub Window_OnLoad() parH.innertext = parH.innertext & Space(100) Window.SetInterval "TextShift", 50 End Sub

Sub TextShift() parH.innertext = Mid(parH.innertext, 2) & Left(parH.innerText, 1) End Sub </script> </head>

<body> <p id="parH"> Hello There</p> </body></html>

Page 13: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 13

Adaptive Behaviour• So far

– every statement always executed in sequence

• Often necessary for software to– change behaviour under different circumstances

Page 14: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 14

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 15: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 15

Example: Multiplication Test v1<html> <head> <title>Multiply</title> <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> </head> <body> <p>What is 5 times 3? <input type=text id=txtAns> <p><input id=btnAns type=button value=Check> <p id=lblComment> </body></html>

Page 16: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 16

Example: Multiplication Test v1

Page 17: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 17

Example: Multiplication Test v1

Page 18: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 18

Example: Multiplication Test v1.1<html> <head> <title>Multiply</title> <script language=VBScript> Sub btnAns_OnClick() If txtAns.Value = 15 Then document.bgcolor = "yellow" lblComment.innertext = "Correct, well done!" sndFanfare.Play Else document.bgcolor = "cyan" lblComment.innertext = "Sorry, try again" sndFart.Play End If End Sub </script> </head> <body> <p><embed id=sndFanfare src=Fanfare.wav hidden=true autoplay=false> <embed id=sndFart src=Fart.wav hidden=true autoplay=false> What is 5 times 3? <input type=text id=txtAns> <p><input id=btnAns type=button value=Check> <p id=lblComment> </body></html>

Page 19: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 19

If Then statements• Use the following syntax:

If condition Then statementblockEnd If

• For example:If txtAge.value < 21 Then document.bgColor = "Red"End If

Page 20: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 20

If Then Else statements• Use the following syntax:

If condition1 Then statementblock-1Else statementblock-2End If

• For example:If txtAge.value Then document.bgColor = "Red"Else document.bgColor = "Blue"End If

Page 21: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 21

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

Page 22: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 22

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:Assume that picMain.hSpace is 2300 (picMain.hSpace = 2300) (true) (picMain.hSpace = 2309 (false) (picMain.hSpace <> 189 (true) (picMain.hSpace > 1900 (true)

Page 23: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 23

Logical Operators

• And True when both items are True(picMain.vSpace > 5) AND (picMain.vSpace < 35) (true)(picMain.vSpace < 10) AND (picMain.vSpace > 55) (false)(picMain.vSpace > 6) AND (picMain.vSpace < 23) (false)(picMain.vSpace >=6) AND (picMain.vSpace <= 23) (true)

• Or True when either item is True(picMain.vSpace = 23) OR (picMain.vSpace = 11) (true)(picMain.vSpace < 10) OR (picMain.vSpace > 55) (false)

• Not True when item is FalseNot (picMain.vSpace = 23) (false)

Use to join conditions (assume picMain.vSpace is 23):

Page 24: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 24

Exercise: Conditions• What is the result of (picMain.hSpace is 5589):

(picMain.hSpace > 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:

picMain.hSpace is larger than 167

• Write an expression to check if:

picMain.vSpace is larger than picBall.vSpace

true

false

(picMain.hSpace > 167)

(picMain.vSpace > picBall.vSpace)

Page 25: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 25

Example: Student Loan<html> <head> <title>Student Loan Repayment Calculator</title> <script language=VBScript> Sub btnCalc_OnClick() lblPayment.innertext = (txtIncome.value - 15000) * 0.09 End Sub </script> </head> <body> <center><font size=+2><b>Student Loan Repayment Calculator</b></font></center> <input type=text id=txtIncome> <input type=button id=btnCalc value=Calculate> <p id=lblPayment> </body></html>

SLC

Page 26: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 26

Example: Student Loan (v2)<html> <head> <title>Student Loan Repayment Calculator</title> <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> </head> <body> <center><font size=+2><b>Student Loan Repayment Calculator</b></font></center> <input type=text id=txtIncome> <input type=button id=btnCalc value=Calculate> <p id=lblPayment> </body></html>

Page 27: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 27

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 28: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 28

Example: Ball Char (v2)<html> <head> <title>Test</title> <script language="VBScript"> Sub window_OnLoad() window.SetInterval "MoveBallRight", 50 End Sub Sub MoveBallRight() picBall.hspace = picBall.hspace + 5 End Sub </script> </head> <body bgcolor="#00ff00"> <p><img id=picBall src="BallChar.jpg" hspace=0 vspace=11></p> </body></html>

Page 29: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 29

Example: Ball Char (v2.1)<html> <head> <title>Test</title> <script language="VBScript"> Sub Window_OnLoad() Window.SetInterval "MoveBallRight", 50 End Sub Sub MoveBallRight() If picBall.hSpace < Document.Body.ClientWidth Then picBall.hSpace = picBall.hSpace + 5 End If End Sub </script> </head> <body bgcolor="#00ff00"> <p><IMG id=picBall src="BallChar.jpg" hspace=0 vspace=11></p> </body></html>

Page 30: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 30

Example: Ball Char (v2.2)<html> <head> <title>Test</title> <script language="VBScript"> Sub Window_OnLoad() Window.SetInterval "MoveBallRight", 50 End Sub Sub MoveBallRight() If picBall.hspace < (document.body.clientwidth - picBall.Width) Then picBall.hspace = picBall.hspace + 5 End If End Sub </script> </head> <body bgcolor="#00ff00"> <p><IMG id=picBall src="BallChar.jpg" hspace=0 vspace=11></p> </body></html>

Page 31: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 31

Example: Ball Char (v2.3)<html> <head> <title>Test</title> <script language="VBScript"> Sub Window_OnLoad() Window.SetInterval "MoveBallRight", 50 End Sub Sub MoveBallRight() If (picBall.hspace + 5) < (document.body.clientwidth - picBall.Width) Then picBall.hspace = picBall.hspace + 5 End If End Sub </script> </head> <body bgcolor="#00ff00"> <p><IMG id=picBall src="BallChar.jpg" hspace=0 vspace=11></p> </body></html>

Page 32: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 32

Example: Ball Char (v2.4)<<html> <head> <title>Test</title> <script language="VBScript"> Sub Window_OnLoad() Window.SetInterval "MoveBallRight", 50 End Sub Sub MoveBallRight() If (picBall.hspace + 5) < (document.body.clientwidth - picBall.Width) Then picBall.hspace = picBall.hspace + 5 Else Window.SetInterval "MoveBallLeft", 50 End If End Sub Sub MoveBallLeft() picBall.hspace = picBall.hspace - 5 End Sub </script> </head> <body bgcolor="#00ff00"> <p><IMG id=picBall src="BallChar.jpg" hspace=0 vspace=11></p> </body></html>>

Page 33: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 33

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

Page 34: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 34

Tutorial Exercises: AddNum• LEARNING OBJECTIVE:

use function to convert string (text) to integer (number)

• Task 1: get the addnum examples (v1 and v2) working

Page 35: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 35

Tutorial Exercises: Text Shift• LEARNING OBJECTIVE:

use string manipulation functions

• Task 1: get the Text Shift example (from the lecture) working

Page 36: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 36

Tutorial Exercises: Initials• LEARNING OBJECTIVE:

use string manipulation functions

• Task 1: create a new site with two text boxes (surname and forenames), and a button (initials). When the button is clicked the first character from each text box (i.e. the person's initials) should be displayed.

Page 37: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 37

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 38: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 38

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 39: Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.

Mark Dixon, SoCCE SOFT 131 Page 39

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