Sie sind auf Seite 1von 4

1

Chapter 5 Critical Thinking Answers


1. The primary purpose of a loop is to execute the same set of statements again and again until
a specific condition is met.

2. A Do While...Loop will evaluate the condition before executing the loop, whereas a Do...Loop
will execute the loop at least once.

3. a) An infinite loop is one which continues forever.


b) The condition of the loop never becomes False, which results in an infinite loop.

4. a) Message box and input box.


b) They both stop the flow of the program, forcing the user to respond to the box.
c) A message box only supplies information. An input box is used to get information from
the user.

5. Dim numberString As String = Me.txtNumStudents.Text


Dim number As Integer
If numberString = Nothing Then 'Empty text box
numberString = InputBox("Enter a value between 1 and 30.")
Else
number = Val(numberString)
Do While number < 1 Or number > 30
numberString = InputBox("Enter a value between 1 and 30.")
If numberString = Nothing Then 'Cancel or empty text box
numberString = InputBox("Enter a value between 1 and 30.")
Else
number = Val(numberString)
End If
Loop
End If

6. Counters and accumulators both modify a variable by updating the variable’s previous value.
The accumulator variable can be used to keep a running total or sum and is incremented in
various ways by a changing value, while the counter variable is used to keep count of how
many times something has happened and is usually incremented by a constant value of 1.
Answers will vary for the uses of each type, but can include: an accumulator variable can be
used in a loop to keep track of a running total or used with multiplication to find a factorial,
as in the Factorial application from the review in the text, and a counter variable can be used
in a Do...Loop statement to keep track of how many scores are entered in an input box or in a
For loop to keep track of an index in an array.

7. A Do...Loop executes until a condition is false, while a For...Next executes a set number of
times.

8. a) Dim num As Integer = 2


Dim total, count As Integer = 0
Dim avg As Double
Do
total += num
count += 1
num += 3
Loop While num < 100
avg = total / count

An Introduction to Programming Using Microsoft Visual Basic 2010


© 2012 EMC Publishing, LLC
2

b) Dim total, count As Integer


Dim avg As Double
For counter As Integer = 2 To 99 Step 3
total += counter
count += 1
Next counter
avg = total / count

9. Integer and Double can only be set to numeric values, whereas String data types can be
comprised of both numeric and text values.

10. String variables are called objects because the String data type is a class.

11. For i As Integer = 0 To number.Length - 1


sum = sum + Val(number.Substring(i, 1))
Next i
MessageBox.Show("The sum of the digits in " & number & " is " & sum)

12. Strings can be combined to create a new string by using the Concat() String class method or
the & operator.

13. Dim numText1 As String = InputBox("Enter a starting number:")


Dim numText2 As String = InputBox("Enter an ending number:")
Dim num1, num2 As Integer
Dim output As String
If numText1 = Nothing Or numText2 = Nothing Then 'Cancel or empty text box
Me.lblPhrase.Text = "Canceled."
Else
'Check for smaller number
If Val(numText1) < Val(numText2) Then
num1 = Val(numText1)
num2 = Val(numText2)
Else
num1 = Val(numText2)
num2 = Val(numText1)
End If
output = "Number" & Space(10) & "Square" & vbCrLf
For num As Integer = num1 To num2
output &= num & Space(20) & num * num & vbCrLf
Next num
End If
Me.lblOutput.Text = output

14. Const FLAG As String = "."


Dim userWord, output As String
userWord = InputBox("Enter a word (or a period to end):")
Do While userWord <> Nothing And Not userWord.equals(FLAG)
output &= userWord & Space(1)
userWord = InputBox("Enter a word (or a period to end):")
Loop
Me.lblOutput.Text = output

15. a) test now
b) TesT
c) T NOW
d) 0
e) –1

An Introduction to Programming Using Microsoft Visual Basic 2010


© 2012 EMC Publishing, LLC
3

16. Dim first As Char = "f"


Dim middle As Char = "m"
Dim last As Char = "l"
'Create monogram
Me.lblMonogram.Text = Char.ToUpper(first) & Char.ToUpper(middle) _
& Char.ToUpper(last)

17. Dim date As String = Me.txtNumber.Text
Dim month As String = date.Substring(0, 2)
Dim day As String = date.Substring(3, 2)
Dim year As String = date.Substring(6, 2)
Dim monthName As String
Select Case month
Case "01"
monthName = "January"
Case "02"
monthName = "February"
Case "03"
monthName = "March"
Case "04"
...
End Select
Dim newDate = monthName & " " & day & ", 20" & year
Me.lblOutput.Text = newDate

18.

19. ViSuAl bAsIc iS So mUcH FuN!

20. a) Algorithm to count the number of words in a sentence:


1. Assign the sentence to a String variable.
2. Initialize a counter variable to 1.
3. Use the IndexOf() String method to search the sentence for a space (“ “).
4. Enter a loop while a space character has been found.
5. In the loop, increment the counter variable.
6. In the loop, use the Remove() String method to delete the substring of the first word
and space character in the sentence.
7. In the loop, use the IndexOf() String method to search the new, shorter sentence for
a space (“ “).
8. Display the counter variable value.

An Introduction to Programming Using Microsoft Visual Basic 2010


© 2012 EMC Publishing, LLC
4

b) Algorithm to count the number of letters in a sentence:


1. Assign the sentence to a String variable.
2. Initialize a counter variable to 0.
3. Create a for loop from 0 to the sentence length – 1.
4. In the loop, use the Substring() String method to get one character of the string.
5. In the loop, convert the one-character substring to an uppercase character.
6. In the loop, use the Like operator to compare the one-character substring to “[A-Z]”.
If the comparison is True, increment the counter variable.
7. Display the counter variable value.

21. a) True.
b) True.
c) True.
d) False. Accumulator variables can modify any type of numeric variable.
e) False. A flag, also called a sentinel, is a condition used to signify that a loop should stop
executing.
f) False. Sentinel values can be whatever value makes sense for the application.
g) False. sum is used as a counter variable.
h) False. The keyword Step controls the increment of decrement amount of a counter in a
For...Next statement.
i) False. String is a class, not a primitive data type.
j) True.
k) True.
l) False. vbTab is a constant that can be used to place a string of eight characters into a field.
m) False. The corresponding uppercase and lowercase letters have separate Unicode values.
n) False. Compare returns a 0, a positive number, or a negative number.
o) True.

An Introduction to Programming Using Microsoft Visual Basic 2010


© 2012 EMC Publishing, LLC

Das könnte Ihnen auch gefallen