About Programming:: Kill Kill
About Programming:: Kill Kill
Programming is a way for you to tell the computer what to o! The computer is your "sla#e" an has to o what you comman it to o! $or the computer to %now what to o you ha#e to gi#e it instructions in a way that the computer can un erstan !
ABOUT BA,I&:
BA,I& stan s for Beginners All+purpose Sym-olic Instruction Co e! ,o it.s ma e for -eginners) an you can use it for whate#er nee you ha#e) hence all+purpouse) there are other languages that ha#e -eginners in min ) -ut one part of the philosophy of BA,I& is that e#en though it is simple it can o many a #ance tas%s as well) to %eep you inspire an to help you -uil the pro/ects that you want to! Many languages -uil t for -eginners only ma%es you o certain tas%s an limits your free om! BA,I& oes /ust the opposite) it tries NOT to limit you) it trusts you to %now that you shoul n.t ta%e the 0I11 instruction lightly 2as it will erase files from the har is%3) an when you finally nee the 0I11 instruction an ha#e learne how to use it then you are free to o so4
ABOUT ARGUM(NT,:
Arguments are the information that you gi#e to a statement in or er for the statement to o as you want) a argument can -e the te*t to isplay to the screen) or what color a ot on the screen shoul ha#e! In real life you coul gi#e someone an instruction to paint your car) the argument woul -e which color the car shoul -e + or e#en if it is the car that shoul -e painte or the house 2so it epen s on the statement what arguments that goes with it3! There are also statements without arguments) an statements where some arguments are optional) you can rea a-out that in the Argument page!
ABOUT 7ARIAB1(,:
One important part of programming are variables) the name #aria-les comes from the fact that they can change) a #aria-le can hol a num-er for e*ample) an then you can change the num-er to whate#er you want! The #aria-le can ha#e any name you want e*cept for BA,I& statements 2since the computer will thin% it is a statement an not a #aria-le3! .*. can -e a #aria-le) now we can tell .*. to -e whate#er num-er we want) if we want it to -e .89. then we /ust type this: x = 10 .*. e:uals 89) or if we type this: x = 10 + 1 .*. e:uals 89 ; 8) so now .*. e:uals 88! ,o we can tell .*. to -e any num-er we want! .*. can of course -e any name you want) you can for e*ample name it .money. instea ) li%e: money = 1000 <hich ma%es .money. e:ual 8999!
$or #aria-les to -e useful we nee something to isplay them) BA,I& has a statement for this of course) an it is name .PRINT.) PRINT isplays what you want to the screen! ,o we can type this: money = 1000 PRINT money Now the #alue that .money. hol s is isplaye to the screen! If you want PRINT to isplay te*t you nee to ha#e the te*t in :uotation mar%s 2"3 so that it isn.t -eing confuse with #aria-les! ,o we can type this: PRINT "Hello" This will isplay the te*t "5ello" to the screen! =ou may want to change the color of the te*t) the &O1OR statement oes that) there are 8> colors to choose from 29 to 8?3 an each color is a num-er 2@ is re for instance3) so if we want the te*t to -e re we type this: COLOR 4 PRINT "Hello" ,ometimes we woul li%e to isplay -oth te*t an the #alue of a #aria-le) then we ha#e to seperate the te*t an the #aria-le -y a semicolon "A" li%e this: money = 1000 PRINT "Money:"; money This will isplay "Money: 8999" to the screen!
Of course you can also com-ine e#erything) li%e this 2&1, clears the screen3:
CLS money = 1000 myname$ = "Arnold" + " Schwarzenegger" PRINT "My name is: "; myname$; " and I have"; money; " dollars." The te*t "Arnol ,chwarCenegger" can of course -e containe in a single te*t string) li%e this: myname$ = "Arnold Schwarzenegger" Te*t #aria-les are often calle "strings"!
ABOUT INPUT:
,ometimes we woul want the user of our program to -e a-le to set a #aria-le -y himself) this can -e accomplishe -y using the statement INPUT) li%e this:
INPUT "Your name: "; myname$ This will isplay "=our name: " on the screen while waiting for the user to input his name! =ou can later isplay it on the screen using PRINT) li%e this: CLS INPUT "Your name: "; myname$ INPUT "How much money do you have?: "; money PRINT "Your name is: "; myname$; " and you have"; money; " dollars!"
IF money < 10 THEN PRINT "You are poor." IF money > 1000 THEN PRINT "You are rich." D is less than an E is more than! <hat if the user has -etween 89 an 8999 in moneyF Then we can o li%e this: IF money > 10 AND money < 1000 THEN PRINT "You are neither rich nor poor." <hen using AN' -oth con itions has to -e true) money has to -e more than 89 an money has to -e less than 8999! But what if money e:uals 89 or 8999F <ell) we coul ma%e a new con ition li%e this: IF money = 10 OR money = 1000 THEN PRINT "You are neither rich nor poor." <hen using OR either con ition can -e true an it will PRINT "=ou are neither rich nor poor!") howe#er it isn.t necessary to ha#e a new con ition) instea you coul /ust o it li%e this: IF money >= 10 AND money <= 1000 THEN PRINT "You are neither rich nor poor." EG is more than or e:ual to) DG is less than or e:ual to! O%) so now we might want to remin him that he is -ro%e if money e:uals 9) li%e this:
IF money = 0 THEN PRINT "You are broke!" ,o) let.s show the program in its full glory4 CLS INPUT "Your name: "; myname$ INPUT "How much money do you have?: "; money PRINT "Your name is: "; myname$; " and you have"; money; " dollars!" IF money < 10 THEN PRINT "You are poor." IF money > 1000 THEN PRINT "You are rich." IF money >= 10 AND money <= 1000 THEN PRINT "You are neither rich nor poor." IF money = 0 THEN PRINT "You are broke!" O%) -ut there is one thing to -e a e ) you see) if money G 9 then it will print -oth "=ou are poor!" an "=ou are -ro%e4"!!!we can ma%e it only print "=ou are -ro%e4" if money e:uals 9) we only nee to change the first con ition 2I$ money D 893 to this:
IF money < 10 AND money <> 0 THEN PRINT "You are poor."
DE is "not") so if money is less than 89 an money is not 9 then it will print "=ou are poor!") so when money is 9 then it will only print "=ou are -ro%e4"! 1et.s show the entire program one more time: CLS INPUT "Your name: "; myname$ INPUT "How much money do you have?: "; money PRINT "Your name is: "; myname$; " and you have"; money; " dollars!" IF money < 10 AND money <> 0 THEN PRINT "You are poor (or you owe money)." IF money > 1000 THEN PRINT "You are rich." IF money >= 10 AND money <= 1000 THEN PRINT "You are neither rich nor poor." IF money = 0 THEN PRINT "You are broke!" I also a e "2or you owe money3" as the user might enter a negati#e #alue 2one that is D 9 li%e +893 an this program won.t co#er that situation) to practice you coul try to fin a way to co#er that!
ABOUT GRAP5I&,:
,ooner or later you will want to play aroun with graphics! As you might ha#e notice all we ha#e one in this tutorial as of yet is to iscuss how to o things with te*t) you can isplay te*t in a graphics mo e + -ut you can.t isplay graphics in a te*tmo e) this section will co#er how to setup a graphics mo e! 5ere follows some easy to use graphics statements:
SCREEN + ,ets a ifferent screenmo e so that you can use graphics! PSET + Pi*el SET) which sets a pi*el anywhere on the screen in any color! POINT + <hich loo%s at a point on the screen an gi#es what color that point is! CIRCLE + <hich raws a circle anywhere on the screen) in any siCe an any color! LINE + <hich raws a line -etween two points on the screen) in any color!
<e start with ,&R((N) as we ha#e to change the screenmo e from te*t to graphics! There are many ifferent screenmo es -ut we start with screenmo e 8H 2which is HI9JI99 an I?> colors3! <e simply type this: SCREEN 13
Now we are in screenmo e 8H) which can -e use to isplay graphics) you ha#e to ha#e that line in the program for any of the following statements to wor%! A goo place to start when isplaying graphics is P,(T) it raws a pi*el on the screen 2a pi*el is a point on the screen that is lit an can -e in any color3) in or er for the computer to %now where to raw the pi*el we nee to gi#e it coor inates) the * coor inate is the horiContal coor inate D++++++E an y is the #ertical coor inate 2upK own3) so we can o li%e this: PSET (50, 100) <here ?9 is the *+coor inate 2left+to+right3 an 899 is the y+coor inate 2up+to+ own3! Notice that we i n.t nee any argument for color as it will /ust assume the current te*t color when there is no argument) so we might want to fin out what color it is! <e can see that it is white) -ut what #alue is thatF <ell) let.s fin out using POINT) it loo%s at a screen coor inate an gi#es what color it is) we let a #aria-le name .col. store the #alue 2as we can.t use .color. since that is a statement3: PSET (50, 100) col = POINT (50, 100) PRINT col An it prints 8? in -right white4 <e might want to change color to!!!let.s say re ! I alrea y %now that the #alue for re is @) so we can o li%e this:
PSET (50, 100), 4 The thir argument is color! ,o now the pi*el will isplay in re instea of white! <e can point it to see the #alue: PSET (50, 100), 4 col = POINT (50, 100) PRINT col An it shoul show @ 2in -right white) as setting the color of the pi*el oesn.t change the te*t color) the &O1OR statement changes te*t color3! That shoul co#er P,(T) -ut we ha#e the a-ility to easily raw a circle on the screen too using the &IR&1( statement) it also eals with *+ an y+coor inates) li%e P,(T) -ut must also ha#e a ra ius 2how -ig it is from the center3) li%e this: CIRCLE (50, 100), 10 This ma%es a circle at position ?9) 899 an with a ra ius of 89 pi*els 2with the current te*t color as we on.t ha#e any color argument3) to ma%e the circle re 2color @3 we simply a the color argument:
CIRCLE (50, 100), 10, 4 That.s the circle) there are more arguments) -ut since this is a -eginners tutorial we on.t want to complicate things) if you are curious of the other arguments then you can clic% the lin% of the statement an rea a-out them there! ,o what a-out linesF <ell) the 1IN( statement can ma%e -oth lines an -o*es) I.ll show you how) first the line: LINE (50, 100)-(200, 150) This ma%es a line from position ?9) 899 to position I99) 8?9 2in the current te*t color3! Then we can ma%e it re 2color @3: LINE (50, 100)-(200, 150), 4 Now we can ma%e it a -o*: LINE (50, 100)-(200, 150), 4, B B stan s for Bo*) if you on.t want the color argument you must o li%e this!!! LINE (50, 100)-(200, 150),,B !!!to show that there is no color argument 2which ma%es it white if we ha#en.t change the te*t color3) you can.t put 9 there as 9 is -lac%! If we want a fille -o* then we woul simply o li%e this: LINE (50, 100)-(200, 150), 4, BF I ma e it re again too) since I li%e re !!!B$ stan s for Bo* Fille !
A ,UB is a part of a program that you can &A11 an the program will /ump to the su- an follow the instructions that is containe within the su-! A su- has to en with (N' ,UB as the program has to %now when to /ump -ac% to where you were when you calle it! ,u-s must -e after all the other co e! =ou can o this for e*ampleA
CALL mysub
SUB mysub PRINT "hello! This is within the sub!" END SUB
There is no limit in LB>@ for how large a ,UB may -e) you can write a entire program within a su-) -ut nothing will happen unless you &A11 it4 As you can see if you clic% the lin% to ,UB there is much more information to learn a-out su-s) -ut this will get you starte ! Now) let.s mo#e on to %*ncti!ns) a $UN&TION is li%e a su- with the only ifference that it can return a #alue 2an you can.t use &A11 to call it3! As with su-s) functions must also -e after the other co e! ,ince functions return a #alue we nee to gi#e that #alue to something) so we.ll start -y gi#ing it to a #aria-le! <e.ll call it .ret. which is short for .return.) we can.t use .return. since that is a reser#e wor 2see: R(TURN3!
ret = afunction
PRINT ret
O%) so ret is 9 as we can see -y PRINTing it! This is -ecause we ha#en.t tol the function what to return 2an the efault is 9 if it is a numerical function or a empty string if it is a string function3! 1et.s ma%e it return something else) you o this -y assigning the function the #alue you want returne ) it might soun complicate -ut it isn.t! This is what you shoul o to gi#e the function a #alue to return:
ret = afunction
PRINT ret
,o when we run this co e ret is ? since the function returne ?! This isn.t #ery useful in its current state) we woul li%e to gi#e #alues to the function so it can return something -ase on the #alues we gi#e! That -rings us to arg*ments which can -e use to gi#e su-s an functions #alues to use within the su- or function! <hether it is a su- or a function arguments are enclose in paranteses with a comma -etween each argument! 1i%e thisA $UN&TION 2argument8) argumentI) argumentH3 (N' $UN&TION Any num-er of arguments nee e can -e use ! 5ere is a e*ample using arguments in a function:
PRINT ret
It prints H9) as 89 ; I9 is H9! <e can gi#e the returne #alue irectly to PRINT if we want) as PRINT accepts returne #alues 2the function acts li%e a argument3! $or e*ampleA
The same co e) -ut we on.t gi#e it to the #aria-le .ret. -ut irectly to PRINT!
=ou on.t actually nee &A11) /ust using the su- name will o) li%e thisA
The only ifference is that I remo#e &A11 which isn.t really nee e -ut can ma%e the co e more clear! Also) notice that when I remo#e &A11 I remo#e the paranteses as well) they are only nee e when using &A11 to call the su- an will cause a error if you use them without &A11! As you can see using arguments is rather easy) an su-s an functions can sa#e you ha#ing to o things o#er an o#er4 More information a-out su-s can -e foun here an more information a-out functions can -e foun here4
Note: This tutorial has only scratche the surface of the statements an concepts use ! It is meant to get you on the right trac%) as such many other features of the statements can -e learne ) at your own pace -y following the lin% to the e*planations of the statements! If you are still unsure) stop -y this place from time to time as I will a more information for -eginners) an if you are unsure a-out something on.t hesitate to stop -y at the forums) we 2the &ommunity3 at :->@!net woul -e gla to help you4