programming language 2 (pl2) - pbworksbasilissachin.pbworks.com/w/file/fetch/69509891/337.3 apply...
TRANSCRIPT
337.3.1 – Illustrate the logical flow of program in sequence, selection and iteration structure
337.3.2 – Apply selection and repetitive structure
Repetition structure (or loop): a structure that repeatedly processes one or more program instructions until a condition is met
Pretest loop
The condition is evaluated before the instructions within the loop are processed
The instructions may be processed zero or more times
Posttest loop
The condition is evaluated after the instructions within the loop are processed
The instructions are always processed at least once
Do...Loop statement: codes both a pretest or posttest loop
Use While or Until to code the condition for the loop
Repetition symbol in a flowchart is the diamond
Figure 6-6: Examples showing that the pretest and posttest loops do not always produce the same results
Counter: a numeric variable used for counting
Accumulator: a numeric variable used for accumulating (adding together)
Initializing: assigning a beginning value to a counter or accumulator variable
Updating (or incrementing): adding a number to the value of a counter or accumulator variable
Counters are always incremented by a constant value, usually 1
Function: a predefined procedure that performs a specific task and returns a value
InputBox function: displays a predefined dialog box that allows the user to enter data
Contains a text message, an OK button, a Cancel button, and an input area
InputBox function returns: The user’s entry if the user clicks the OK button
An empty string if the user clicks the Cancel button or the Close button on the title bar
• InputBox function parameters:
– prompt: the message to display inside the dialog box
– title: the text to display in the dialog box’s title bar
– defaultResponse: a prefilled value for the user input
• Requirements: display the average amount the
company sold during the prior year
• Input: the amount of each salesperson’s sales
• Priming read: used to obtain the first input
• Must verify that a variable does not contain the
value 0 before using it as a divisor
List box: displays a list of choices from which the user can select zero or more choices
SelectionMode property: controls the number of choices a user can select
None: user can scroll but not select anything
One: user can select one item
MultiSimple and MultiExtended: user can select multiple items
Items collection: a collection of the items in a list box
Collection: a group of one or more individual objects treated as one unit
Add method: adds an item to the list box’s Items collection
Items to be added must be converted to String
Load event of a form: occurs when an application is started and the form is displayed for the first time
Sorted property: Determines if the list box items are sorted
Sort order is dictionary order
Figure 6-15: Items added to the animalListBox and codeListBox
Index: A unique number that identifies an item in a collection
Is zero-relative: the first item has index of 0
Items.Count property: stores the number of items in a list box
Count value is always one higher than the highest index in the list box
SelectedItem property: Contains the value of the selected item in the list
If nothing is selected, it contains the empty string
SelectedIndex property: Contains the index of the selected item in the list
If nothing is selected, it contains the value -1
Default list box item: the item that is selected by default when the interface first appears
• SelectedValueChanged and SelectedIndexChanged events:
– Occur when a user selects an item in a list box
– Occur when a code statement selects an item in a list box
Figure 6-23: Result of processing the SelectedValueChanged and SelectedIndexChanged event procedures
• Allows the user to enter a product ID
• Searches for the ID in a list box
• If found, highlights the ID
Figure 6-25: Sample run of the application when an ID is found
Figure 6-26: Sample run of the application when an ID is not found
For...Next statement: processes a set of instructions a known number of times
Is a pretest loop
Counter variable: used to keep track of the number of times the loop has been processed
Startvalue, endvalue, and stepvalue items Control the number of times the loop is processed
Must evaluate to numeric values
Can be positive or negative
A negative stepvalue causes the loop counter to count down
Flowchart symbol for the For...Next loop is a hexagon
Values for the counter variable, startvalue, stepvalue, and endvalue are shown within the hexagon
Financial.Pmt method Calculates a periodic payment on a loan or investment
Returns the periodic payment as a Double type value
Rate and number of periods arguments must be expressed in the same units (monthly, annual, etc.)
Also calculates the amount that must be saved each period to accumulate a specific sum
Windows standard: highlight the existing text when a text box receives the focus
SelectAll method: selects all text in a text box
Enter event: occurs when the text box receives the focus
Figure 7-9: The principalTextBox’s Enter event procedure
Figure 7-10: Result of processing the Enter event procedure
TextChanged event Occurs when a change is made in a control’s Text property
Change may be made by user or the program
Figure 7-11: New principal entered in the Principal text box
Figure 7-13: Status of the paymentsLabel after entering a value in the text box
Figure 7-12: The principalTextBox’s TextChanged event procedure
Combo Box control: Similar to a list box with a list of choices
May contain a text field that allows the user to type an entry that is not on the list
List portion may be hidden
Three styles of combo boxes: Simple
DropDown (the default)
DropDownList
Items.Add method: used to add items to a combo box
SelectedItem property: contains the value of the selected item in the list
Text property: contains the value that appears in the text portion of the control (item selected or typed in)
Items.count property: used to obtain the number of items in the combo box
Sorted property: used to sort the items in the list
Most applications need to manipulate string data in some fashion String properties and methods are used to manipulate string data
TrimStart method: removes one or more characters from the beginning of a string
TrimEnd method: removes one or more characters from the end of a string
Trim method: removes one or more characters from both the beginning and end of a string
Each of these methods returns a string with the appropriate characters removed trimChars argument:
Comma-separated list of characters to be removed If omitted, spaces will be removed Default value is the space character: “ ”
Remove method: Removes characters from a string
Can remove one or more characters located anywhere in the string
Returns a string with the appropriate characters removed
startIndex argument: The position of a character in a string
Is zero-relative (starts with 0 as first position)
count argument: number of characters to remove
Replace method: replaces a sequence of characters in a string with another sequence of characters
Arguments: string: the original String value
oldValue: sequence of characters you want to replace
newValue: replacement characters
Mid statement: Replaces a specified number of characters in a string with characters from another string
Arguments: targetString: the string in which characters are to be replaced
replacementString: the replacement characters
start: the starting position for the replacement
count: number of characters to replace
Character position of characters in the string starts with 1 (not the same as index)
Count argument is optional
PadLeft method: inserts characters at the beginning of a string
PadRight method: inserts characters at the end of a string
Arguments: length: represents the total length of the desired resulting string
character: the character used to pad the string; default value is the space character
string: the string on which the operation takes place
• Insert method: used to insert characters anywhere within a string
• Arguments:
– startIndex: specifies where in the string to insert the value
– string: the string being manipulated
– value: the character(s) to be inserted
StartsWith method: determines whether a specific sequence of characters occurs at the beginning of a string
EndsWith method: determines whether a specific sequence of characters occurs at the end of a string
Arguments: subString: sequence of characters to be searched for
StartsWith and EndsWith methods both return Boolean values and perform a case-sensitive search
Contains method: Determines if a string contains a specific sequence of characters
Is a method of the String class
Returns a Boolean value
Performs a case-sensitive search
Arguments: subString: represents the sequence of characters to be searched for
string: the string in which to search
IndexOf method: returns an integer representing the location of a substring within a string
Performs a case-sensitive search
Arguments: subString: sequence of characters to be searched for
string: the string to be searched
startIndex: the starting position for the search (zero-relative)
Substring method: accesses any number of characters contained in a string
Arguments: startIndex: index of the first character to be accessed (zero-relative)
string: the string containing the characters to be accessed
count: number of characters to be accessed
String.Compare method: compares two strings
Arguments: string1, string2: the two strings to be compared
ignoreCase: a Boolean value indicating whether to perform a case-sensitive (False) or case-insensitive (True) search
String.Compare returns an integer result: Returns 0 when string1 equals string2
Returns 1 when string1 > string2
Returns -1 when string1 < string2
String.Compare uses word sort rules: Numbers are less than lowercase letters
Lowercase letters are less than uppercase letters
Like operator: Uses pattern-matching characters to determine if one string is equal to another
Returns a Boolean value (True/False)
Arguments: pattern: contains one or more pattern-matching characters
string: the string to be evaluated
charList: a listing of characters to be matched
Figure 6-29: User interface
• Creating the Roll ‘Em Game
• Sleep method: delays execution of the program
• Refresh method: redraws the user interface