18
Chapter 6 Looping Structures

Chapter 6

  • Upload
    abram

  • View
    17

  • Download
    0

Embed Size (px)

DESCRIPTION

Chapter 6. Looping Structures. Do…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 - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter 6

Chapter 6

Looping Structures

Page 2: Chapter 6

Do…Loop Statement

• Can operate statements repetitivelyDointx=intx + 1Loop While intx < 10

– The Loop While operates the statement at least once

Do While intx < 10intx=intx + 1Loop

Page 3: Chapter 6

Infinite Loops

• May have problems with loops– Logic error– Overflow error– Infinite Loop

Page 4: Chapter 6

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

Page 5: Chapter 6

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

Page 6: Chapter 6

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)”)

Page 7: Chapter 6

For…Next Statement

• Looping structure that performs a set number of times

• Operates until a counter reaches an ending value

Page 8: Chapter 6

String Class

• The string data type is a class containing multiple properties

• String class properties:

Page 9: Chapter 6

String ClassExample:

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

0 1 2 3 4 5S U M M E R

Page 10: Chapter 6

String Methods

• Methods are procedures within a class.ToUpper.ToLower.Trim.TrimEnd.TrimStart.PadLeft(length,”char”).PadRight(length, “char”)

Page 11: Chapter 6

String Substring

• These return a portion of the string.Substring(startPos, numOfChars).Remove(startPos, numOfChars).Replace(oldString, newString).Insert(startPos, substring).IndexOf(substring)

Page 12: Chapter 6

String Class ExamplesDim strSeason As String = “SummerTime”Dim strNewString As String

strNewString = strSeason.ToUpper ‘SUMMERTIMEstrNewString = strSeason.ToLower ‘summertime

strSeason = “ SummerTime “strNewString = strSeason.Trim ‘SummerTimestrNewString = strSeason.TrimEnd ‘

SummerTimestrNewString = strSeason.TrimStart ‘SummerTime

Page 13: Chapter 6

Examples Cont…

strSeason = “SummerTime”strNewString = strSeason.PadLeft(15,

”x”) ‘xxxxxSummerTimestrNewString = strSeason.PadLeft(9, “x”)

‘SummerTimestrNewString = strSeason.PadRight(13,

“x”)‘SummerTimexxx

Page 14: Chapter 6

Examples Cont…Dim strSeason As String = “SummerTime”Dim strNewString As StringDim intPos As Integer

strNewString = strSeason.Substring(6,4) ‘TimestrNewString = strSeason.Remove(0,6) ‘TimestrNewString = strSeason.Replace(“Time”, “ is fun!”)

‘Summer is FunstrNewString = strSeason.Insert(6, “ is a fun “)

‘Summer is a fun TimeintPos = strSeason.IndexOf(“mer”) ‘3

Page 15: Chapter 6

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

Page 16: Chapter 6

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)

Page 17: Chapter 6

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

Page 18: Chapter 6

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