question

MatthewTurcotte-2973 avatar image
0 Votes"
MatthewTurcotte-2973 asked ·

For Loops to prompt a user for 5 facts about a topic.

Any suggestions on how to start a for loop to prompt the user for five facts about a topic. I am a beginner and have not done well with for loops wit past sections and am trying to grasps the concept. I am supposed to create the facts had the user use a input box to get a fact set to a string variable add the fact to the list box. I have a menu box that I have added add fact, remove fact, clear list, and close. I am pretty lost on how to start this code if anyone has any educational tips that they could share with me I would appreciate it.

dotnet-visual-basic
· 4
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

My honest best advice is to go to your TA or professor and explain your confusion. Most of us here are not educators.

0 Votes 0 ·

Well I have and they prefer we research online which I have. I am trying to contact a tutor through school just thought there may be someone here that could give an example of a for loop. My class is virtual so I just have power points, no book, and they read it to us with very limited explanation. So, I thought I’d ask here to see if I could get some information.

0 Votes 0 ·
DonR-8455 avatar image DonR-8455 MatthewTurcotte-2973 ·

I am no teacher, but maybe this will help a little. In this example:

 For idx As Integer From 1 To 5
     ' code for asking a question goes here
 Next

what's happening is, you are declaring a variable of type Integer, called idx (in this example; it can be whatever variable name you want). From initializes the variable with the value of 1, in this case. Then the code for getting the user to input something is run. When Next is reached, the value of idx is incremented, and compared to the value specified by To. If it is not greater than that value, the code between For and Next is run again, and so on, until the value of idx is greater than the To value, 5 in this case, and then execution continues with the line after Next. In short, a For-Next loop will run a specific number of times, depending on exactly what you put in the From and To values.
















0 Votes 0 ·
Show more comments

1 Answer

karenpayneoregon avatar image
1 Vote"
karenpayneoregon answered ·

Here is the base logic where entering nothing or pressing cancel will exit the loop.

 Public Class Form1
     Private Sub EnterFactsButton_Click(sender As Object, e As EventArgs) Handles EnterFactsButton.Click
    
         FactsListBox.Items.Clear()
    
         Dim factIndex = 1
    
         While True
    
             Dim value = InputBox($"Enter fact {factIndex}")
    
             If String.IsNullOrWhiteSpace(value) OrElse factIndex = 5 Then
                 Exit While
             Else
                 FactsListBox.Items.Add(value)
                 factIndex += 1
             End If
    
         End While
    
     End Sub
 End Class


·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.