QBASIC Lesson 3 - IF and FOR...NEXT Okay, this is a bit of an ambitious one. You see, the IF the command is quite easily the most important command in the entire of QBASIC, so it's important that I cover it fully. Then again, FOR...NEXT statements are also immensly important. So I'm going to have to take this one slowly. Firstly, and IF statement. Simply speaking, an IF statement does something IF something else is true. For example, IF a=b you could make c=c+1. And here is how you'd do it: IF a = b THEN c = c + 1 Nothing to it, eh? So, basically, if at any point in the program you want to do something ONLY if something (the condition) is true. There are some very important symbols that can be used in IF statements: < less than > greater than <= less than or equal to >= greater than or equal to <> not equal to = equal to NOT turns TRUE into FALSE and FALSE into TRUE AND this can be used to state that condition 1 AND condition 2 must be true OR this can be used to state that condition 1 OR condition 2 must be true Brackets can also be used to build up some quite complicated logic statement. As always, see what the QBASIC index of commands turns up for the above. For now, however, have a look at this program: CLS INPUT "Please enter three numbers separated by commas? ", a, b, c PRINT "In order the numbers are: "; IF (a > b) AND (b > c) THEN PRINT a, b, c IF (a > c) AND (c > b) THEN PRINT a, c, b IF (b > a) AND (a > c) THEN PRINT b, a, c IF (b > c) AND (c > a) THEN PRINT b, c, a IF (c > a) AND (a > b) THEN PRINT c, a, b IF (c > b) AND (b > a) THEN PRINT c, b, a This program merely asks the user for three numbers and then, using a whole lotta IF statements, it prints them out in order. Later in the series we'll discuss a much better way of doing this, with more than three numbers, but for now type the program in and experiment with different combinations of numbers. And now, for the line by line breakdown you've all been waiting for: CLS Clears the screen; you should be pretty familiar with this by now. INPUT "Please enter three numbers separated by commas", a, b, c This line demonstrates how the INPUT command is capable of getting more than one variable (or string) from the user. Note that the user must type them in separated by commas, and that they must always type in the correct number Of variables. PRINT "In order the numbers are: "; A simple print statement, apart from the semicolon at the end. This just tells the computer not to go onto the next line. Take the semicolon out and you'll get the idea. IF (a > b) AND (b > c) THEN PRINT a, b, c IF (a > c) AND (c > b) THEN PRINT a, c, b IF (b > a) AND (a > c) THEN PRINT b, a, c IF (b > c) AND (c > a) THEN PRINT b, c, a IF (c > a) AND (a > b) THEN PRINT c, a, b IF (c > b) AND (b > a) THEN PRINT c, b, a These lines check for any possible combination of values, and print out the answers in order. Note the use of AND to tell the computer that the conditions must be true. Try putting in OR (or even XOR) to see what kind of results you get. Of course, the IF statement doesn't just have to print things out. You can use any commands at all, or any group of commands, on the end of an IF statement. This can be achieved in two ways: IF condition THEN command 1: command 2: command 3: command 4, etc In this way the commands are separated by colons. IF condition THEN command 1 command 2 command 3 command 4 END IF In this way the commands can be placed on separate lines, making the program much neater. However, you do have to use the ENDIF command to tell the computer that you have finished the IF statement. Finally on the subject of IF, there is another extremely important command: ELSE. This basically carries out a command if the IF statement was not true. Okay, things are starting to get a bit complicated. This might clear it up a bit for you: CLS INPUT "Please enter two numbers separated by commas", a, b IF a > b THEN PRINT "A is greater than B" ELSE PRINT "A is less than or equal to B" This asks for two number, then IF the first is greater than the second, it tells the user that this is so. Otherwise (ELSE) it tells the user that this is not so. Note that this line could have been written making use of the ENDIF command: CLS INPUT "Please enter two numbers separated by commas", a, b IF a > b THEN PRINT "A is greater than B" ELSE PRINT "A is less than or equal to B" END IF This can look much neater than having everything on one line. Anyway, that about covers it for the IF statement. Another major use of the IF statement is to branch off to a subroutine; don't worry about this yet, I'll be covering subroutines and labels next time. For the time being, see if you can write a program which asks the user their name and age then tells them whether they should be in school, in work or retired. Good luck! Right, now onto the subject of FOR...NEXT loops. These are the ultimate labour saving device, and as such you should make every effort to learn to make good use of them. Here is an example of two programs, one of which uses a FOR...NEXT loop. CLS PRINT "1" PRINT "2" PRINT "3" PRINT "4" PRINT "5" PRINT "6" PRINT "7" PRINT "8" PRINT "9" PRINT "10" that wasn't much fun, was it? Now have a look at this: CLS FOR a = 1 TO 10 PRINT a NEXT What this is doing is telling the computer to count from 1 to 10, using the variable a to store the counted number. The command NEXT tells the program to loop back to the FOR statement. So if you want to print out all the numbers from 1 to 1000 (God knows why): CLS FOR a = 1 TO 1000 PRINT a NEXT FOR...NEXT loops are also useful for doing something a set number of times. Finally on the subject of FOR...NEXT loops, you can also change the amount that it counts up in, which is ideal for creating times tables, as in this program: INPUT "Please enter a number", n FOR a = 1 TO 12 * n STEP n PRINT a NEXT Oh, there was one more thing before I finish: FOR...NEXT loops are also ideal for slowing down programs so you can see what they are doing. Taking the above program as an example: FOR a = 1 TO 12 * n STEP n PRINT a FOR d = 1 TO 1000 NEXT NEXT What is going on here is that, to create a delay, the computer is told to use the variable d to count up to 1000. This doesn't take it all that long, but some well-used delays in a program can make it look quite a lot better, although they can also make it far more annoying to use. Until next time, experiment, read the help files, mess about with the commands given here, don't run while holding sharp objects, and I'll see you then!