Difference between revisions of "BASIC Programming Basics"

From QB64 Wiki
Jump to navigation Jump to search
imported>Cyperium
imported>Cyperium
Line 63: Line 63:
'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' 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:


 
{{CodeStart}}
:x = 10
x = 10
{{CodeEnd}}




'x' equals 10, or if we type this:
'x' equals 10, or if we type this:


 
{{CodeStart}}
:x = 10 + 1
x = 10 + 1
{{CodeEnd}}




Line 78: Line 80:
So we can tell 'x' to be any number we want. 'x' can of course be any name you want, you can for example name it 'money' instead, like:
So we can tell 'x' to be any number we want. 'x' can of course be any name you want, you can for example name it 'money' instead, like:


{{CodeStart}}
money = 1000
{{CodeEnd}}


:money = 1000
Which makes 'money' equal 1000.
 
 
Which makes 'money' equal 1000 (dollars perhaps?)




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:
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:


 
{{CodeStart}}
:money = 1000
money = 1000
:[[PRINT]] money
{{Cl|PRINT}} money
 
{{CodeEnd}}


Now the value that 'money' holds is displayed to the screen.
Now the value that 'money' holds is displayed to the screen.
Line 97: Line 99:
If you want PRINT to display text you need to have the text in quotation marks (") so that it isn't being confused with variables. So we can type this:
If you want PRINT to display text you need to have the text in quotation marks (") so that it isn't being confused with variables. So we can type this:


 
{{CodeStart}}
:[[PRINT]] "Hello"
{{Cl|PRINT}} "Hello"
 
{{CodeEnd}}


This will display the text "Hello" to the screen. You may want to change the color of the text, the [[COLOR]] statement does that, there are 16 colors to choose from (0 to 15) and each color is a number (4 is red for instance), so if we want the text to be red we type this:
This will display the text "Hello" to the screen. You may want to change the color of the text, the [[COLOR]] statement does that, there are 16 colors to choose from (0 to 15) and each color is a number (4 is red for instance), so if we want the text to be red we type this:


:[[COLOR]] 4
{{CodeStart}}
:[[PRINT]] "Hello"
{{Cl|COLOR}} 4
 
{{Cl|PRINT}} "Hello"
{{CodeEnd}}


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:
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:


 
{{CodeStart}}
:money = 1000
money = 1000
:[[PRINT]] "Money:"''';''' money
{{Cl|PRINT}} "Money:"''';''' money
 
{{CodeEnd}}


This will display "Money: 1000" to the screen.
This will display "Money: 1000" to the screen.
Line 124: Line 127:
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:
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:


 
{{CodeStart}}
:myname$ = "Arnold"
myname$ = "Arnold"
 
{{CodeEnd}}


You can also add text and display it, like this:
You can also add text and display it, like this:


 
{{CodeStart}}
:myname$ = "Arnold" + " Schwarzenegger"
myname$ = "Arnold" + " Schwarzenegger"
:[[PRINT]] myname$
{{Cl|PRINT}} myname$
 
{{CodeEnd}}


Of course you can also combine everything, like this (CLS clears the screen):
Of course you can also combine everything, like this (CLS clears the screen):




:[[CLS]]
{{CodeStart}}
:money = 1000
{{Cl|CLS}}
:myname$ = "Arnold" + " Schwarzenegger"
money = 1000
:[[PRINT]] "My name is: "; myname$; " and I have"; money; " dollars."
myname$ = "Arnold" + " Schwarzenegger"
 
{{Cl|PRINT}} "My name is: "; myname$; " and I have"; money; " dollars."
{{CodeEnd}}


The text "Arnold Schwarzenegger" can of course be contained in a single text string, like this:
The text "Arnold Schwarzenegger" can of course be contained in a single text string, like this:


 
{{CodeStart}}
:myname$ = "Arnold Schwarzenegger"
myname$ = "Arnold Schwarzenegger"
 
{{CodeEnd}}


Text variables are often called "'''strings'''".
Text variables are often called "'''strings'''".
Line 161: Line 165:




:[[INPUT]] "Your name: "; myname$
{{CodeStart}}
 
{{Cl|INPUT}} "Your name: "; myname$
{{CodeEnd}}


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:
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:


 
{{CodeStart}}
:[[CLS]]
{{Cl|CLS}}
:[[INPUT]] "Your name: "; myname$
{{Cl|INPUT}} "Your name: "; myname$
:[[INPUT]] "How much money do you have?: "; money
{{Cl|INPUT}} "How much money do you have?: "; money
:[[PRINT]] "Your name is: "; myname$; " and you have"; money; " dollars!"
{{Cl|PRINT}} "Your name is: "; myname$; " and you have"; money; " dollars!"
 
{{CodeEnd}}




Line 178: Line 183:




[[IF...THEN|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.
{{KW|IF...THEN|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:
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:


 
{{CodeStart}}
:[[IF...THEN|IF]] money < 10 [[THEN]] [[PRINT]] "You are poor."
{{Cl|IF...THEN|IF}} money < 10 {{Cl|THEN}} {{Cl|PRINT}} "You are poor."
:[[IF...THEN|IF]] money > 1000 [[THEN]] [[PRINT]] "You are rich."
{{Cl|IF...THEN|IF}} money > 1000 {{Cl|THEN}} {{Cl|PRINT}} "You are rich."
 
{{CodeEnd}}


< is less than and > is more than. What if the user has between 10 and 1000 in money? Then we can do like this:
< is less than and > is more than. What if the user has between 10 and 1000 in money? Then we can do like this:


 
{{CodeStart}}
:[[IF...THEN|IF]] money > 10 [[AND (boolean)|AND]] money < 1000 [[THEN]] [[PRINT]] "You are neither rich nor poor."
{{Cl|IF...THEN|IF}} money > 10 {{Cl|AND (boolean)|AND}} money < 1000 {{Cl|THEN}} {{Cl|PRINT}} "You are neither rich nor poor."
 
{{CodeEnd}}


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:
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:


 
{{CodeStart}}
:[[IF...THEN|IF]] money = 10 [[OR (boolean)|OR]] money = 1000 [[THEN]] [[PRINT]] "You are neither rich nor poor."
{{Cl|IF...THEN|IF}} money = 10 {{Cl|OR (boolean)|OR}} money = 1000 {{Cl|THEN}} {{Cl|PRINT}} "You are neither rich nor poor."
 
{{CodeEnd}}


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:
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:


 
{{CodeStart}}
:[[IF...THEN|IF]] money >= 10 [[AND (boolean)|AND]] money <= 1000 [[THEN]] [[PRINT]] "You are neither rich nor poor."
{{Cl|IF...THEN|IF}} money >= 10 {{Cl|AND (boolean)|AND}} money <= 1000 {{Cl|THEN}} {{Cl|PRINT}} "You are neither rich nor poor."
 
{{CodeEnd}}


>= is more than or equal to, <= is less than or equal to. Ok, so now we might want to remind him that he is broke if money equals 0, like this:
>= is more than or equal to, <= is less than or equal to. Ok, so now we might want to remind him that he is broke if money equals 0, like this:




:[[IF...THEN|IF]] money = 0 [[THEN]] [[PRINT]] "You are broke!"
{{CodeStart}}
 
{{Cl|IF...THEN|IF}} money = 0 {{Cl|THEN}} {{Cl|PRINT}} "You are broke!"
{{CodeEnd}}


So, let's show the program in its full glory!
So, let's show the program in its full glory!


 
{{CodeStart}}
:[[CLS]]
{{Cl|CLS}}
:[[INPUT]] "Your name: "; myname$
{{Cl|INPUT}} "Your name: "; myname$
:[[INPUT]] "How much money do you have?: "; money
{{Cl|INPUT}} "How much money do you have?: "; money
:[[PRINT]] "Your name is: "; myname$; " and you have"; money; " dollars!"
{{Cl|PRINT}} "Your name is: "; myname$; " and you have"; money; " dollars!"
:[[IF...THEN|IF]] money < 10 [[THEN]] [[PRINT]] "You are poor."
{{Cl|IF...THEN|IF}} money < 10 {{Cl|THEN}} {{Cl|PRINT}} "You are poor."
:[[IF...THEN|IF]] money > 1000 [[THEN]] [[PRINT]] "You are rich."
{{Cl|IF...THEN|IF}} money > 1000 {{Cl|THEN}} {{Cl|PRINT}} "You are rich."
:[[IF...THEN|IF]] money >= 10 [[AND (boolean)|AND]] money <= 1000 [[THEN]] [[PRINT]] "You are neither rich nor poor."
{{Cl|IF...THEN|IF}} money >= 10 {{Cl|AND (boolean)|AND}} money <= 1000 {{Cl|THEN}} {{Cl|PRINT}} "You are neither rich nor poor."
:[[IF...THEN|IF]] money = 0 [[THEN]] [[PRINT]] "You are broke!"
{{Cl|IF...THEN|IF}} money = 0 {{Cl|THEN}} {{Cl|PRINT}} "You are broke!"
 
{{CodeEnd}}


Ok, but there is one thing to be added, you see, if money = 0 then it will print both "You are poor." and "You are broke!"...we can make it only print "You are broke!" if money equals 0, we only need to change the first condition (IF money < 10) to this:
Ok, but there is one thing to be added, you see, if money = 0 then it will print both "You are poor." and "You are broke!"...we can make it only print "You are broke!" if money equals 0, we only need to change the first condition (IF money < 10) to this:


 
{{CodeStart}}
:[[IF...THEN|IF]] money < 10 [[AND (boolean)|AND]] money '''<>''' 0 [[THEN]] [[PRINT]] "You are poor."
{{Cl|IF...THEN|IF}} money < 10 {{Cl|AND (boolean)|AND}} money '''<>''' 0 {{Cl|THEN}} {{Cl|PRINT}} "You are poor."
 
{{CodeEnd}}


<> is "not", so if money is less than 10 and money is not 0 then it will print "You are poor.", so when money is 0 then it will only print "You are broke!".
<> is "not", so if money is less than 10 and money is not 0 then it will print "You are poor.", so when money is 0 then it will only print "You are broke!".
Line 236: Line 242:
Let's show the entire program one more time:
Let's show the entire program one more time:


 
{{CodeStart}}
:[[CLS]]
{{Cl|CLS}}
:[[INPUT]] "Your name: "; myname$
{{Cl|INPUT}} "Your name: "; myname$
:[[INPUT]] "How much money do you have?: "; money
{{Cl|INPUT}} "How much money do you have?: "; money
:[[PRINT]] "Your name is: "; myname$; " and you have"; money; " dollars!"
{{Cl|PRINT}} "Your name is: "; myname$; " and you have"; money; " dollars!"
:[[IF...THEN|IF]] money < 10 [[AND (boolean)|AND]] money <> 0 [[THEN]] [[PRINT]] "You are poor (or you owe money)."
{{Cl|IF...THEN|IF}} money < 10 {{Cl|AND (boolean)|AND}} money <> 0 {{Cl|THEN}} {{Cl|PRINT}} "You are poor (or you owe money)."
:[[IF...THEN|IF]] money > 1000 [[THEN]] [[PRINT]] "You are rich."
{{Cl|IF...THEN|IF}} money > 1000 {{Cl|THEN}} {{Cl|PRINT}} "You are rich."
:[[IF...THEN|IF]] money >= 10 [[AND (boolean)|AND]] money <= 1000 [[THEN]] [[PRINT]] "You are neither rich nor poor."
{{Cl|IF...THEN|IF}} money >= 10 {{Cl|AND (boolean)|AND}} money <= 1000 {{Cl|THEN}} {{Cl|PRINT}} "You are neither rich nor poor."
:[[IF...THEN|IF]] money = 0 [[THEN]] [[PRINT]] "You are broke!"
{{Cl|IF...THEN|IF}} money = 0 {{Cl|THEN}} {{Cl|PRINT}} "You are broke!"
 
{{CodeEnd}}


I also added "(or you owe money)" as the user might enter a negative value (one that is < 0 like -10) and this program won't cover that situation, to practice you could try to find a way to cover that.
I also added "(or you owe money)" as the user might enter a negative value (one that is < 0 like -10) and this program won't cover that situation, to practice you could try to find a way to cover that.
Line 258: Line 264:




*[[SCREEN (statement)|SCREEN]] - Sets a different screenmode so that you can use graphics.
*{{KW|SCREEN (statement)|SCREEN}} - Sets a different screenmode so that you can use graphics.
*[[PSET]] - '''P'''ixel '''SET''', which sets a pixel anywhere on the screen in any color.
*{{KW|PSET}} - '''P'''ixel '''SET''', which sets a pixel anywhere on the screen in any color.
*[[POINT]] - Which looks at a point on the screen and gives what color that point is.
*{{KW|POINT}} - Which looks at a point on the screen and gives what color that point is.
*[[CIRCLE]] - Which draws a circle anywhere on the screen, in any size and any color.
*{{KW|CIRCLE}} - Which draws a circle anywhere on the screen, in any size and any color.
*[[LINE]] - Which draws a line between two points on the screen, in any color.
*{{KW|LINE}} - Which draws a line between two points on the screen, in any color.




We start with SCREEN, as we have to change the screenmode from text to graphics. There are many different screenmodes but we start with screenmode 13 (which is 320*200 and 256 colors). We simply type this:
We start with SCREEN, as we have to change the screenmode from text to graphics. There are many different screenmodes but we start with screenmode 13 (which is 320*200 and 256 colors). We simply type this:


 
{{CodeStart}}
:[[SCREEN (statement)|SCREEN]] 13
{{Cl|SCREEN (statement)|SCREEN}} 13
 
{{CodeEnd}}


Now we are in screenmode 13, which can be used to display graphics, you have to have that line in the program for any of the following statements to work.
Now we are in screenmode 13, which can be used to display graphics, you have to have that line in the program for any of the following statements to work.
Line 276: Line 282:
A good place to start when displaying graphics is PSET, it draws a pixel on the screen (a pixel is a point on the screen that is lit and can be in any color), in order for the computer to know where to draw the pixel we need to give it coordinates, the x coordinate is the horizontal coordinate <------> and y is the vertical coordinate (up/down), so we can do like this:
A good place to start when displaying graphics is PSET, it draws a pixel on the screen (a pixel is a point on the screen that is lit and can be in any color), in order for the computer to know where to draw the pixel we need to give it coordinates, the x coordinate is the horizontal coordinate <------> and y is the vertical coordinate (up/down), so we can do like this:


 
{{CodeStart}}
:[[PSET]] (50, 100)
{{Cl|PSET}} (50, 100)
 
{{CodeEnd}}


Where 50 is the x-coordinate (left-to-right) and 100 is the y-coordinate (up-to-down). Notice that we didn't need any argument for color as it will just assume the current text color when there is no argument, so we might want to find out what color it is. We can see that it is white, but what value is that? Well, let's find out using POINT, it looks at a screen coordinate and gives what color it is, we let a variable named 'col' store the value (as we can't use 'color' since that is a statement):
Where 50 is the x-coordinate (left-to-right) and 100 is the y-coordinate (up-to-down). Notice that we didn't need any argument for color as it will just assume the current text color when there is no argument, so we might want to find out what color it is. We can see that it is white, but what value is that? Well, let's find out using POINT, it looks at a screen coordinate and gives what color it is, we let a variable named 'col' store the value (as we can't use 'color' since that is a statement):


 
{{CodeStart}}
:[[PSET]] (50, 100)
{{Cl|PSET}} (50, 100)
:col = [[POINT]] (50, 100)
col = {{Cl|POINT}} (50, 100)
:[[PRINT]] col
{{Cl|PRINT}} col
 
{{CodeEnd}}


And it prints 15 in bright white! We might want to change color to...let's say red. I already know that the value for red is 4, so we can do like this:
And it prints 15 in bright white! We might want to change color to...let's say red. I already know that the value for red is 4, so we can do like this:


 
{{CodeStart}}
:[[PSET]] (50, 100), '''4'''
{{Cl|PSET}} (50, 100), '''4'''
 
{{CodeEnd}}


The third argument is color. So now the pixel will display in red instead of white. We can point it to see the value:
The third argument is color. So now the pixel will display in red instead of white. We can point it to see the value:


 
{{CodeStart}}
:[[PSET]] (50, 100), 4
{{Cl|PSET}} (50, 100), 4
:col = [[POINT]] (50, 100)
col = {{Cl|POINT}} (50, 100)
:[[PRINT]] col
{{Cl|PRINT}} col
 
{{CodeEnd}}


And it should show 4 (in bright white, as setting the color of the pixel doesn't change the text color, the [[COLOR]] statement changes text color).
And it should show 4 (in bright white, as setting the color of the pixel doesn't change the text color, the [[COLOR]] statement changes text color).
Line 307: Line 313:
That should cover PSET, but we have the ability to easily draw a circle on the screen too using the CIRCLE statement, it also deals with x- and y-coordinates, like PSET, but must also have a radius (how big it is from the center), like this:
That should cover PSET, but we have the ability to easily draw a circle on the screen too using the CIRCLE statement, it also deals with x- and y-coordinates, like PSET, but must also have a radius (how big it is from the center), like this:


 
{{CodeStart}}
:[[CIRCLE]] (50, 100), 10
{{Cl|CIRCLE}} (50, 100), 10
 
{{CodeEnd}}


This makes a circle at position 50, 100 and with a radius of 10 pixels (with the current text color as we don't have any color argument), to make the circle red (color 4) we simply add the color argument:
This makes a circle at position 50, 100 and with a radius of 10 pixels (with the current text color as we don't have any color argument), to make the circle red (color 4) we simply add the color argument:


 
{{CodeStart}}
:[[CIRCLE]] (50, 100), 10, 4
{{Cl|CIRCLE}} (50, 100), 10, 4
 
{{CodeEnd}}


That's the circle, there are more arguments, but since this is a beginners tutorial we don't want to complicate things, if you are curious of the other arguments then you can click the link of the statement and read about them there.
That's the circle, there are more arguments, but since this is a beginners tutorial we don't want to complicate things, if you are curious of the other arguments then you can click the link of the statement and read about them there.
Line 322: Line 328:
So what about lines? Well, the LINE statement can make both lines and boxes, I'll show you how, first the line:
So what about lines? Well, the LINE statement can make both lines and boxes, I'll show you how, first the line:


 
{{CodeStart}}
:[[LINE]] (50, 100)-(200, 150)
{{Cl|LINE}} (50, 100)-(200, 150)
 
{{CodeEnd}}


This makes a line from position 50, 100 to position 200, 150 (in the current text color). Then we can make it red (color 4):
This makes a line from position 50, 100 to position 200, 150 (in the current text color). Then we can make it red (color 4):


 
{{CodeStart}}
:[[LINE]] (50, 100)-(200, 150), 4
{{Cl|LINE}} (50, 100)-(200, 150), 4
 
{{CodeEnd}}


Now we can make it a box:
Now we can make it a box:


 
{{CodeStart}}
:[[LINE]] (50, 100)-(200, 150), 4, B
{{Cl|LINE}} (50, 100)-(200, 150), 4, B
 
{{CodeEnd}}


B stands for '''B'''ox, if you don't want the color argument you must do like this...
B stands for '''B'''ox, if you don't want the color argument you must do like this...


 
{{CodeEnd}}
:[[LINE]] (50, 100)-(200, 150),,B
{{Cl|LINE}} (50, 100)-(200, 150),,B
 
{{CodeEnd}}


...to show that there is no color argument (which makes it white if we haven't changed the text color), you can't put 0 there as 0 is black. If we want a filled box then we would simply do like this:
...to show that there is no color argument (which makes it white if we haven't changed the text color), you can't put 0 there as 0 is black. If we want a filled box then we would simply do like this:


 
{{CodeStart}}
:[[LINE]] (50, 100)-(200, 150), 4, BF
{{Cl|LINE}} (50, 100)-(200, 150), 4, BF
 
{{CodeEnd}}


I made it red again too, since I like red...BF stands for '''B'''ox '''F'''illed.
I made it red again too, since I like red...BF stands for '''B'''ox '''F'''illed.

Revision as of 16:38, 24 June 2010

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.

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).



ABOUT VARIABLES:


One important part of programming is variables, the name variables comes from the fact that they can change, a variable can hold a number for example, and then you can change the number to whatever you want. The variable can have any name you want except for BASIC statements (since the computer will think it is a 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 can tell 'x' to be any number we want. 'x' can of course be any name you want, you can for example name it 'money' instead, like:

money = 1000

Which makes 'money' equal 1000.


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 marks (") 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 the color of the text, the COLOR statement does that, there are 16 colors to choose from (0 to 15) and each color is a number (4 is red for instance), so if 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 <= 1000 THEN PRINT "You are neither rich nor poor."

>= is more than or equal to, <= is less than or equal to. Ok, so now we might want to remind him that he is broke if money equals 0, like this:


IF money = 0 THEN PRINT "You are broke!"

So, let's show the program in its full glory!

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!"

Ok, but there is one thing to be added, you see, if money = 0 then it will print both "You are poor." and "You are broke!"...we can make it only print "You are broke!" if money equals 0, we only need to change the first condition (IF money < 10) to this:

IF money < 10 AND money <> 0 THEN PRINT "You are poor."

<> is "not", so if money is less than 10 and money is not 0 then it will print "You are poor.", so when money is 0 then it will only print "You are broke!".


Let'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 added "(or you owe money)" as the user might enter a negative value (one that is < 0 like -10) and this program won't cover that situation, to practice you could try to find a way to cover that.



ABOUT GRAPHICS:


Sooner or later you will want to play around with graphics. As you might have noticed all we have done in this tutorial as of yet is to discuss how to do things with text, you can display text in a graphics mode - but you can't display graphics in a textmode, this section will cover how to setup a graphics mode. Here follows some easy to use graphics statements:


  • SCREEN - Sets a different screenmode so that you can use graphics.
  • PSET - Pixel SET, which sets a pixel anywhere on the screen in any color.
  • POINT - Which looks at a point on the screen and gives what color that point is.
  • CIRCLE - Which draws a circle anywhere on the screen, in any size and any color.
  • LINE - Which draws a line between two points on the screen, in any color.


We start with SCREEN, as we have to change the screenmode from text to graphics. There are many different screenmodes but we start with screenmode 13 (which is 320*200 and 256 colors). We simply type this:

SCREEN 13

Now we are in screenmode 13, which can be used to display graphics, you have to have that line in the program for any of the following statements to work.


A good place to start when displaying graphics is PSET, it draws a pixel on the screen (a pixel is a point on the screen that is lit and can be in any color), in order for the computer to know where to draw the pixel we need to give it coordinates, the x coordinate is the horizontal coordinate <------> and y is the vertical coordinate (up/down), so we can do like this:

PSET (50, 100)

Where 50 is the x-coordinate (left-to-right) and 100 is the y-coordinate (up-to-down). Notice that we didn't need any argument for color as it will just assume the current text color when there is no argument, so we might want to find out what color it is. We can see that it is white, but what value is that? Well, let's find out using POINT, it looks at a screen coordinate and gives what color it is, we let a variable named 'col' store the value (as we can't use 'color' since that is a statement):

PSET (50, 100) col = POINT (50, 100) PRINT col

And it prints 15 in bright white! We might want to change color to...let's say red. I already know that the value for red is 4, so we can do like this:

PSET (50, 100), 4

The third argument is color. So now the pixel will display in red instead of white. We can point it to see the value:

PSET (50, 100), 4 col = POINT (50, 100) PRINT col

And it should show 4 (in bright white, as setting the color of the pixel doesn't change the text color, the COLOR statement changes text color).


That should cover PSET, but we have the ability to easily draw a circle on the screen too using the CIRCLE statement, it also deals with x- and y-coordinates, like PSET, but must also have a radius (how big it is from the center), like this:

CIRCLE (50, 100), 10

This makes a circle at position 50, 100 and with a radius of 10 pixels (with the current text color as we don't have any color argument), to make the circle red (color 4) we simply add the color argument:

CIRCLE (50, 100), 10, 4

That's the circle, there are more arguments, but since this is a beginners tutorial we don't want to complicate things, if you are curious of the other arguments then you can click the link of the statement and read about them there.


So what about lines? Well, the LINE statement can make both lines and boxes, I'll show you how, first the line:

LINE (50, 100)-(200, 150)

This makes a line from position 50, 100 to position 200, 150 (in the current text color). Then we can make it red (color 4):

LINE (50, 100)-(200, 150), 4

Now we can make it a box:

LINE (50, 100)-(200, 150), 4, B

B stands for Box, if you don't want the color argument you must do like this...

LINE (50, 100)-(200, 150),,B

...to show that there is no color argument (which makes it white if we haven't changed the text color), you can't put 0 there as 0 is black. If we want a filled box then we would simply do like this:

LINE (50, 100)-(200, 150), 4, BF

I made it red again too, since I like red...BF stands for Box Filled.



That marks the end of this tutorial!


Note: This tutorial has only scratched the surface of the statements and concepts used. It is meant to get you on the right track, as such many other features of the statements can be learned, at your own pace by following the link to the explanations of the statements. If you are still unsure, stop by this place from time to time as I will add more information for beginners, and if you are unsure about something don't hesitate to stop by at the forums, we (the Community) at qb64.net would be glad to help you!



Navigation:

QB64 Tutorials

Go to Keyword Reference - Alphabetical

Go to Keyword Reference - By usage