User:Anish Singh

From Wikibooks, open books for an open world
Jump to navigation Jump to search

There are three types of loop for us to cover with VB.NET: a For loop, a Do loop, and a While … End While loop. This last one is almost the same as a Do loop, and we won't be covering it here. But the other two types of loop come in very handy, and a lot of the time you can't programme effectively without using loops.


What Is Loop ?

A loop is something that goes round and round and round. If someone told you to move your finger around in a loop, you'd know what to do immediately. In programming, loops go round and round and round, too. In fact, they go round and round until you tell them to stop. You can programme without using loops. But it's an awful lot easier with them. Consider this.

You want to add up the numbers 1 to 4: 1 + 2 + 3 + 4. You could do it like this

Dim answer As Integer

answer = 1 + 2 + 3 + 4

MsgBox answer

Fairly simple, you think. And not much code, either. But what if you wanted to add up a thousand numbers? Are you really going to type them all out like that? It's an awful lot of typing. A loop would make life a lot simpler.

But don't get hung up too much on the name of the Loop. Just remember what they do: go round and round until you tell them to stop.