.:: Quick BASIC Beginner's Guide (Part 2)::. By: infinite_reality aka `uN[In$tall]` Released under (T)he (G)oon (S)quad; April 2004; www.TGS-Security.com Contact: clientside@hotmail.com IRC: irc. zoite.net//#Hackerlounge//6667 Find me at: http://HackerLounge.no-ip.com http://lnx.hackerscenter.com/forums/index.php http://www.Brain-Hack.org/forum/index.php http://www.anomalous-security.org Shoutouts: To the fellas: R@v3n, asTHma, Mr.Mind, DJSplit, NoUse, Subby, Enjoi, D@MN3D, Shamus, sn4k3, Kevin, wkd, Messiah, Messiah's Mum, GluTuk, VBFavre69; all the good people at HackersCenter.com, Brain-Hack.org, AnomalousSecurity.org, and HackerLounge Forums; all my homies from {Dark`Nexu$}; and to The Goon Squad, TGS. Phuck you: To all the children on Battle.Net that are unbelievably stupid (chat logs @ HackerLounge); to all you lamers that give hacking a bad name; to all those greedy over-wealthy companies that don't live up to their company policy; to all those retard consumer-whores that continually support the over-wealthy companies by buying their bullshit products; to all those governmental wankers that think they can pull the shit over our eyes and stop us from learning, teaching, mastering our new age of technology. ---- CONTENTS: 0.5--STUFF 1.0--DIM 2.0--FOR <> NEXT 3.0--STEP 4.0--GOTO 5.0--IF <> THEN 6.0--COLOUR 7.0--Next, and final, QB Guide (part 3) ---- 0.5--STUFF Last guide included the syntax commands LET, INPUT, and PRINT. But they're limited, so now I'll explain some new commands that will help with more involved programs. Numbers are obviously a large and important part of any programming language, which is also why variables are important. Instead of using alot of variable names, and in effect, creating too many to "clean", we use Arrays. Arrays are bastards. I don't like them, and they're a bitch to teach, so I'll do my best to explain how it all works. The most common analogy of how to describe arrays is the "boxes in memory" image. Since it seems to work, I'll use it too. Try to imagine arrays as boxes in the memory, which are also divided into smaller boxes--boxes inside boxes of the memory. Arrays allow you to put shit in the smaller boxes, and then organise them into a big box. This is what an array does--it is an area in the computer's memory seperated into smaller boxes for different variables. ------------------------------------------------------------------ --------------------------------- - - - one - four - - - - - one - four - - - - - - ------------------------------------------------------------------ - one - four - - - - two - three - - - - - two - three - - - - - - ------------------------------------------------------------------ --------------------------------- - one - four - - - - - - - - - one - four - - - - ------------------------------------------------------------------ - two - three - - two - three - - - - - - - - - two - three - - - - ------------------------------------------------------------------ --------------------------------- As you can see, there is one "box" of memory (right). It is then broken up into four smaller boxes (right), and each contain four more smaller boxes (left). This is basically what an array is, and I hope the diagram helps you understand it. Of course, arrays can change to the diagram, but that is the basic visual representation. To create an array, QB uses the DIM command... ---- 1.0--DIM [code] DIM arrayName$ (number) [/code] is the basic command structure for DIM. arrayName$ is obviously the name of the array you're creating (always follow it up with $, !, etc that was shown in Guide 1); number, in the parentheses, is the amount of smallers boxes you want the array to have. Now to use an array, we must assign values to variables using the LET command (Guide 1), but because this is for an array, the variable must be specified as to which part of the array it will be assigned to. [code]DIM Haxxor$(5) LET Haxxor$(1) = "Leeto" LET Haxxor$(2) = "Neato" LET Haxxor$(3) = "Burrito" LET Haxxor$(4) = "Dorito" LET Haxxor$(5) = "LeanCuisino" [/code] Now the program knows that the array Haxxor$ has 5 boxes to fill with variables, and the variables' information are listed below (which is what will be in the 5 boxes). To output _one_ of the variables in the array, you just simply PRINT like normal [code]PRINT "I am "; Haxxor$(1); "!" PRINT PRINT "This Guide is so "; Haxxor$(2); "..." PRINT PRINT "I am sick of eating "; Haxxor$(5); "." [/code] And each sentence will be displayed [quote]I am Leeto! This Guide is so Neato... I am sick of eating LeanCuisino. [/quote] Alternatively, you can input a value into one of the array boxes, or edit a value existing, by using the INPUT command (Guide 1) [code]INPUT "What am I"; Haxxor$(1) INPUT "I like to eat"; Haxxor$(3) [/code] and so on. As you can see, it is possible to use an array just as a variable is used. Using arrays, however, gives the advantage of managing (if you wanted to) hundreds of different variables, such as names, values, string and dollar amounts (with decimals if you wanted). ---- 2.0--FOR <> NEXT Heya, now you know arrays and shit, so I guess it's time to move on to the next part, the FOR <> NEXT command. Basically, this command is used to reprint the same variable/string/sentence/data over again--a lopp,if you will. Instead of typing out 20 PRINT commands, you use a loop command to do the same job, only less effort. A loop is exactly that, a loop. It is like driving your car around a round-a-bout, and every time you do a complete 360, you end up at the same spot and repeat yourself until the next cycle. A loop is a set of instructions to the program that repeat: a set amount of times, or cycles; until a condition is met; inifinitely. The first loop I'll show is FOR <> NEXT command. For FOR <> NEXT to work, you simply replace <> with commands of choice. Everything in between FOR and NEXT will be looped. Simple enough? Here's the syntax: [code]FOR a = 1 to 10 PRINT a NEXT a [/code] When you run this with F5 in QB, the program will PRINT all numbers from 1 to 10. You give a variable a value, of 1 to 10, and because it, and PRINT a command, are in between FOR <> NEXT, it loops until it gets to 10 by doing NEXT a--or, "move tothe next value of the variable a". Then it stops at 10. You can call the variable anything you want, it doesn't have to be "a", I just used it because it avoids confusion for the moment. You can also change the values to any numerical whole number, such as "66 to 104303", or "1 to 2". You can also do it like this [code]FOR a = 1 to 10 PRINT"Rawr, I'll chose a number between 1 and 10!"; a NEXT a [/code] This simply adds a comment into the code, making it look fancy, and stuff. ---- 3.0--STEP This is an alternative way to use the FOR <> NEXT. It changes the increments by using STEP--if you use the STEP command in conjunction with the FOR <> NEXT command, it increases the variable (in this case, a) by however many increments you desire. [code]FOR a = 1 to 100 STEP 2 PRINT a NEXT a [/code] This code, when run with F5 again, PRINTs all the numbers from 1 up to 100, in increments of 2. That is, evenly, or, even numbers only--2, 4, 6, 8, and so on. Since this PRINTs numbers forwards to 100, you can also do it backwards, or descending [code]FOR a = 100 to 1 STEP 2 PRINT a NEXT a [/code] does just the opposite: 100 down to 1, in increments of 2--100, 98, 96, 94, etc etc. And that's that for FOR <> STEP <> NEXT. Goof around with it and become accustomed to it further. ---- 4.0--GOTO Another loop-type command is this, GOTO. It is a fun command that let's you split up your program into smaller parts. In order to use this command, you label the lines that you want to go to, or loop back too--hence, GOTO--with numbers. [code]1 CLS PRINT"The GOTO command is a (theoretically) infinite loop" GOTO 1 [/code] By labelling CLS with 1, we can then make the program run back on itself, using GOTO 1, thus starting from the start--and thus, creating a (theoretically) infinite loop. Simple enough? ---- 5.0--IF <> THEN For what reason would one wish to GOTO and create a loop? For a choice, an option--to give the user something to do :) This is where IF <> THEN comes in--giving choices in the program. GOTO is fun by itself, but not much else. It becomes very useful, though, when used in conjunction with IF <> THEN statement, which allows logical progression of the program, based on a condition. IF <> THEN is exactly that--IF this is "true", THEN do "this". This can be used for many things, including a "menu" or a simple "game". [code]PRINT"Hello. Welcome to Testing IF <> THEN Program, by Moi." PRINT PRINT"Press 1 to continue, Press 2 to clear the screen, or Press 3 to say Hello to the world" PRINT"What would you like to do"; choice IF choice = 1 THEN GOTO cont IF choice = 2 THEN GOTO clrscrn IF choice = 3 THEN GOTO hihi cont: PRINT"Ok, the demo is done" END clrscrn: CLS PRINT" Ok, the demo is done" END hihi: PRINT"HELLO WORLD! K thx" END [/code] If you wanted to, you can utilise any QB command into this type of program. It gets more fun that way, and more complex. This is why QB is good, it simply executes the program in logical order :) If you wanted to, you can use LET, PRINT, INPUT, or mathematical equations into a IF <> THEN program. The following I found somewhere, thought it would be cool to use as an example: [code] PRINT "Program Example #1" PRINT "Try to guess the number I am thinking of between 1 and 10." PRINT "You get 3 chances." INPUT "First chance"; number IF number = 3 THEN GOTO gotit PRINT "Sorry!" INPUT "Second chance"; number IF number = 3 THEN GOTO gotit PRINT "Ooh!" INPUT "Last chance"; number IF number = 3 THEN GOTO gotit PRINT "Sorry! The number was 3!" END gotit: PRINT "You win! Good job!" [/code] You can change the GOTO after the THEN.. statement to any valid QBasic command, like LET, PRINT, or INPUT. Or, you can replace the "=" with any mathematical symbol (like greater than(>), less than(<), or not equal(<>)) Here's a couple of examples in one program: PRINT "Program Example #1" PRINT "Try to guess the number I am thinking of between 1 and 10." PRINT "You get 3 chances." INPUT "First chance"; number IF number = 3 THEN GOTO gotit PRINT "Sorry!" INPUT "Second chance"; number IF number = 3 THEN GOTO gotit PRINT "Ooh!" INPUT "Last chance"; number IF number = 3 THEN GOTO gotit PRINT "Sorry! The number was 3!" END gotit: PRINT "You win! Good job!" It is a basic, simple to make and use guessing game. You can make more complex, indepth versions of this, and more, just by using IF <> THEN and the other commands you've read about so far. And that's pretty much all for IF <> THEN. Mess around with it, create some small programs, and just get used to it. ---- 6.0--COLOUR This isn't really much at all, just to show you ways to use colour in your programs for your text. It makes things look a lot nicer than just black and white, and can be used to coordinate certain parts of the program. There are 16 colours in total. They consist of two numerals, 00 - 15, as the code. 00 - black 01 - dark blue 02 - dark green 03 - dark cyan 04 - dark red 05 - dark purple 06 - orange/brown 07 - gray 08 - dark gray 09 - light blue 10 - light green 11 - light cyan 12 - light red 13 - magenta 14 - yellow 15 - "bright" white Now, to use these, the code is very simple: [code] COLOR 04 PRINT"This turns up "dark red" PRINT PRINT"For the coding itself, spell colour COLOR (american way)" PRINT"Not my Australian/English way, "colour"" PRINT COLOR 09 PRINT"Now it's light blue..." PRINT PRINT"So yes, ignore my English spelling, it'll work for QB better" END [/code] So that's very simple to use. I added it because it makes shit look nice, otherwise does nothing of great importance. ---- 7.0--Next, and final, QB Guide (part 3) I will explain how to use DO <> LOOP, INT, RANDOMISE TIMER, SELECT, END SELECT, OPEN, CLOSE, RND, INPUT#, PRINT #, and INKEY # That is all I plan to do on QB. I'll have another C++ article, some social issue articles, and a planned explanation of the "number" system. ---- (c) Infinite, The Goon Squad (TGS), April 2004. http://www.TGS-Security.com Any questions, comments, shit like that, contact me at clientside@hotmail.com, or SystemAdmin@spymac.com