1 / 24

String and General Procedures

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

sonyasmith
Télécharger la présentation

String and General Procedures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. String and General Procedures

  2. 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

  3. Statistics of Exam 1 • Max: 98 • Average: 75.18 • Median: 75

  4. 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.

  5. 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

  6. & Adding Two Strings & Phrase1 = TextBox1.Text Phrase2 = TextBox2.Text Sentence = Phrase1 & Phrase2 Adding two strings is called “CONCATENATING”

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

  8. 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)

  9. 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. • >, <=, >=

  10. 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)

  11. 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

  12. Mid String • DNA=“ACGTGACACTGAC” • Base1=Mid(DNA, 1, 1) “A” • Base2=Mid(DNA, 2, 4) “CGTG” . . . • Base6=Mid(DNA, 6, 2) “AC”

  13. InStr() – In String • DNA=“ACGTGACACTGAC” • Location=InStr(DNA, “CACTG”) Returns location of first character of substring

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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()

  19. 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

  20. 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)

  21. 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.

  22. 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

  23. 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.

More Related