You are here

For Loop vb.net tutorial

Loops

Count the number of cups you need to fill a pitcher. This number is feasible and may be programmed on a computer as seen below.


cups = 0
'pitcher is now empty.'
cups = cups + 1
cups = cups + 1
cups = cups + .5
'pitcher is now full.'
msgbox(cups)

In the code above, you will notice that you can actually count the cups one by one. You will know when the pitcher is empty and you will know when the pitcher is full. In some cases, counting things one by one isn't actually practical anymore. For instance, what if you were asked to count the number of cups needed to fill a swimming pool? Does this sound fun to you? If you are really bored and have nothing to do with your life then you might as well count the number of cups one by one. If you are knowledgeable with loops then you can let the computer do the counting.

www.seaworld.com, at Sea World, you can enjoy looking at different water creatures from all over the world. Do you think it is possible to count the number of cups of water needed to operate the park everyday?

For

The For Loop is called the For Loop because it loops "for every number." In the code below, you will see that the For Loop adds "for every number."


counter = 0
sum = 0
for counter = 1 to 50
sum = sum + counter
next counter
msgbox(sum)

If you loop the code in your head, it would go like 1 + 2 + 3 + 4 + ... ... + 49 + 50 = sum. "For every" number, the sum is incremented.

The For Loop takes on the structure below.


For [variable] = [1 to N]
[command lines]
next [variable]

Forums: