Search code examples
vb.netbasic

Visual Basic Programming freezing when using do while loop


I have a VB project for school and it requires to use a loop. I'm trying to test it on my computer but each time I press "OK" on my program the program freezes and nothing is added onto the listbox. The only thing that appears new is the scroll bar on the listbox.

    Dim counter As Integer = 0

    If total = 5 Then
        Do While counter < 5
            Marks.Items.Add(studentNumber)
        Loop
    End If

Solution

  • You have to increment the counter

    Dim counter As Integer = 0
    
    Do While counter < 5
       Marks.Items.Add(studentNumber)
       counter += 1
    Loop
    

    If you want to add 5 items to your list, it is not necessary to use the If statement, because the while loop will be executed exactly 5 times