Chapter 6 Looping Structures
Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates the statement at least once Do While intx < 10 intx=intx + 1 Loop
Infinite Loops May have problems with loops –Logic error –Overflow error –Infinite Loop
Input Box Pop-up text box that allows the user to input information Has a prompt, text box, OK and cancel button How to display: strx = InputBox(“prompt”, “title”) intx = val(strx) me.lbl.text = intx If text box is blank = nothing
Accumulator Variables Variable that stores an accumulating score Keeps a running total or sum Just like a counter intTotal = intTotal + intScore Keep in mind if you’re adding decimals or integers
Flags or Sentinels Something significant in your program that stops program execution or ends a loop Generally declared as a constant Inputbox(“Enter a positive number (-1 to finish)”)
For…Next Statement Looping structure that performs a set number of times Operates until a counter reaches an ending value
String Class The string data type is a class containing multiple properties String class properties:
String Class Example: Think of a String as a row of numbered boxes. The first boxe’s number is zero and they go up from left to right. Only one letter can be in each box SUMMER
String Methods Methods are procedures within a class.ToUpper.ToLower.Trim.TrimEnd.TrimStart.PadLeft(length,”char”).PadRight(length, “char”)
String Substring These return a portion of the string.Substring(startPos, numOfChars).Remove(startPos, numOfChars).Replace(oldString, newString).Insert(startPos, substring).IndexOf(substring)
String Class Examples Dim strSeason As String = “SummerTime” Dim strNewString As String strNewString = strSeason.ToUpper ‘SUMMERTIME strNewString = strSeason.ToLower ‘summertime strSeason = “ SummerTime “ strNewString = strSeason.Trim ‘SummerTime strNewString = strSeason.TrimEnd ‘ SummerTime strNewString = strSeason.TrimStart ‘SummerTime
Examples Cont… strSeason = “SummerTime” strNewString = strSeason.PadLeft(15, ”x”) ‘xxxxxSummerTime strNewString = strSeason.PadLeft(9, “x”) ‘SummerTime strNewString = strSeason.PadRight(13, “x”) ‘SummerTimexxx
Examples Cont… Dim strSeason As String = “SummerTime” Dim strNewString As String Dim intPos As Integer strNewString = strSeason.Substring(6,4) ‘Time strNewString = strSeason.Remove(0,6) ‘Time strNewString = strSeason.Replace(“Time”, “ is fun!”) ‘Summer is Fun strNewString = strSeason.Insert(6, “ is a fun “) ‘Summer is a fun Time intPos = strSeason.IndexOf(“mer”)‘3
String Concatenation Join two or more strings together String.concat(string1, string2) The & operator also joins strings To add spaces use empty quotes or Space(#) The Space function will add a stated number of spaces vbTab adds 8 spaces, vbCrLf - returns
Char Structure A structure is a simple form of a class Like the class, a structure has properties chr1 = char.ToUpper(chr2) chr3 = char.ToLower(chr2)
Comparing Strings Used to alphabetize a list Compare(string1,string2,Case-insensitive) –Case-insensitive should be true or false True-case is not considered, a, A 0-string1 and string 2 equal 1-string1 is after string2 -1-string1 is before string2
Like Operator The Like is used as a Boolean expression –String1 Like String2 –True if String1 matches the pattern of String2 –False if there is no match –? Used for a single character –* used for many characters –# used for a single number –[] used to enclose a list of characters – used to indicate a range of characters in a character list –, used to separate characters in a character list