Upload
jam
View
47
Download
0
Embed Size (px)
DESCRIPTION
String and General Procedures. Answer to the last question of Exam 1. Start. Print The Remain to the left of previous remains. If number Equal to 1 or 0. Divide the Number By 2. No. Get a number. Yes. Is the quotient Equal to 1?. No. Output number. Print 1 - PowerPoint PPT Presentation
Citation preview
String and General ProceduresString and General Procedures
Answer to the last question of Answer to the last question of Exam 1Exam 1
Start
Get a number
Divide the Number
By 2
Is the quotient Equal to 1?
PrintThe Remainto the left of
previous remains
No
Print 1To the left of
Previous remainsEnd
YesOutput number
If numberEqual to 1 or
0
Yes
No
Statistics of Exam 1Statistics of Exam 1
Max: 98Max: 98 Average: 75.18Average: 75.18 Median: 75Median: 75
Midterm grade
0
2
4
6
8
10
12
14
100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 175 180 185 190 195 200 MorePoints
Num
ber
of
studen
ts
.00%
10.00%
20.00%
30.00%
40.00%
50.00%
60.00%
70.00%
80.00%
90.00%
100.00%
FrequencyCumulative %
StringsStrings
Strings are variables that hold letters, Strings are variables that hold letters, words, sentences, or even numberswords, sentences, or even numbers
String holds a bunch of ASCII codes.String holds a bunch of ASCII codes. You can not do mathematical operations on You can not do mathematical operations on
string.string. So you need to use Val(string) function to So you need to use Val(string) function to
convert strings to numbers.convert strings to numbers.
String VariablesString Variables
Declaring StringsDeclaring Strings
Dim Phrase1 As StringDim Phrase1 As String Assigning value to a string variableAssigning value to a string variable
– Phrase1 = “Hello”Phrase1 = “Hello”
Phrase1
H
e
l
l
o
72
101
108
108
111
ASCII code
& Adding Two Strings && Adding Two Strings &
Phrase1 = TextBox1.TextPhrase1 = TextBox1.Text
Phrase2 = TextBox2.TextPhrase2 = TextBox2.Text
Sentence = Phrase1 & Phrase2Sentence = Phrase1 & Phrase2
Adding two strings is called Adding two strings is called ““CONCATENATINGCONCATENATING””
Adding text to a String VariableAdding text to a String Variable
Dim TheAnswer as StringDim TheAnswer as String TheAnswer = “a plague upon society!”TheAnswer = “a plague upon society!”
Sentence= “Boy Bands are ” & “TheAnswer”Sentence= “Boy Bands are ” & “TheAnswer” Sentence= “Boy Bands are ” & TheAnswerSentence= “Boy Bands are ” & TheAnswer
SpacingSpacing
Spacing: Does not insert spaces for youSpacing: Does not insert spaces for you– ““This is” & “not right” This is” & “not right”
This isnot rightThis isnot right– ““This is ” & “right” This is ” & “right”
This is rightThis is right Space itself has an ASCII code, Space itself has an ASCII code, which is 32 (10 base), or 00100000 which is 32 (10 base), or 00100000 (binary)(binary)
String RelationshipsString Relationships
String can be compared.String can be compared. StringA = StringB : comparing if two string StringA = StringB : comparing if two string
is identical.is identical. StringA <> StringB : different fromStringA <> StringB : different from
– ““Hello” <> “hello” is true.Hello” <> “hello” is true. StringA < StringB : if the string A will StringA < StringB : if the string A will
appear earlier in a dictionary than B.appear earlier in a dictionary than B. >, <=, >=>, <=, >=
String FunctionsString Functions
Functions are built in processes that do Functions are built in processes that do something to a variable or piece of something to a variable or piece of informationinformation
Functions have Functions have ARGUMENTSARGUMENTS, or values , or values that you put into themthat you put into them
ARGUMENTS go inside parenthesesARGUMENTS go inside parentheses Left(“fanatic”, 3) Left(“fanatic”, 3) Left(fanatic, 3)Left(fanatic, 3)
String FunctionsString Functions
Len(string) – finds lengthLen(string) – finds length Left(string, n) takes left most Left(string, n) takes left most nn characterscharacters Right(string, n) does same from rightRight(string, n) does same from right Ucase(string) makes string UPPERCASEUcase(string) makes string UPPERCASE Trim(string) – takes spaces off endsTrim(string) – takes spaces off ends Mid(string, start, length) – takes a string from Mid(string, start, length) – takes a string from
middle (great for dividing strings up into pieces)middle (great for dividing strings up into pieces) InStr(string, substring) searches for substring in InStr(string, substring) searches for substring in
stringstring
Mid StringMid String
DNA=“ACGTGACACTGAC”DNA=“ACGTGACACTGAC” Base1=Mid(DNA, 1, 1)Base1=Mid(DNA, 1, 1) “A” “A” Base2=Mid(DNA, 2, Base2=Mid(DNA, 2, 44)) “CGTG” “CGTG”
..
..
.. Base6=Mid(DNA, 6, Base6=Mid(DNA, 6, 22)) “AC” “AC”
InStrInStr()() – In String – In String
DNA=“ACGTGACACTGAC”DNA=“ACGTGACACTGAC” Location=InStr(DNA, “CACTG”)Location=InStr(DNA, “CACTG”)
Returns location of first character of substringReturns location of first character of substring
Working with Text StringsWorking with Text Strings
Comparison = means “Comparison = means “identical toidentical to”” ““yes” is not equal to “Yes”yes” is not equal to “Yes” ““yes “ is not equal to “yes”yes “ is not equal to “yes” Prepare strings for comparisonPrepare strings for comparison
– answer = Ucase(answer) answer = Ucase(answer) – makes it all caps– makes it all caps– answer = Trim(answer) answer = Trim(answer) – takes off spaces– takes off spaces
Then, compare to all caps!Then, compare to all caps! – if answer = “YES” thenif answer = “YES” then
Numeric FunctionsNumeric Functions
answer = Sqr(varNum)answer = Sqr(varNum)
answer = Int(varNum)answer = Int(varNum) – – truncatestruncates (cuts (cuts off decimal stuff) and makes integer off decimal stuff) and makes integer Int(3.7) Int(3.7) 3 3
Int(-2.34) Int(-2.34) -2 -2
answer= Round(varNum)answer= Round(varNum) – rounds number – rounds number
Round(3.7)Round(3.7) 4 4
ProceduresProcedures
String and numeric functions belong to a String and numeric functions belong to a VB category called “procedure”VB category called “procedure”
Procedures are pre-defined code fragments.Procedures are pre-defined code fragments. There are three types of procedures.There are three types of procedures.
– Event procedure (event handler)Event procedure (event handler)– General procedureGeneral procedure– Function procedureFunction procedure
Event ProcedureEvent Procedure
Event procedure is also called event handler.Event procedure is also called event handler. It is associated with an event of the object.It is associated with an event of the object. The name of a event procedure is specially The name of a event procedure is specially
structured.structured.Private Sub ObjectName_EventName()Private Sub ObjectName_EventName()
……
End SubEnd Sub Underscore
General ProceduresGeneral Procedures
Defined by key word Defined by key word Sub Sub Private Sub ProcedureName()Private Sub ProcedureName()
Block of codeBlock of code
End SubEnd Sub Not linked with any eventNot linked with any event Can be called by other part of code.Can be called by other part of code.
– Call ProcedureName()Call ProcedureName()
Why using general procedures?Why using general procedures?
Making the code clear and well structured.Making the code clear and well structured.– Rule of coding #1: Don’t write a procedure that Rule of coding #1: Don’t write a procedure that
is more than 50 lines long.is more than 50 lines long. Distinguishing different function blocks.Distinguishing different function blocks. Reusing the repetitive code Reusing the repetitive code
Passing arguments to a procedurePassing arguments to a procedure
AnalogyAnalogy– Inputs for a computer is as :Inputs for a computer is as :– Arguments for a procedure.Arguments for a procedure.
DefinationDefinationPrivate Sub ProcedureName(arg1 as type, Private Sub ProcedureName(arg1 as type, arg2 as type)arg2 as type)Block of codeBlock of code
End SubEnd Sub In the code section that calls itIn the code section that calls it
Call ProcedureName(arg1, arg2)Call ProcedureName(arg1, arg2)
A computer without a output device?A computer without a output device?
A sub procedure is like a computer that A sub procedure is like a computer that only has input device.only has input device.
Although you can display result on the Although you can display result on the screen, other output devices are definitely screen, other output devices are definitely helpful.helpful.
Therefore we need another kind of Therefore we need another kind of procedure --- function procedure.procedure --- function procedure.
Functions procedureFunctions procedure
Function is a sub procedure with a returned value.Function is a sub procedure with a returned value. Function procedure is used in the same way as a Function procedure is used in the same way as a
built-in numeric or string function.built-in numeric or string function.Variable = FunctionName(arguments…)Variable = FunctionName(arguments…)
Define a functionDefine a functionPrivate Function FunctionName(arg1 As Private Function FunctionName(arg1 As type1, arg2 As type2,…) As ReturnTypetype1, arg2 As type2,…) As ReturnType
Block of codeBlock of codeFunctionName=expressionFunctionName=expressionEnd FunctionEnd Function
HomeworkHomework
Read book page 97-110Read book page 97-110 Lots of useful information in the comments Lots of useful information in the comments
section of the book.section of the book. Today’s office hours are rescheduled to Today’s office hours are rescheduled to
Wednesday 1:30-3:30 Wednesday 1:30-3:30 only for this weekonly for this week..