Upload
silas
View
20
Download
0
Embed Size (px)
DESCRIPTION
04 – Information Processing: Expressions, Operators & Functions. Questions: Events. Consider the following code: a) How many unique events does it contain? b) Name the event(s ). Sub btnAns_OnClick() If txtAns.Value = 15 Then document.bgcolor = "yellow" - PowerPoint PPT Presentation
Citation preview
Mark Dixon, SoCCE SOFT 131 Page 1
04 – Information Processing:Expressions, Operators & Functions
Mark Dixon, SoCCE SOFT 131 Page 2
Questions: Events
• Consider the following code:
a) How many unique events does it contain?
b) Name the event(s).
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 IfEnd Sub
1
ClickOnClick
Mark Dixon, SoCCE SOFT 131 Page 3
Questions: Properties
• Consider the following code:
a) How many unique properties does it contain?
b) Name the properties.
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 IfEnd Sub
3
value, bgcolor, & innertext
Mark Dixon, SoCCE SOFT 131 Page 4
Questions: Keywords
• Consider the following code:
a) How many unique keywords does it contain?
b) Name the keywords.
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 IfEnd Sub
5
Sub, If, Then, Else, & End
Mark Dixon, SoCCE SOFT 131 Page 5
Session Aims & Objectives• Aims
– Introduce you to main processing concepts, i.e. expressions, operators and functions
• Objectives,by end of this week’s sessions, you should be able to:
– evaluate expressions– assign a value to a object's property,
• using combination of literal values, operators, functions, and identifiers
Mark Dixon, SoCCE SOFT 131 Page 6
Information Processing• All computing problems:
– involve processing information/data• information has meaning (e.g. 5lb 3.3kg 18 years)• data has no meaning (e.g 5 3.3 18)
– following this pattern:
• For example:– to multiply two numbers: 7 * 9 = 63
Input Data Process Output Data
9
763*
Mark Dixon, SoCCE SOFT 131 Page 7
Information Processing (cont.)• Hence, to solve any computing problem ask:
– what information goes in
– what processing is done to it
– what information comes out
Mark Dixon, SoCCE SOFT 131 Page 8
Example: Multiply<html> <head> <title></title> <script language="VBScript"> Sub btnMultiply_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=btnMultiply value="Multiply"> <p ID=lblResult> </body></html>
Mark Dixon, SoCCE SOFT 131 Page 9
Operators• Sit between the data
5 + 2 addition operator result is 7 5 - 2 subtraction operator result is 3 5 * 2 multiplication operator result is 10 5 / 2 division operator result is 2.5 "5" & "2" string concatenation result is "52"
• work with objects: txtNum1.Value * txtNum2.Value
Mark Dixon, SoCCE SOFT 131 Page 10
• The following assignment statement:lblResult.InnerText = txtNum1.Value * txtNum2.Value
contains an expression
Expressions
• Given: txtNum1.Value = "7", txtNum2.Value = "9"can evaluate expression:lblResult.InnerText = txtNum1.Value * txtNum2.Value
(from above)lblResult.InnerText = "7" * "9" (substitute)lblResult.InnerText = 63 (calculate)Animation
Mark Dixon, SoCCE SOFT 131 Page 11
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>
Mark Dixon, SoCCE SOFT 131 Page 12
Types of Information• Numbers (numeric) 29 (integer/whole) 56.23 (decimal/real)
• Text (string) "Hello there!""BOO"
• Pictures
• Sound
Mark Dixon, SoCCE SOFT 131 Page 13
AddNum problem
• The + operator works with:– numbers, and
– text
• Text input boxes store text
23 + 16 = 39
"23" + "16" = "2316"
Mark Dixon, SoCCE SOFT 131 Page 14
Functions & Operators
• Used to:– process (manipulate) data
• Both Functions & Operators:– take input data/parameters (1 or more item)– process it– return a result
• which replaces the expression (substitution)
Parameter(s) Result
SQR
Function
(16) 4
Mark Dixon, SoCCE SOFT 131 Page 15
Functions (cont.)• Functions: come before the data (which is in
brackets) Sqr(16) square root result is 4 Abs(-23) absolute value result is 23 Int(2.543) integer result is 2 CInt("63") integer convert 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"
Mark Dixon, SoCCE SOFT 131 Page 16
Questions: Expressions• What is the result of:
1 + CInt("23") + Int(2.76786) + Sqr(Int(9.4523))
• What is the result of:
"23" & "18" + Left("bob",1) + Right("sal",2)
• Write an expression to:
give integer value of "16.7658765"
• Write an expression to:
give the first two letters of "Mr John Smith"
1 + 23 + 2 + 3 = 29
"23" & "18" & "b" & "al" = "2318bal"
Int(CInt("16.7658765"))
Left("Mr John Smith", 2)
Mark Dixon, SoCCE SOFT 131 Page 17
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>
Mark Dixon, SoCCE SOFT 131 Page 18
Testing & Debugging: Errors• 3 error types :
– syntax: computer unable to understand your instructions (program does not execute), e.g.
• variable not defined (probably most common)
– run-time: program can't execute instruction and exits (future lecture)
• Device unavailable (e.g. floppy drive)
– logical: program executes but does not not match specification (do what was intended), e.g.
• clicking on button does nothing, when you thought you attached code to it
• clicking on blue button changes colour to red (when it should change to blue)
Mark Dixon, SoCCE SOFT 131 Page 19
<html> <head> <title>Hello</title> <script language="VBScript"> Sub btnHello_OnCluck() lblHello.inerText = "Hello" End Sub </script> </head> <body> <input type=button ID=btnHello Value="Hello"> <p ID=lblHello> </body></html>
• Computer – just symbol matching– No intelligence
Example: Hello Errors
missing nn: syntax error
u instead of i: logical error (button does nothing)
Mark Dixon, SoCCE SOFT 131 Page 20
Questions: Errors
• The following example is from lecture 1,– Spot the errors (you
should find 6), and– decide whether they are
syntax or logical
<html> <head> <title>Hello</title> <script language="VBScript"> Sub btnBlue_OnCluck() document.bgColor = "Red" End Sub Sub btnRed_Onlick() document.bgColor "Red" End Sub Sub window_OnClick() document.bgColour = "White" End Sib </script> </head> <body> <input type=button ID=btnRed Value="Red"> <input type=button ID=btnBlue Value="Blue"> <p ID=lblHello> </body></html>
Mark Dixon, SoCCE SOFT 131 Page 21
Tutorial Exercises
• Task 1: get multiple example working
• Task 2: get addnum examples (v1 and v2) working
• Task 3: create a new page 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.
• Task 4: create a new page that helps the user covert from Pounds to Dollars