Programming Qbasic Part Four

Hi and welcome to lesson 4. Now we use some commands that don't effect on screen necessarily.

RANDOMIZE TIMER ' Here we make sure that we get random numbers from computer
Guessed = 0 ' Makes sure that variable Guessed is 0
FOR I = 1 TO 5 ' Here we start loop from 1 to 5
    CLS ' Clears the screen
    INPUT "Guess a number between 1 and 6" ;Number 'Ask's you to give a number between 1-6
    Dice = INT(RND * 6 + 1) ' Randomizes one number between 1 -6
    IF Dice = Number THEN GOSUB CORRECT ' If guess was correct then we go down there ;)
    IF Dice  Number THEN PRINT "You guessed wrong" ' If guessed wrong then we print this.
NEXT ' And from here we go back to begin of loop if it haven't done it 5 times

CLS ' Clears the screen 
PRINT "You guessed right number at" ;Guessed;" times" ' How many times guessed correct 
END ' End of program

CORRECT: ' If guess was right we visit here quickly 
Guessed = Guessed + 1 ' How many times guessed correct 
PRINT "YIPPEE!!! You got it!!!" ' Let's celebrate 
RETURN ' Get's back on loop

Ok, you typed it and tested what it does? Yeah, it's kind of a dice game that ask's you to guess what number program will randomize. You probably noticed few new commands and that END aint last command of program? But let's start as usual at beginning of code and check line by line what it does.

RANDOMIZE TIMER
Randomize command works together with INT(RND * 6 + 1). INT(RND * 6 + 1) randomizes one number between 1 to 6. If you don't use RANDOMIZE TIMER command in your program the randomized numbers will be always same ones. You can turn your computer off back on and try your program with out that command. Numbers will be always the same. That's why we need RANDOMIZE TIMER. QBasic's help section explains this command so good that I don't see point to do it here.

Dice = INT(RND * 6 + 1) ' Randomizes one number between 1 -6
Ok, this might look a bit funny now. You probably guessed that what ever number is randomized, Dice variable get's that value But (RND * 6 + 1)??? Ok, you think now why that +1 is there? We already asked program to randomize RND * 6 so then the max. number should be 6, so why we need that +1 at end of there?

Well, computer start's RND command from 0. So if we want 6 first numbers then computer thinks that we want these 0,1,2,3,4,5. There is six numbers, right? So there comes that + 1 usable. If you type this; Dice = INT(RND * 100 + 1) ' Randomizes one number between 1 -100 ;)

Guessed = 0 ' Makes sure that variable Guessed is 0

Ok, comment explains it wery well :D

FOR I = 1 TO 5 ' Here we start loop from 1 to 5
    '(Here is some code)
NEXT I' And from here we go back to begin of loop if it haven't done it 5 times

These two commands also works together. You cant use FOR with out NEXT, alltough you cant use NEXT with out FOR. Idea here is that FOR starts a loop. NEXT ends it and what ever are typed between these two commands are made 5 times. If you use 6 instead of 5 on that FOR' line then it loop's 6 times instead of 5.

Ok, one to go. Well, two in fact, but they also work together.

GOSUB (somewhere)
RETURN (goes back)

GOSUB works same way than GOTO but it has one great option that makes it more usefull than GOTO. GOTO goes somewhere but you cant get back from there if you dont know where you came originally. RETURN makes program to go back to where we jumped with GOSUB command. Let me put a simple example.

CLS ' Clears the screen
INPUT "Whats your name";Variable$ : GOSUB BLAA
INPUT "Whats your mother name";Variable$:GOSUB BLAA
END

:BLAA PRINT "You typed:";Variable$ : RETURN
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.