© 2006 Lawrenceville Press Slide 1 Chapter 6 The Post-Test Do…Loop Statement Loop structure that...
-
Upload
reginald-lambert-watts -
Category
Documents
-
view
218 -
download
0
description
Transcript of © 2006 Lawrenceville Press Slide 1 Chapter 6 The Post-Test Do…Loop Statement Loop structure that...
© 2006 Lawrenceville PressSlide 1
Chapter 6Chapter 6The Post-Test Do…Loop The Post-Test Do…Loop
StatementStatement 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 terminating condition is checked after the
body of the loop is executed. The loop below iterates while sum is less than
10:sum = 0;Do
sum += 2Loop While sum < 10
© 2006 Lawrenceville PressSlide 2
Chapter 6Chapter 6Pre-Test Do…LoopPre-Test Do…Loop
Executes only if the condition is initially true. May not iterate at all.
The terminating condition is checked before the body of the loop is executed.
The statementsum = 20Do While sum < 10
sum += 2Loop
does not iterate at all because sum is initially greater than 10.
© 2006 Lawrenceville PressSlide 3
Chapter 6Chapter 6Infinite LoopsInfinite Loops
A loop that continues executing forever Can be caused by syntax or logic errors. For
example:num = -1Do
num -= 1 'num is decreased by 1While num < 0
Some errors result in an overflow causing a run-time error.
© 2006 Lawrenceville PressSlide 4
Chapter 6Chapter 6The InputBox FunctionThe InputBox 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:stringVar = InputBox(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.
© 2006 Lawrenceville PressSlide 5
Chapter 6Chapter 6Accumulator VariablesAccumulator Variables
A variable that is incremented by a varying amount. accumulator = accumulator + value
Often used for keeping a running total. intTotalScore = intTotalScore +
intNewScore intTotalScore += intNewScore
Should be initialized when declared.
© 2006 Lawrenceville PressSlide 6
Chapter 6Chapter 6Assignment OperatorsAssignment Operators
Operator Operation+= addition and then assignment-= subtraction and then assignment*= multiplication and then
assignment/= division and then assignment\= integer division and then
assignment^= exponentiation and then
assignment
© 2006 Lawrenceville PressSlide 7
Chapter 6Chapter 6Using FlagsUsing 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.
© 2006 Lawrenceville PressSlide 8
Chapter 6Chapter 6The For…Next StatementThe 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 10i += num
Next num
© 2006 Lawrenceville PressSlide 9
Chapter 6Chapter 6The String ClassThe 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()
© 2006 Lawrenceville PressSlide 10
Chapter 6Chapter 6String MethodsString Methods
String methods for manipulating a string include:
ToUpper converts a string to all uppercaseToLower converts a string to all lowercase Trim removes spaces from the beginning
and end of a string
TrimEnd removes spaces from the end of a string
TrimStart removes spaces from the beginning of a string
PadLeft(len, char) adds a specified character
to the beginning of a string until the string is
len characters longPadRight(len, char) adds a specified
characterto the end of a string until the string is
lencharacters long
© 2006 Lawrenceville PressSlide 11
Chapter 6Chapter 6String Methods String Methods ((cont.cont.))
String methods for manipulating a substring:Substring(startPos, numOfChars)
returns the substring that is numOfChars in
length and starts at startPosRemove(startPos, numOfChars)
deletes the substring that is numOfChars in
length and starts at startPos Replace(oldString, newString)
exchanges every occurrence of oldString
with newString Insert(startPos, substring)
inserts substring at startPos IndexOf(substring)
returns the first position of substring
© 2006 Lawrenceville PressSlide 12
Chapter 6Chapter 6String ConcatenationString 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"
© 2006 Lawrenceville PressSlide 13
Chapter 6Chapter 6String Concatenation (String Concatenation (cont.cont.))
Dim strSeason As String = "SummerTime"Dim strMessage As String = " is a fun time!"Dim strNewString As String
'SummerTime is a fun time!strNewString = String.Concat(strSeason, strMessage)
Dim strFirstName As String, strLastName As StringDim strFullName As String
strFirstName = "Elaine"strLastName = "Malfas“'Elaine MalfasstrFullName = strFirstName & " " & strLastName
© 2006 Lawrenceville PressSlide 14
Chapter 6Chapter 6Space(), vbTab, vbCrLfSpace(), vbTab, vbCrLf
The Space() function returns a string of spaces. vbTab is a built-in constant that represents 8
spaces. vbCrLf is a built-in constant that represents a
carriage return-linefeed combination.
© 2006 Lawrenceville PressSlide 15
Chapter 6Chapter 6The Char StructureThe Char Structure
A simple form of a class. Char has two shared methods:
ToUpper()ToLower()
The methods must be used with the Char structure:
newLetter = Char.ToUpper(letter1)
© 2006 Lawrenceville PressSlide 16
Chapter 6Chapter 6UnicodeUnicode
A digital code with representations for every character in every language and symbol. Table with some examples : pg. 6-15
Two built-in functions for converting between characters and Unicode:
AscW(char)ChrW(integer)
© 2006 Lawrenceville PressSlide 17
Chapter 6Chapter 6Comparing StringsComparing 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(string1, string2, case-insensitive)
returns 0 if string1 and string2 are the same. A
positive number is returned if string1 is greater
than string2 and a negative number if string1 is
less than string2. case-insensitive should be
true if the case of the strings should not beconsidered.
© 2006 Lawrenceville PressSlide 18
Chapter 6Chapter 6The Like OperatorThe 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