For Loops

We did some quick questions (here they are without answers constructs-quick-qus-wthout-answers.jpg in case you want to try them again, with answers at the bottom of the post so you can check them), then we had a look at how to make code repeat itself a set number of times. One way to do this is with a for loop.

In Visual Basic you need a counter (to keep track of how often the loop has gone round). This can then be used to do clever things, like change a particular element of an array. For example in the code below on the tenth go around of the loop, what the user types in will go into place 10 of the array (if you are starting from 1):

 

 

For counter := 1 to 15

 

name(counter) = InputBox(“Please type in another name”)

 

Next

If you still don’t get it, re-read the powerpoint below:

for-loop.jpg

 

And these are the answers to the quick questions:

constructs-quick-qus-with-answers.jpg

Advertisements

One Response to “For Loops”

  1. hobbylobby Says:

    for each loops technically have counters, but the count is implied by the object being referenced.

    for each dr as datarow in datagridview

    next

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: