Chapter 5 The DoLoop Statement Loop structure that

  • Slides: 17
Download presentation
Chapter 5 The Do…Loop Statement § Loop structure that executes a set of statements

Chapter 5 The Do…Loop Statement § Loop structure that executes a set of statements as long as a condition is true. § The condition is a Boolean expression. § Executes at least once. § The loop below iterates while sum is less than 10: sum = 0; Do sum += 2 Loop While sum < 10 © 2010 Lawrenceville Press

Chapter 5 Alternative Do…Loop § Executes only if the condition is initially true. May

Chapter 5 Alternative Do…Loop § Executes only if the condition is initially true. May not iterate at all. § The statement sum = 20 Do While sum < 10 sum += 2 Loop does not iterate at all because sum is initially greater than 10. © 2010 Lawrenceville Press

Chapter 5 Infinite Loops § A loop that continues executing forever § Can be

Chapter 5 Infinite Loops § A loop that continues executing forever § Can be caused by syntax or logic errors. For example: num = -1 Do num -= 1 While num < 0 'num is decreased by 1 § Some errors result in an overflow causing a run-time error. © 2010 Lawrenceville Press

Chapter 5 The Input. Box. Function § Displays a predefined dialog box that has

Chapter 5 The Input. Box. Function § Displays a predefined dialog box that has a prompt, a text box, and OK and Cancel buttons, and then returns a string. § Used to obtain information from the user. § The function is used as part of an assignment statement: string. Var = Input. Box(prompt, title) § Clicking Cancel or leaving the text box blank returns Nothing. § The Val() function can be used to convert string data to numeric data. © 2010 Lawrenceville Press

Chapter 5 Accumulator Variables § A variable that is incremented by a varying amount.

Chapter 5 Accumulator Variables § A variable that is incremented by a varying amount. § Often used for keeping a running total. § Should be initialized when declared. © 2010 Lawrenceville Press

Chapter 5 Assignment Operators Operator *= /= = ^= Operation multiplication and then assignment

Chapter 5 Assignment Operators Operator *= /= = ^= Operation multiplication and then assignment division and then assignment integer division and then assignment exponentiation and then assignment © 2010 Lawrenceville Press

Chapter 5 Using Flags § A flag, or sentinel, indicates when a loop should

Chapter 5 Using Flags § A flag, or sentinel, indicates when a loop should stop iterating. § Often a constant. § Code is easier to modify when sentinels are constants declared at the beginning of a procedure. © 2010 Lawrenceville Press

Chapter 5 The For…Next Statement § Loop structure that executes a set of statements

Chapter 5 The For…Next Statement § Loop structure that executes a set of statements a fixed number of times. § Uses a counter to control loop iterations. § The keyword Step can optionally be used to change the amount the counter is incremented or decremented. § The loop below executes until num is equal to 10: For num As Integer = 0 To 10 i += num Next num © 2010 Lawrenceville Press

Chapter 5 The String Class § Includes properties and methods. § A String object

Chapter 5 The String Class § Includes properties and methods. § A String object is comprised of a sequence of characters with the first character at index position 0. § String properties include: Chars(index) Length() © 2010 Lawrenceville Press

Chapter 5 String Methods § String methods for manipulating a string include: To. Upperconverts

Chapter 5 String Methods § String methods for manipulating a string include: To. Upperconverts a string to all uppercase To. Lowerconverts a string to all lowercase Trim removes spaces from the beginning and end of a string Trim. Endremoves spaces from the end of a string Trim. Startremoves spaces from the beginning of a string Pad. Left(len, char) adds a specified character to the beginning of a string until the string is len characters long Pad. Right( len, char) adds a specified character to the end of a string until the string is len characters long © 2010 Lawrenceville Press

Chapter 5 String Methods(cont. ) § String methods for manipulating a substring: Substring( start.

Chapter 5 String Methods(cont. ) § String methods for manipulating a substring: Substring( start. Pos, num. Of. Chars) returns the substring that is num. Of. Chars in length and starts at start. Pos Remove(start. Pos, num. Of. Chars) deletes the substring that is num. Of. Chars in length and starts at start. Pos Replace(old. String, new. String) exchanges every occurrence of old. String with new. String Insert(start. Pos, substring) inserts substring at start. Pos Index. Of(substring) returns the first position of substring © 2010 Lawrenceville Press

Chapter 5 String Concatenation § Concatenation is joining two or more strings together. §

Chapter 5 String Concatenation § Concatenation is joining two or more strings together. § The String method Concat() joins two or more strings. It is a shared method and must be used with the String class, not an object of the class: s = String. Concat("this", "and", "that") § The &= operator concatenates a string to an existing string: s = "thisand" s &= "that" § The & operator concatenates strings: s = "this" & "and" & "that" © 2010 Lawrenceville Press

Chapter 5 Space(), vb. Tab, vb. Cr. Lf § The Space() function returns a

Chapter 5 Space(), vb. Tab, vb. Cr. Lf § The Space() function returns a string of spaces. § vb. Tab is a built-in constant that represents 8 spaces. § vb. Cr. Lf is a built-in constant that represents a carriage return-linefeed combination. © 2010 Lawrenceville Press

Chapter 5 The Char Structure § A simple form of a class. § Char

Chapter 5 The Char Structure § A simple form of a class. § Char has two shared methods: To. Upper() To. Lower() The methods must be used with the Char structure: new. Letter = Char. To. Upper(letter 1) © 2010 Lawrenceville Press

Chapter 5 Unicode § A digital code with representations for every character in every

Chapter 5 Unicode § A digital code with representations for every character in every language and symbol. § Two built-in functions for converting between characters and Unicode: Asc. W(char) Chr. W(integer) © 2010 Lawrenceville Press

Chapter 5 Comparing Strings § When relational operators (=, >, <, >=, <>) are

Chapter 5 Comparing Strings § When relational operators (=, >, <, >=, <>) are used to compare strings, their Unicode values determine the relationship between the strings. § The Compare() method is a better choice to alphabetically compare strings: Compare(string 1, string 2, case-insensitive) returns 0 if string 1 and string 2 are the same. A positive number is returned if string 1 is greater than string 2 and a negative number if string 1 is less than string 2. case-insensitive should be true if the case of the strings should not be considered. © 2010 Lawrenceville Press

Chapter 5 The Like Operator § Used to perform a textual comparison between two

Chapter 5 The Like Operator § Used to perform a textual comparison between two strings. § Can be used to perform pattern matching. The pattern can include: ? used in place of any single character * used in place of many characters # used in place of any single number [] used to enclose a list of characters used to indicate a range of characters in a list , used to separate characters in a list © 2010 Lawrenceville Press