CHAPTER 4

advertisement
Chapter 4
I)
Decisions and conditions – take one action or another based upon a condition – ie.
Data validation
II)
If Statements
something else – pg. 124 - 125
 Review Question 1 – if true do one thing, else do
A) Visual Basic uses the If…Then…Else statement to make decisions. An Else clause
is optional and specifies the action to be taken if the condition is false. An
If...Then...Else statement must conclude with an End If – use indentation for
readability.
B) Flowcharting If Statements - Flowcharts can help visualize the logic of an
If...Then...Else – pg. 125-126.
III) Conditions
 Review Questions 2, 7
A) The conditions for an If statement can be composed of the relational operators, which
compare items for equality, greater than, or less than. The comparison of numeric
values is based on the quantity of the number, while string comparisons are based on
the ASCII code table – comparison symbols – pg. 127.
B) Comparing Numeric Variables and Constants – algebraic comparison – sign is
taken into account.
C) Comparing Strings
 Review Question 4 – enclosed in quotation
marks, left-most character compared first and from left to right, one character at a
time – ascii code – pg. 129, 130
D) Testing for True or False
 Review Question 8 – Boolean variable name –
pg. 131
E) Comparing the Text Property of Text Boxes
 Review Question 5 – pg. 131
F) Comparing Uppercase and Lowercase Characters – is case sensitive  Review
Question 6 – to check all possiblities – Ucase or Lcase – pg. 132 - can convert a
text value to upper- or lowercase.
G) Compound Conditions
 Review Question 3
1) The And and Or logical operators may be used to combine multiple conditions.
With the And operator, both conditions must be true for the entire condition to
evaluate True. For the Or operator, if either condition or both are true, the entire
condition evaluates as True. When both And and Or are used in a condition, the
And condition is evaluated before the Or condition.
IV) Nested If Statements
 Review Question 9
A) A nested If statement contains an If statement within either the true or false actions of a
previous If statement. Nesting an If statement inside of another requires the use of the
End If clause. An Else clause always applies to the last unmatched If regardless of
indentation – pg. 134.
V)
Using If Statements with Option Buttons and Check Boxes
A) The state of option buttons and check boxes should be tested with If statements in the
event procedure for a command button, rather than coding event procedures for the
option button or check box – check to determine if options are selected.
Decisions and Conditions
29
B) A Simple Sample
1) Checking the value of a check box – pg. 137
2) Checking the Value of an Option Button Group – pg. 138
3) Checking the Values of Multiple Check Boxes – pg. 139
VI) Displaying messages in Message Boxes
 Review Question 10
A) The MsgBox statement can be used to display a message to the person running the
program. The programmer must supply the message and can optionally select an icon
to display and enter a caption for the message box title bar – pg. 139.
1) Message string – appear in the message box; caption of title bar – appear in message
box title bar
2) Selecting the MsgBox Icon  Review Question 11 – pg. 140
3) Displaying a Message String – message string to display may be a string leteral
enclosed in quotes or a string variable; also concatenate several items – pg. 140
VII) Input Validation
 Review Question 12 – careful programmers
check to make sure the values entered are correct before beginning the processing –
GIGO – self protection – better to reject bad data than to try to find bad output and the
reason
A) Checking for Numeric Values – isnumeric – pg. 141 – avoid a run time error
B) Data validation checks the reasonableness or appropriateness of the value in a variable
or property. Since an error will occur if the data placed in a text box are not numeric, a
popular validation tool is the IsNumeric function.
C) Checking for a Range of Values
 Review Question 13 – reasonable value –
pg. 142
D) Checking for a Required Field – make sure it is entered – compare to an empty
string literal – pg. 142
E) Performing Multiple Validations – to validate several input fields
VIII) Calling Event Procedures – handy if the program is to perform a set of instructions
inmore than one location without duplicating the code – call to execute the entire
procedure when needed – example pg. 144.
A) One procedure can call another procedure.
IX) Hands-on programming example – pg. 145-151
X)
30
Debugging Visual Basic Projects
A) A variety of debugging tools are available in Visual Basic. These include breaking
program execution, displaying the current contents of variables, and stepping through
code. These tools can help find and eliminate logic and run-time errors; can also help
follow the logic of existing projects to better understand how they work.
1) Pausing Execution with the Break Button
2) Forcing a Break – want to stop at a particular spot to see what happens –
breakpoint – to set the breakpoint – place the cursor, place cursor in gray margin
on left and click – line highlighted in red with a large red dot – pg. 153
3) Using the Immediate Window – is available at design time, run time, and break
time – display the values of data or messages while the project is executing – can
see contents of a variable – from view menu
Chapter 4
4) Checking the Current Values of Expressions
 Review Question 15 –
can quickly check – select - tool tip when cursor held over it
B) Stepping through Code
 Review Question 14
1) The best way to debug a program is to thoroughly understand what the project is
doing every step of the way – trace execution line by line
(a) Two stepping commands – debug menu – execute a single line at a time and
display the code window with current statement highlighted
(a) Step into – does go to called programs
(b) Step over – does not go to called programs - skips
XI)
Debugging Step-by-Step Tutorial – pg. 155
Decisions and Conditions
31
Download