240 likes | 253 Vues
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
E N D
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 To the left of Previous remains Yes End
Statistics of Exam 1 • Max: 98 • Average: 75.18 • Median: 75
Strings • Strings are variables that hold letters, words, sentences, or even numbers • String holds a bunch of ASCII codes. • You can not do mathematical operations on string. • So you need to use Val(string) function to convert strings to numbers.
String Variables • Declaring Strings Dim Phrase1 As String • Assigning value to a string variable • Phrase1 = “Hello” ASCII code H 72 Phrase1 e 101 l 108 l 108 o 111
& Adding Two Strings & Phrase1 = TextBox1.Text Phrase2 = TextBox2.Text Sentence = Phrase1 & Phrase2 Adding two strings is called “CONCATENATING”
Adding text to a String Variable • Dim TheAnswer as String • TheAnswer = “a plague upon society!” • Sentence= “Boy Bands are ” & “TheAnswer” • Sentence= “Boy Bands are ” & TheAnswer
Spacing • Spacing: Does not insert spaces for you • “This is” & “not right” This isnot right • “This is ” & “right” This is right • Space itself has an ASCII code, which is 32 (10 base), or 00100000 (binary)
String Relationships • String can be compared. • StringA = StringB : comparing if two string is identical. • StringA <> StringB : different from • “Hello” <> “hello” is true. • StringA < StringB : if the string A will appear earlier in a dictionary than B. • >, <=, >=
String Functions • Functions are built in processes that do something to a variable or piece of information • Functions have ARGUMENTS, or values that you put into them • ARGUMENTS go inside parentheses • Left(“fanatic”, 3) • Left(fanatic, 3)
String Functions • Len(string) – finds length • Left(string, n) takes left most ncharacters • Right(string, n) does same from right • Ucase(string) makes string UPPERCASE • Trim(string) – takes spaces off ends • Mid(string, start, length) – takes a string from middle (great for dividing strings up into pieces) • InStr(string, substring) searches for substring in string
Mid String • DNA=“ACGTGACACTGAC” • Base1=Mid(DNA, 1, 1) “A” • Base2=Mid(DNA, 2, 4) “CGTG” . . . • Base6=Mid(DNA, 6, 2) “AC”
InStr() – In String • DNA=“ACGTGACACTGAC” • Location=InStr(DNA, “CACTG”) Returns location of first character of substring
Working with Text Strings • Comparison = means “identical to” • “yes” is not equal to “Yes” • “yes “ is not equal to “yes” • Prepare strings for comparison • answer = Ucase(answer) – makes it all caps • answer = Trim(answer) – takes off spaces • Then, compare to all caps! • if answer = “YES” then
Numeric Functions answer = Sqr(varNum) answer = Int(varNum) – truncates (cuts off decimal stuff) and makes integer Int(3.7) 3 Int(-2.34) -2 answer= Round(varNum) – rounds number Round(3.7) 4
Procedures • String and numeric functions belong to a VB category called “procedure” • Procedures are pre-defined code fragments. • There are three types of procedures. • Event procedure (event handler) • General procedure • Function procedure
Event Procedure • Event procedure is also called event handler. • It is associated with an event of the object. • The name of a event procedure is specially structured. Private Sub ObjectName_EventName() … End Sub Underscore
General Procedures • Defined by key word Sub Private Sub ProcedureName() Block of code End Sub • Not linked with any event • Can be called by other part of code. • Call ProcedureName()
Why using general procedures? • Making the code clear and well structured. • Rule of coding #1: Don’t write a procedure that is more than 50 lines long. • Distinguishing different function blocks. • Reusing the repetitive code
Passing arguments to a procedure • Analogy • Inputs for a computer is as : • Arguments for a procedure. • Defination Private Sub ProcedureName(arg1 as type, arg2 as type) Block of code End Sub • In the code section that calls it Call ProcedureName(arg1, arg2)
A computer without a output device? • A sub procedure is like a computer that only has input device. • Although you can display result on the screen, other output devices are definitely helpful. • Therefore we need another kind of procedure --- function procedure.
Functions procedure • Function is a sub procedure with a returned value. • Function procedure is used in the same way as a built-in numeric or string function. Variable = FunctionName(arguments…) • Define a function Private Function FunctionName(arg1 As type1, arg2 As type2,…) As ReturnType Block of code FunctionName=expression End Function
Homework • Read book page 97-110 • Lots of useful information in the comments section of the book. • Today’s office hours are rescheduled to Wednesday 1:30-3:30 only for this week.