Member Avatar for robert.montgomery.75054

I need help with the code below. I coded using the For..Next statement, now I have to place the For..Next statement with a Do..Loop statement. Can anyone help down the right path? Thank you in advance.

`

        Dim strGrades() As String = {"A", "B", "C", "A", "B", "A", "F", "A", "D", "B", "C"}
        Dim strSearchFor As String
        Dim intCount As Integer


        strSearchFor = txtLetterGrade.Text.Trim.ToUpper
        intCount = 0



        For intSub As Integer = 0 To strGrades.Length - 1
            If strGrades(intSub) = strSearchFor Then
                intCount = intCount + 1

            End If
        Next intSub

        'Message box that displays the answer from the execution of the For Statement
        MessageBox.Show(strSearchFor & " : " &
                        intCount.ToString,
                        "Grades", MessageBoxButtons.OK,
                        MessageBoxIcon.Information)

`

Hi,

You can find some explanation about how to use a Do...Loop, Here

Does this help..i maybe way off grid with this

Do Until intSub <= strGrades.Length
            If strGrades(intSub) = strSearchFor Then
                intCount = intCount + 1

            End If
        Loop
        MessageBox.Show(strSearchFor & " : " &
                          intCount.ToString,
                          "Grades", MessageBoxButtons.OK,
                          MessageBoxIcon.Information)

I'm pretty sure that won't work you might need to change it to this:

Dim insSub As Integer = 0
Do Until intSub = strGrades.Length
    If strGrades(intSub) = strSearchFor Then
        intCount += 1
    End If
    intSub += 1
Loop
commented: Thank you for that +0

You don't need a For loop or a Do loop or any kind of loop. The number you want is

intCount = Filter(strGrades,strSearchFor).Count

as long as you won't have other than single letter grades. For example, if your list of grades included "A B B A A+ A-" then the above would return 4 if strSearchFor was "A".

If you were told (for an assignment) to use a Do loop then the assignment is stupid. Nobody should teach you the wrong way to do something. Even a For loop with an index is silly (in this case). If you must use a loop then use

For Each grade As String In strGrades
    If grade = strSearchFor Then intCount += 1
Next

The above does an EXACT match to strSearchFor

While of very little practical value, an exercise like this, could be designed to encourage flexible thinking, and to get the student used to the idea of approaching a problem from different angles. Also the instructor may not have control over the curriculum and has to include an exercise using a Do-Loop.

Thank you tinstaafl. I see the error..I have a follow-up question on this if yall do not mind, hope robert dont mind...How would a code look for a message box that propmpts the user to enter a valid grades, in this case A through F. i want to expand my learning..This is what I have so far, so what I am missing?

If intSub = strGrades.Length Then
            MessageBox.Show(strSearchFor & " : " &
                            intCount.ToString,
                            "Grades", MessageBoxButtons.OK,
                            MessageBoxIcon.Information)
        Else
            MessageBox.Show("Enter a valid grade", "Grades",
                            MessageBoxButtons.OK, MessageBoxIcon.Information)

        End If

I don't see anything obviously wrong. Is it doing something you don't want it to do?

It is not prompting message "Enter a valid grade..everytime I give it a letter not in the array it stills give me a value..Like if I type in the letter G it gives me a value G:0..what i want to happen when a person does this is that a message box will inform the user to enter a valid code

You make a string of valid grades such as

Dim ValidGrades As String = "ABCDF"

then you validate the input as

If Instr(ValidGrades,strSearchFor) = 0 Then
    MsgBox("Invalid grade")
Else
    'do the search
End If
commented: Awesomeness..it works..thank you for helping expand my knowledge +0

Since the Do loop is already indicating validation you can use that result like this:

Dim strGrades() As String = {"A", "B", "C", "A", "B", "A", "F", "A", "D", "B", "C"}
Dim strSearchFor As String
Dim intCount As Integer
strSearchFor = txtLetterGrade.Text.Trim.ToUpper
intCount = 0
Dim insSub As Integer = 0
Do Until intSub = strGrades.Length
    If strGrades(intSub) = strSearchFor Then
        intCount += 1
    End If
    intSub += 1
Loop
If intCount = 0 Then
    MessageBox.Show(strSearchFor & " : " & intCount.ToString, "Grades", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
    MessageBox.Show("Enter a valid grade", "Grades",  MessageBoxButtons.OK, MessageBoxIcon.Information)
End If

oops noticed a typo line 13 should be If intCount <> 0 Then

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.