Chapter 6 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do...

18
Chapter 6 Looping Structures

Transcript of Chapter 6 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do...

Page 1: Chapter 6 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.

Chapter 6

Looping Structures

Page 2: Chapter 6 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.

Do…Loop Statement

• Can operate statements repetitivelyDo

intx=intx + 1

Loop While intx < 10– The Loop While operates the statement at least

once

Do While intx < 10

intx=intx + 1

Loop

Page 3: Chapter 6 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.

Infinite Loops

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

Page 4: Chapter 6 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.

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 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.

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 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.

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 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.

For…Next Statement

• Looping structure that performs a set number of times

• Operates until a counter reaches an ending value

Page 8: Chapter 6 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.

String Class

• The string data type is a class containing multiple properties

• String class properties:

Page 9: Chapter 6 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.

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.

0 1 2 3 4 5

S U M M E R

Page 10: Chapter 6 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.

String Methods

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

Page 11: Chapter 6 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.

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 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.

String Class Examples

Dim 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 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.

Examples Cont…

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

”x”) ‘xxxxxSummerTime

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

strNewString = strSeason.PadRight(13, “x”)

‘SummerTimexxx

Page 14: Chapter 6 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.

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 Time

intPos = strSeason.IndexOf(“mer”) ‘3

Page 15: Chapter 6 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.

String Concatenation

• Join two or more strings together

• String.concat(string1, string2)

• The & operator also joins strings

• To add spaces use empty quotes orSpace(#)

• The Space function will add a stated number of spaces

• vbTab adds 8 spaces, vbCrLf - returns

Page 16: Chapter 6 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.

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 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.

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 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.

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