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)

`

robert.montgomery.75054
Deleted Member

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
Comments
Thank you for that

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

Edited 3 Years Ago by Reverend Jim

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

Edited 3 Years Ago by siaosituimoloaublood: edit comment

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
Comments
Awesomeness..it works..thank you for helping expand my knowledge

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

Edited 3 Years Ago by tinstaafl

This article has been dead for over six months. Start a new discussion instead.