Note: This tutorial is for absolute beginners. If you aren't an absolute beginner and you dislike the tutorial, that's too bad. If you are an absolute beginner and there's something you don't understand, read through the tutorial a second time. If you STILL have difficulties, e-mail me (TheMysteriousStrangerFromMars@yahoo.com) and I'll try to help you out. I do assume you at least have a decent knowledge of how to use your computer - if not, there's not much I can do for you. By the way, you'll need FreeBasic to use this tutorial - you download that at freebasic.net, so be sure to download and install it before doing this tutorial. Now start reading!
In tutorial 2 you learned about conditionals - which is one kind of flow control Operation. Another kind of flow control Operation is the loop. No, there isn't an associated thing called a twirl - that's just for fun. To loop is to do something 0 or more times - you may do it several times, or not at all. You may also just do it once, but usually you do it more than once. Loops are also conditional, but rather than just executing code once if the condition is true, a loop executes a block of code again and again until the condition becomes untrue.
The most basic looping construct in FreeBasic is the Do Loop. It looks like this:
This may be a bit hard to understand. What that means is that you can put the condition before the block of code or after it - if you put it before, the code might never execute since the condition could be false. If you put it after, the code will always execute at least once but if the condition is false then no more after that. Each has its own use; but you can kind of guess what each means. You also put either While or Until before the condition. This way, you may do the Loop so long as the condition is true - and stop looping as soon as the condition becomes false - or you may do it so long as the condition is false, and stop looping as soon as it becomes true. If you Do While or Loop While, it executes the code so long as the condition is true. If you Do Until or Loop Until, it executes the code so long as the condition is false.Do [While|Until condition] 'Stuff to do Loop [While|Until condition]
Looping is yet another extremely useful coding practice. For example, you may wish to ask the user after your program finishes running "Would you like to do it again" and if so, start the program again. The easiest way to do this (that you've learned so far - later you'll learn how to use Subs and it becomes even easier) is using a loop. So let's write a very simple program the sole purpose of which is to tell the user what the temperature is. Of course, we have no way of knowing what the temperature actually is, so we'll just say it's 70F. This program obviously isn't very useful, but it's to illustrate a point (if you did have a way of finding what the actual temperature is, you'd do it exactly the same way):
There's some very familiar things in this program, but I also added a statement you've probably never seen before: Cls. Run the program, try figuring out what the Cls does. If you can't figure it out, try commenting it out (by putting a ' character in front of it) and see what it does then. Kind of a useful thing, eh?Dim mychar As String * 1 Do Cls Print "The temperature is 70 degrees Fahrenheit." Print "" Print "Enter y to see the temperature again or anything else to quit." Loop Until mychar <> "y" Print "Press any key to end the program... Sleep
I encourage you to experiment with the Do...Loop construct. I've told you how each one works, and what it does. Try writing a program like this, not necessarily a useful program, that makes use of each form of the Do...Loop construct. That'll be your first assignment, but don't worry - the party isn't over yet!
There is another looping construct. This one is the While...Wend loop. It's technically redundant, since Do can do exactly the same thing, and we don't actually use it a lot, but I'll tell you about it now so if you ever see it you know what it means. Below I show two blocks of code; they are equivalent.
As you can see, there's not a lot about While you don't already know. It does just the same thing as the form of Do shown: it executes code so long as the condition is true, and it checks the condition before it executes the code so that it is possible for the code not to execute at all.Do While condition 'Put some code here Loop While condition 'Put some code here Wend
Before we continue and look at the last kind of loop, let me make one quick point: it is important that the code inside of the loop is capable of modifying the variable checked in the condition, or the loop will never end. Consider this example:
Since 0 will never equal 1, this loop will continue forever. Also if you don't put any conditions at all:Do 'Code here Loop Until 0 = 1
The code will execute forever. Now it's actually true that there are times when you'll need a loop running forever; for example, if you create a program whose sole goal is to monitor all information coming in on the ethernet line (not that you know how to do that at this point - it's just an example). If you program is supposed to sit there and check bits of data coming into your PC from the internet, that's an example where you'd probably use an infinite loop like this. But otherwise, a loop like that will never end so your program will just sit there doing the same thing again and again. So you'll need to put code inside the loop that is capable of detecting (probably using If) whether it's done enough. If the code is done executing, it will change the variable in the loop condition so that the condition will cause the loop to end. For example, if the condition is "Until t = 0" then so long as the code runs, t has to be nonzero. But if the code decides it's done doing what needs to be done, it sets t to 0 so the loop will stop. Otherwise the loop will never end.Do 'Code here Loop
Quite to the contrary, the following code will never run. This may help you understand the difference between putting a condition by the Do and putting it by the Loop (While...Wend acts the same as if the condition were by the Do):
While if the condition where by the Loop, it would run once before quitting... and if it were an Until instead of a While, it would run forever no matter where you put it. Of course, this loop is unlikely to be used for anything practical, but what may be used is a variable that is equal to 0 and being checked constantly. Once again you'll have the same thing - unless somewhere in your code the variable is modified.Do While 0 = 1 'No matter what you put here, it will never run Loop
Note, by the way, that "Until t = 0" does the same thing as "While t <> 0". It's preferable, whenever possible, to use the positive form - in this case, it would be the Until form. Not a few people have non-easiness figuring out what a non-small number of non-positive statements means (many people have a hard time understanding lots of negative statements).
Right, so now that you understand that you always need to make sure these loops stop (unless you have a reason never to make them stop) we'll look at one more type of loop: the For loop. For is quite a bit different from any other loop, because it doesn't use a condition... exactly. Instead, it merely repeats the code a certain number of times. For also needs a variable to work; there's no such thing as a For loop without a variable (unlike Do and While loops, which can work without a variable albeit not very well).
What follows is a For loop. All For loops have the same syntax, though there are a few other options which I'll cover in a moment:
The important feature of For loops is that they modify the variable automatically. The For loop given above is equivalent to:For i = 1 to 10 'Code here Next i
So technically, For is also redundant, but it's a shorthand and we much prefer it when we need that particular type of loop. The nice thing about For loops is that you can use the number in the For loop for all sorts of things. For example, if you wanted to print the numbers 1 to 10 on the screen you would use this code:i = 1 Do 'Code here i = i + 1 Loop Until i > 10
You can guess what it does - each time in the loop, it prints i on the screen, and each time i is a different number. So i starts out 1, which is printed, then goes up each time until it's 10, which is printed, and then quits. Now of course you can simply type this if you want to count to 10:Dim As Integer i For i = 1 to 10 Print i Next i Sleep
But what if you wanted to count to a variable number? Why then you need the For loop! Here's an example:Print "1" Print "2" Print "3" Print "4" Print "5" Print "6" Print "7" Print "8" Print "9" Print "10"
Of course, you may call the variable anything - it doesn't have to be i. Furthermore, the first number and the last number can be anything you want as well. The first number may also be a variable, just like the last one is in that example. Finally, you may count differently.Dim As Integer i, countto Print "What number shall I count to?" Input countto For i = 1 to countto Print i Next i Sleep End
What do I mean by that? Well, remember in grade school when you had to count by 2's? You know, "2, 4, 6, 8, 10, 12" and so on? Well For can do that, too. For can also count backwards - from a bigger number to a smaller one. But in order to do that, you have to add something to the For statement: "Step." Here's how you would count by 2's:
And if you want to count backwards? "Step" by a negative number!Dim As Integer i For i = 0 to 10 Step 2 Print i Next i
Once again the power and simplicity of Basic has been demonstrated. The past three tutorials have shown you the most important concepts in programming and all of it is really easy to learn - very similar to English! But there's more coming, don't you worry!
Your assignment is to write a program that asks for twenty numbers and gives the average after each one. You calculate the average by adding all the numbers together and dividing by the number of numbers - for example, the average of 1, 2, 3, 4 is (1+2+3+4)/4, since there's 4 numbers in all. The number of numbers will change after each one, so you'll need to keep track of the sum of all the numbers so far, the current number of numbers, and the number just entered by the user. Of course, be sure to make it user friendly by giving helpful instructions on what to do, and clear the screen before showing each new average. And use Sleep to make sure the window doesn't disappear!
In part 4 we'll talk more about different variable types and number systems. Until then, work on your assignments!
The fourth tutorial is here(<--Previous) Back to Index (Next-->)