CIS 230, Visual Basic
Ch. 4 Notes, page 1
Objective #1: Use block Ifs to control the flow of logic.
- Conditional statements are used in most programming languages to allow
a program to follow different control paths. That is, your program can respond to user
inputs or arithmetic expressions in different ways when you use a conditional statement.
The If statement is an example of a conditional statement.
- The general form for the If statement is
If (condition) Then
statement(s)
[ElseIf (condition) Then
statement(s)]
[Else
statement(s)]
End If
- Note that there is no space in the keyword, ElseIf, and that there is a
space in the last line, End If.
- In the general form above, brackets indicate that the section is optional.
- Therefore, an If statement could be as simple as the following example:
If (condition) Then
X = 50
End If
The last line, End If, is NOT optional though. This line indicates that the If statement
has ended.
- On the other hand, the following If statement could be used following the general form:
If (condition) Then
X = 50
ElseIf (condition) Then
X = 60
End If
- Another example of a mildly complicated If statement is:
If (condition) Then
X = 50
ElseIf (condition) Then
X = 60
Else
X = 100
End If
- Yet another example of a valid If statement is:
If (condition) Then
X = 50
ElseIf (condition) Then
X = 60
ElseIf (condition) Then
X = 70
ElseIf (condition) Then
X = 80
ElseIf (condition) Then
X = 90
Else
X = 100
End If
That is, you may have as many ElseIf portions as you need but you can only use one (if
any) Else portion. Either way you must end the whole If statement with an End If line.
- The If statements above are called "blocks" because they have
multiple lines of code but yet they each consist of only one executable statement. Also,
it is proper (but not absolutely necessary) to indent the statements under the Then and
Else clauses. This makes it easier for others to understand your code.
- It is possible to write an If statement on one line of code as in the following example:
If (condition) Then X = 50
in which case, no End If line is necessary. In this case you would not consider the If
statement to be a block of code. This should only be done for very simple If statements
that would be easy for fellow programmers to understand.
- The conditions referred to in the general form of the If statement and the examples
above are Boolean expressions. A Boolean expression is one that evaluates
to a Boolean TRUE value or a Boolean FALSE value. An
example of a condition is (Total < 100). If the variable Total is less than 100, then
the Boolean expression evaluates to TRUE. If the variable Total is equal to 100, the
Boolean expression evaluates to FALSE. If Total is greater than 100, then the expression
evaluates to FALSE as well.
- Writing conditions that reflect the logic necessary to make your algorithm work
correctly is the difficult part. Having one symbol misplaced or reversed can easily cause
your If statement to work incorrectly.
- While you may use as many ElseIf clauses as you would like in one If block, the first
condition to evaluate to TRUE will cause the next statement to execute. Basic will then
send the flow of the program to the next statement after the End If. If neither the
initial If condition nor any of the ElseIf conditions evaluate to TRUE then the Else
statement will execute if there is one. The Else statement can be considered a default
statement which evaluates if no other statements execute.
Objective #2: Understand and use nested Ifs.
- If an If statement is entirely contained between the Then and the End If of another If
statement, the statement is considered to be a nested If statement.
- Example:
If (condition) Then
[statement(s)]
If (condition) Then
statement(s)
End If
[statement(s)]
End If
- Be careful to use If statements that can be understood by those who read your code.
Often, the correct logic can be represented many ways If (and If/ElseIf) statements but it
is your responsibility to make sure that your algorithm is intelligible.
Objective #3: Read and create flowcharts indicating the logic in a selection
process.
- Carefully read about the flowcharts in chapter 4 and understand how
they are used to represent the conditional logic. We will not make a lot of formal use of
such flowcharts though in class.
- Diamond-shape symbols in flowcharts refer to conditions and show how the flow of control
can branch one of two ways.
Objective #4: Evaluate conditions using the relational operators.
- Conditions can be more complicated than those used as examples above. One can use
familiar algebraic operators such as +, -, /, and *. One can also use 6 different relational
operators can be used within the Boolean expressions that make up conditions.
Relational operators are sometimes called comparison operators. The 6
relational operators are:
- < less than
- > greater than
- = equal to (in this context the = symbol is
used as a relational operator, not as an assignment operator)
- <> not equal to
- >= greater than or equal to (there is no space between the two
symbols)
- <= less than or equal to
- When you use relational operators, you should make sure that the two operands (variables
or values) that you are comparing have the same data type. For example, the condition (intTotal
< 100) is valid because the variable intTotal is an Integer data type which
can be compared to the whole number 100. Be careful, though not to use a condition like
(txtUserEntry < 100) even if the
user was asked to type a numerical value into the textbox, UserEntry. Since VB treats the
Text property of a text box as Text, VB would make the possible comparison "99"
< 100 which may cause a run-time error.
- When strings are compared as opposed to numerical values (Integers, Singles, etc.), VB
compares the two strings character by character, left to right as a librarian would
compare book titles. For the letters 'A' to 'Z', it is obvious that 'A' would be
considered less than 'Z'. But to determine the correct order of other kinds of legal
keyboard characters such as '*' and '~', VB uses the ASCII codes. ASCII
stands for American Standard Code for Information Interchange and an
ASCII chart can be found in the Appendix of many reference manuals and textbooks. There is
an abbreviated ASCII chart on p. 129 of our textbook. To compare '*' with '~', you need to
look up the numerical ASCII codes for the two characters which are 42 and 126,
respectively. Therefore the condition ('*' < '~') evaluates to TRUE. Note that single
quotes are placed around any single character, which is commonly called a character
literal.
- Note that conditions can be simplified in VB where
If blnFinished = 1 Then....
is equivalent to If blnFinished
Then....
Objective #5: Combine conditions using And and Or.
- Sometimes you need to use compound conditions to represent the necessary logic in an
algorithm. Compound conditions consist of several Boolean expressions, which are joined by
the logical operators, And and Or. The three logical operators that you are likely to use
in conditions are Or, And, and Not. Often these logical operators are referred to as the
Boolean Or, the Boolean And, and the Boolean Not as opposed to the words"or",
"and", and "not" as used in the English language.
- Examples: (X < 100 And Y < 50)
(Total = 4 Or Total = 5)
(Not (Total = 4) Or Total = 5)
- The order of operations among the logical operators is: Not,
And, Or but you can use parentheses to
override this order.
Objective #6: Test the Value property of option buttons and check boxes.
- The Value property of a check box can be used to determine if the user
has placed a checkmark in that check box. The Value property will either be Checked (which
equals the value of 1), Unchecked (which equals the value of 0), or Grayed (which equals
the value of 2).
- By simply using code like: If chkColor.Value = Checked
Then.... you can perform an action if indeed the check box,
chkColor is checkmarked.
- The Value property of an option button can be used to determine if the
user has selected that particular option. The value property for an option button is
either True or False.
- The If statement If optYear.Value = True Then....
can be used to perform an action if the option button optYear is selected.
- Since the Value properties of both check boxes and option buttons are the default
properties of those controls, the If statements given above could be shortened to
If chkColor = Checked Then... and
If optYear = True Then....
Objective #7: Perform validation on numeric fields.
- You should check users' input into text boxes to make sure that the data they enter is
valid. If a user enters a number that is not in the range that you are expecting, it could
cause logic or run-time errors. Making sure that the user enters valid data is called validation
(or error-checking). Since we often use text boxes to obtain user input
and the Text property of a text box is considered as text (a string) by VB, it is a wise
idea to validate this data when you expect the user to enter numeric data.
- If you simply wish to make sure that a user's entry is a valid number, you can use the
IsNumeric function. IsNumeric returns a true if its argument is a valid numerical value
and can be used in an arithmetic expression. For example,
IsNumeric("13") = True whereas
IsNumeric("375-4031") = False
- You can validate users' inputted number to make sure that it falls within a certain
range by using relational operators as in
If
(Val(txtUserInputA) > 10) And (Val(txtUserInputB) <= 100) Then ...
Objective #8: Call event procedures from other procedures.
- You can call an event procedure from within another event procedure
simply by typing the name of the event procedure on a line of code.
Objective #9: Create message boxes to display error conditions.
- A message box is a special VB window that can be displayed to send an
important message to the user that he or she cannot easily ignore. It includes a message
(a string), an optional icon next to that string, a title bar caption at the top of the
window, and a command button(s) that requires the user to click to make the window
disappear.
- The general form of the MsgBox statement which produces a message box is
MsgBox "Message string directed to the
user" [, Buttons/icon] [, "Caption of the title bar"]
- The "Message string directed to the user" is a word or phrase that you want
the user to be sure to read.
- The Buttons/icon are VB constants that you can use to make get the attention of the
user. An example is the OK button which uses the VB constant vbOKOnly.
- The Caption of the title bar is the word or phrase you wish to appear in the title bar
at the top of the window.
Objective #10: Apply the message box constants.
- See the Hands-On programming example in the chapter to use the message box
constants which are listed on p. 140.
Objective #11: Debug projects using breakpoints, stepping program execution,
and displaying intermediate results.
- Follow the "Debugging Step-by-Step Tutorial" on pp. 155+ to practice debugging
programs.
CIS 230 Home Page | Mr. Minich's Education Home Page | Minich.com Web Design