basic programming basics - qb64 wiki

Download BASIC Programming Basics - QB64 Wiki

If you can't read please download the document

Upload: mazhari

Post on 24-Nov-2015

311 views

Category:

Documents


3 download

DESCRIPTION

programming basuc

TRANSCRIPT

BASIC Programming Basics - QB64 WikiBASIC Programming Basics From QB64 Wiki Jump to: navigation, search This is a brief introduction to programming, and especially BASIC programming. You should read the tutorial Getting started first as this shows you how to setup QB64 and how to write a simple program (which you will need to do in order to test the examples here) In the Community of QB64 we are used to programming, so we might not always appreciate the difficulties of learning for those that are new to the concept. This Tutorial will try to explain to the best of my efforts basic concepts of programming and programming in BASIC (apologies if it seems to be written to a 3-year old because of this, but I want to cover as many obstacles as I can find and it's my impression that it is a good way to learn)

ABOUT PROGRAMMING: Programming is a way for you to tell the computer what to do. The computer is your "slave" and has to do what you command it to do. For the computer to know what to do you have to give it instructions in a way that the computer can understand.

ABOUT PROGRAMMING INSTRUCTIONS: Now, the instructions that the computer can understand are very complex and non-intuitive, so languages were developed to make instructions easier, in one BASIC instruction there can be many instructions accomplished that you don't need to worry about. Even Assembly is a set of instructions to make the computer instructions easier, but BASIC is much easier than assembly.

ABOUT BASIC: BASIC stands for Beginners All-purpose Symbolic Instruction Code. So it's made for beginners, and you can use it for whatever need you have, hence all-purpouse, there are other languages that have beginners in mind, but one part of the philosophy of BASIC is that even though it is simple it can do many advanced tasks as well, to keep you inspired and to help you build the projects that you want to. Many languages buildt for beginners only makes you do certain tasks and limits your freedom. BASIC does just the opposite, it tries NOT to limit you, it trusts you to know that you shouldn't take the KILL instruction lightly (as it will erase files from the hard disk), and when you finally need the KILL instruction and have learned how to use it then you are free to do so!

ABOUT THE BASIC INSTRUCTIONS: The BASIC instructions are often called "Statements", or "Commands", in the wiki and in many situations they can also be called "Keywords" so you might want to

remember that as well, "statements" are used in this tutorial though from now on (and bolded so that it repeats in your head :) ). The statements have names that resembles the english language, like FOR, NEXT, IF, THEN, GOTO (go to), etc., so when you have learned how they work they are very easy to remember. Some statements are shortened so that it is easier to type (like CLS - CLear Screen), but once you learned them you are instantly reminded of their use if you look at the name. Read more about statements here! Many instructions have arguments that lets you put information into the instruction.

ABOUT ARGUMENTS: Arguments are the information that you give to a statement in order for the statement to do as you want, a argument can be the text to display to the screen, or what color a dot on the screen should have. In real life you could give someone an instruction to paint your car, the argument would be which color the car should be - or even if it is the car that should be painted or the house (so it depends on the statement what arguments that goes with it). There are also statements without arguments, and statements where some arguments are optional, you can read about that in the Argument page.

ABOUT VARIABLES: One important part of programming are variables, the the fact that they can change, a variable can hold a then you can change the number to whatever you want. name you want except for BASIC statements (since the statement and not a variable). 'x' can be a variable, now we can tell 'x' to be whatever number we want, if we want it to be '10' then we just type this: x = 10 'x' equals 10, or if we type this: x = 10 + 1 'x' equals 10 + 1, so now 'x' equals 11. So we want, money Which can tell 'x' to be any number we want. 'x' can of course be any name you you can for example name it 'money' instead, like: = 1000 makes 'money' equal 1000. name variables comes from number for example, and The variable can have any computer will think it is a

For variables to be useful we need something to display them, BASIC has a statement for this of course, and it is named 'PRINT', PRINT displays what you want to the screen. So we can type this:

money = 1000 PRINT money Now the value that 'money' holds is displayed to the screen. If you want PRINT to display text you need to have the text in quotation (") so that it isn't being confused with variables. So we can type this: PRINT "Hello" This will display the text "Hello" to the screen. You may want to change color of the text, the COLOR statement does that, there are 16 colors to from (0 to 15) and each color is a number (4 is red for instance), so if marks the choose we want

the text to be red we type this: COLOR 4 PRINT "Hello" Sometimes we would like to display both text and the value of a variable, then we have to seperate the text and the variable by a semicolon ";" like this: money = 1000 PRINT "Money:"; money This will display "Money: 1000" to the screen.

ABOUT TEXT VARIABLES: In order to differ ordinary value variables from text variables we use a $ after the variable. Like values we can set a text variable to be whatever text we want, be sure to use quotation marks for the program to know it isn't a variable ("), like this: myname$ = "Arnold" You can also add text and display it, like this: myname$ = "Arnold" + " Schwarzenegger" PRINT myname$ Of course you can also combine everything, like this (CLS clears the screen): CLS money = 1000 myname$ = "Arnold" + " Schwarzenegger" PRINT "My name is: "; myname$; " and I have"; money; " dollars." The text "Arnold Schwarzenegger" can of course be contained in a single text string, like this: myname$ = "Arnold Schwarzenegger" Text variables are often called "strings".

ABOUT INPUT: Sometimes we would want the user of our program to be able to set a variable by himself, this can be accomplished by using the statement INPUT, like this: INPUT "Your name: "; myname$ This will display "Your name: " on the screen while waiting for the user to input his name. You can later display it on the screen using PRINT, like this: CLS INPUT "Your name: "; myname$ INPUT "How much money do you have?: "; money

PRINT "Your name is: "; myname$; " and you have"; money; " dollars!"

ABOUT COMPARING VALUES: IF is a very important statement, it compares your variable to a certain condition and if that condition comes true then you can tell the computer what to do next. For example, if the user has less than 10 dollars then you might want to reply that he is poor, and if the user has more than 1000 dollars then you might want to reply that he is rich. Like this: IF money < 10 THEN PRINT "You are poor." IF money > 1000 THEN PRINT "You are rich." < is less than and > is more than. What if the user has between 10 and 1000 in money? Then we can do like this: IF money > 10 AND money < 1000 THEN PRINT "You are neither rich nor poor." When using AND both conditions has to be true, money has to be more than 10 and money has to be less than 1000. But what if money equals 10 or 1000? Well, we could make a new condition like this: IF money = 10 OR money = 1000 THEN PRINT "You are neither rich nor poor." When using OR either condition can be true and it will PRINT "You are neither rich nor poor.", however it isn't necessary to have a new condition, instead you could just do it like this: IF money >= 10 AND money = is more than or equal to, 1000 THEN PRINT "You are rich." IF money >= 10 AND money