Beruflich Dokumente
Kultur Dokumente
3. Check boxes, radio buttons, and textboxes all accept user input.
a) List the differences in the way the three accept input.
b) Give an example of how each should be used in an application.
a) Check boxes accept input similar to radio buttons: they allow the user
to select options; if the check box is clicked, it is activated. The
difference between check boxes and radio buttons is that only one
radio button can be activated at a time, whereas multiple check boxes
can be activated at the same time. Text boxes accept text or numerical
value that is typed into the box by the user.
b) A check box should be used if the programmer wants the user to be
able to select multiple options at the same time; for example, an
ordering menu program. Radio buttons should be used if the user
needs to select options, but doesnt need to select multiple options; for
example, switching between operators in a calculator. Finally, a text
box should be used if the user needs to input text or a numerical value,
like typing in a number into a calculator.
a) The appropriate word for the first blank is even because an even
number divided by 2 gives no remainders. The appropriate word for the
second blank is odd because an odd number divided by 2 will give a
remainder.
b) Select Case number Mod 2
Case 0
MessageBox.Show(Your number is even.)
Case Else
MessageBox.Show(Your number is odd.)
7. List the errors in the statement below and then rewrite the statement
so that it will execute as expected:
If 50 <= numTickets <= 100 And _
Me.radStu.Checked
MessageBox.Show = (Both discounts.)
ElseIf 50 <= numTickets <= 100
MessageBox.Show = (Volume discount.)
ElseIf Me.radStu.Checked
MessageBox.Show = (Student discount.)
Case Else
MessageBox.Show = (No discount.)
EndIf
EndIf
One error is that the programmer forgot to use Then at the end of
each If. Another issue is that the programmer forgot to add a second
quotation mark at the end of the intended message box quote. Finally,
the Case Else should be changed to just Else. The code should look
as follows:
If 50 <= numTickets <= 100 And Me.radStu.Checked Then
MessageBox.Show(Both discounts.)
ElseIf 50 <= numTickets <= 100 Then
MessageBox.Show(Volume Discount.)
ElseIf Me.radStu.Checked Then
MessageBox.Show(Student discount.)
Else
MessageBox.Show (No discount.)
8. Rewrite the following statement so that it does not include the nested
IfThen statement:
If Me.chkValue1.Checked Then
If Me.chkVslue2.Checked Then
MessageBox.Show = (Both Applied.)
End If
End IF
num1 = Val(Me.TextBox1.Text)
num2 = Val(Me.TextBox2.Text)
If num1 > num2 Then
MessageBox.Show("First number is larger")
ElseIf num1 < num2 Then
MessageBox.Show("Second number is larger")
ElseIf num1 = num2 Then
MessageBox.Show("Both numbers are equal")
End If
12. a) List the errors in the statement below and then rewrite the
statement so that it will execute as expected:
Select Case num
Case 2 Or 3, num > 10
MessageBox.Show(1st Case)
Case 20 <= num < 30
MessageBox.Show(2nd Case)
a) End Select is used to end the SelectCase statement, not End Case.
A comma is used to separate the two values, not Or; and the To
keyword is used to include a range of values, not the greater than or
equal to(=>) or less than (<) symbols. Finally, the Is keyword is used
instead of num to give the statement meaning.
End If
End Select
End Sub
End Class
purchase += 1
i) The value of local variables are always retained in memory for the
duration of program execution.
False, the value of a local variable is retained in memory for the
duration of the procedure in which it is declared.
j) A compound Boolean expression uses more than one Boolean
expression to determine whether a condition is true or false.
True
k) In a logical And expression, both operands must be true for the
expression to evaluate to true.
True
l) In a logical expression, Or is evaluated before Not.
False, Not is evaluated before And, and Or is evaluated last
(after Not).
m) Message boxes can only be used in decision statements.
False, they can be used outside of decision statements.
n) Counter variables are useful for keeping track of the number of
times a specific event occurs.
True
o) Sum, as assigned as sum = 1 + 2 + 3, is a counter variable.
False, sum would have to be assigned as sum += 1.
p) Only one check box can be selected at a time.
False, multiple check boxes can be selected at a time. Only one
radio button may be selected at one time, however.
q) A Visual Basic statement must be typed in its entirety on a single
line.
False, you can use an _ at the end of the line to continue writing
the line of code on the next line.