Tracing For Loops with strings

advertisement
Visual Basic
Tracing Loops with Strings
Name_____________________________
Period_______
Trace the following code segments. Assume that all variables are initialized to zero and that underscore characters represent
blank spaces.
1.
strWord = "alphabet"
J
intLessThanK
strPart
J
intGreaterThanK
For J = 0 to 7
If (strWord.Substring(J, 1) < "k") Then
intLessThanK = intLessThanK + 1
Else
intGreaterThanK = intGreaterThanK +
1
End If
Next
2.
strName = "Wyo_Spartans"
intLength = strName.Length()
For J = intLength - 1 To 0 Step -1
strPart = strName.Substring(J)
If (strPart = "Spartans") Then
Exit For
End If
Next
3.
strWhole = "John Doe"
intLength = strWhole.Length()
strWhole
intLength
J
intBlank
strPart1
For J = 0 To intLength - 1
If (strWhole.Substring(J, 1) = " ") Then
intBlank = J
End If
Next
strPart1 = strWhole.Substring(0, intBlank)
strPart2 = strWhole.Substring(intBlank + 1)
4.
strRiver = "Mississippi"
For J = 0 to strRiver.Length() – 1 Step 2
If (strRiver.Substring(J, 1) = "s") Then
intCount = intCount + 1
ElseIf (strRiver.Substring(J, 1) = "i") Then
intCount = intCount - 1
End If
Next
strRiver
J
intCount
strPart2
Evaluate the following code segments where the following variables have been declared. Show the output that would display in
the label. Delimit your output with double quotes and represent blank spaces as the letter b with a slash through it ( b ). If an
errorwould occur explain it.
Dim strSchool As String = "Wyomissing"
Dim strFirstName As String = "Franklin"
Dim strLastName As String = "Roosevelt"
Dim strZip As String = "19610"
Dim J As Integer = 0
Dim intBegin As Integer = 1
Dim intNum As Integer = 6
1. lblOutput.Text = strFirstName + strLastName
"FranklinRoosevelt"
2. lblOutput.Text = strLastName + ",b" + strFirstName
3. lblOutput.Text = Str(strSchool.Length())
4. lblOutput.Text = Str(Val(strZip) + strZip.Length())
5. lblOutput.Text = strSchool.toUpper()
6. lblOutput.Text = strFirstName.Substring(0, 5)
7. lblOutput.Text = strLastName.Substring(5, 1)
8. lblOutput.Text = strSchool.Substring(3)
9. lblOutput.Text = strFirstName.Substring(5, 5)
10. lblOutput.Text = strFirstName.Substring(J, 2) + strLastName.Substring(4, 1) + strLastName.Substring(6, 1)
11. lblOutput.Text = strLastName.Substring(intBegin, intNum)
12. lblOutput.Text = strFirstName.Substring(intBegin – 3)
13. lblOutput.Text = strFirstName.Substring(strFirstName.Length() – 1, 1)
14. lblOutput.Text = strLastName.Substring(strLastName.Length() – 1)
15. lblOutput.Text = strSchool.Substring(strSchool.Length() – 2, 1)
16. Write a line of code that displays the last 3 letters of strLastName in lblOutput.
17. Write a statement that displays the second letter of strSchool in lblOutput.
Download