Jump to content
xisto Community
Sign in to follow this  
heaven_master_ash

Qbasic Tutorial By Heaven_master_ash Own

Recommended Posts

Well this Is My Hard Work On Qbasic So Please Don Underasstimate Me I Have Learned Many Thing And How To Describe Them Today I tell U My Experince Of Qbasic In That Way That Every One Can Understand This Its Easy And nice Simple So Concentrate now..

When you open QBasic, you see a blue screen where you can type your program. Let’s begin with the basic commands that are important in any program.

PRINT

Command PRINT displays text or numbers on the screen.

The program line looks like this:

PRINT “My name is Nick.”

Type the bolded text into QBasic and press F5 to run the program. On the screen you’ll see

My name is Nick.

Note: you must put the text in quotes, like this – “text”. The text in quotes is called a string.

If you put the PRINT alone, without any text, it will just put an empty line.

PRINT can also put numbers on the screen.

PRINT 57 will show the number 57. This command is useful for displaying the result of mathematical calculations. But for calculations, as well as for other things in the program, you need to use variables.


Variables

When you think, you keep words or numbers in your mind. This allows you to speak and to make calculations. Qbasic also needs to keep words or numbers in its memory. To do this, you use variables, pieces of Qbasic memory, which can keep information. A variable can be named with any letter, for example – a. It can also have a longer name, which can be almost any word. It is important to know that there are two main types of variables – that keep a number and that keep a word or a string of words.

* Numeric variables. It’s basically variables named with just a letter or a word. You tell this variable to keep a number like this:

a = 15

In other words, you assigned the value 15 to the variable a.

Qbasic will now know that the variable named a keeps the number 15. Now, if you type
PRINT a

and run the program, the computer will show this number on the screen.

* String variables can keep so called “strings”, which is basically any text or symbols (like % or Ł), which you put in the quotes “ ”. You can also put numbers in a string variable, but again, you must include them in quotes, and QBasic will think that those numbers are just a part of text. The string variables look like this – a$. The $ sign tells Qbasic that this variable contains text.

Example:

a$ = “It is nice to see you”
PRINT a$

On the screen you’ll see:
It is nice to see you



The PRINT command can print more that one string on the line. To do this, put the; sign between the variables. For example, you have two variables – name$, which contains name Rob, and age, which contains the number 34. Then, to print both name and age, you type:

PRINT “Name - ”; name$; “. Age - ”; age

As you can see, the name of a variable can be more than just one letter – it can be a short word which describes what sort of information does this variable keep.



What you see on the screen when you run the program will look like this:

Name – Rob. Age – 34

Or, you can type the program like that:
PRINT “Name - ”; name$
PRINT “Age - ”; age

The result is:

Name – Rob

Age - 34


INPUT

INPUT is a command that allows you or anybody else who runs the program to enter the information (text or number) when the program is already running. This command waits for the user to enter the information and then assigns this information to a variable. Since there are two types of variables, the INPUT command may look like this – INPUT a (for a number), or INPUT a$ (for a string).

Example (Type this program into Qbasic and run it by pressing F5)

PRINT “What is your name?”

INPUT name$

PRINT “Hi, ”; name$; “, nice to see you!”

PRINT “How old are you?”

INPUT age

PRINT “So you are ”; age; “ years old!”

END

Note: The END command tells Qbasic that the program ends here.

You don’t have to use PRINT to ask the user to enter the information. Instead, you can use

INPUT “Enter your name”; name$

and the result will be the same.



GOTO

Quite often you don’t want the program to run exactly in the order you put the lines, from the first to the last. Sometimes you want the program to jump to a particular line. For example, your program asks the user to guess a particular number:

~ ~ ~ ~ ‘some of the program here

INPUT “Guess the number”; n

~ ~ ~ ~ ‘some of the program there

The program then checks if the entered number is correct. But if the user gives the wrong answer, you may want to let him try again. So you use the command GOTO, which moves the program back to the line where the question is asked. But first, to show Qbasic where to go, you must “label” that line with a number:

1 INPUT “Guess the number”; n ‘this line is labelled with number 1

Then, when you want the program to return to that line, you type

GOTO 1


You can use GOTO to jump not only back but also forward, to any line you want. Always remember to label that line. You can have more than one label, but in that case they should be different.

QBasic was obviously created for us to have fun, play games, draw nice graphics and even make sounds.

But, as you might guess, nothing good comes without a bit of effort that has to be put in it. In the most QBasic programs a bit of math has to be done.

The math… Doh!

If you hate mathematics, don’t worry. Qbasic will do it all for you, you just need to know how to tell QBasic to do that.



Qbasic can perform the following mathematical operations:

Operator


What it does


Example


Result

+


Add


7 + 2


9

-


Subtract


7 – 2


5

*


Multiply


7 * 2


14

/


Divide


7 / 2


3.5



Examples:

1. a = 15 / 4 + 3

PRINT a

Result on the screen – 6



2. PRINT “Enter the first number”

INPUT a

PRINT “Enter the second number”

INPUT b

c = a + b

d = a * b

PRINT a; “+”; b; “=”; c

PRINT a; “*”; b; “=”; d

END

When you run this program it goes like this:

Computer: Enter the first number

You: 22

Computer: Enter the second number

You: 18

Computer: 22 + 18 = 40

22 * 18 = 396





Advanced operations:

Operator


What it does


Example


Result

\


divides and turns the result into an integer (the whole number)


7 \ 2


3

^


Raises a number to the power of another number


3 ^ 4

(means: 3 * 3 * 3 * 3)

2.5 ^ 3

(means:2.5 * 2.5 * 2.5)


243



15.625

SQR


Calculates the square root of a number


SQR(9)





SQR(16)





SQR(5)


3

(because: 3 ^ 2 = 9)



4

(because: 4 ^ 2 = 16)



2.236

MOD


Divides two numbers, and if the result is not an integer (for example - 3.25), finds out how much to subtract from the first number in order to get the integer result.


17 MOD 5


2

(because: 17 / 5 = 3.4

17 – 2 = 15

15 / 5 = 3)



The following program explains MOD. Type this program (except my comments) into Qbasic accurately and run it to see how MOD works.

1 CLS this command clears the screen, so it’s empty

INPUT "Enter the first number "; a

INPUT "Enter the second number "; b

IF b = 0 THEN checks if the second number is zero, because you can’t divide by zero

PRINT "the second number cannot be 0. Try again."

DO: LOOP WHILE INKEY$ = "" waits for you to press a key to continue

GOTO 1 then sends you back to line 1

END IF

CLS clear the screen again

c = a MOD b

d = a / b

e = a - c

f = e / b

PRINT a; "MOD"; b; "="; c

IF c = 0 THEN this checks if the result of a MOD b = 0, because it means that

the result of a / b is integer

PRINT "because"; a; "/"; b; "="; d; " - integer. Try again."

DO: LOOP WHILE INKEY$ = "" waits for you to press a key to continue

GOTO 1 then sends you back to the line 1

END IF

PRINT "because"; a; "/"; b; "="; d; " -not integer" The rest of the program executes if the

PRINT "but"; a; "-"; c; "="; e result of a / b is not integer

PRINT "and"; e; "/"; b; "="; f; " - integer"

END



This program may look very complicated for you, but don’t worry. Qbasic is a very easy language to learn and soon you’ll be having fun with it. I promise you!

From the previous chapters you have learned how to create a simple program with INPUT, GOTO and PRINT commands. In such a program, you are asked to type the information, QBasic processes it and then shows the result on the screen. In many programs (for example - games), the user has a choice of what to enter. In this case, QBasic has to check what the user has typed, and to react accordingly. This can be done with the IF...THEN command.
IF…THEN…ELSE

This command checks if an argument involving a variable is true. An argument may look like this: IF a = 15 THEN... If the argument is true (and a really equals to 15), then QBasic executes the command you put after the IF...THEN.

Example:

IF a = 15 THEN PRINT "OK"

If the argument is not true (if a is not equal to 15), QBasic bypasses this line and goes to the next. In some cases, you can use the ELSE command, which tells QBasic exactly what to do if the argument is not true.

IF a = 15 THEN PRINT "OK" ELSE PRINT "It's not 15"

This example means that if a equals to 15, the computer will show OK on the screen. But if a is not equal to 15, you'll see

It's not 15

To check the argument in IF…THEN command, you can use any of these mathematical operators:
operator meaning example
= Equal to IF a = 15 THEN…
<> Not equal to IF a <> 15 THEN…
< Less than IF a < 15 THEN…
<= Less or equal to IF a <= 15 THEN
> More than IF a > 15 THEN…
>= More or equal to IF a >= 15 THEN…

You can make QBasic to execute more than one command if the argument is true. To do this, put those commands after IF…THEN and divide them with : symbol.

IF a = 15 THEN PRINT "OK": GOTO 1

This example means that if a equals to 15, QBasic will first print OK and then will go to the line labelled 1. Here is an example of full program (a simple game):


1 CLS
score = 0
PRINT "How many days are there in a week?"
INPUT a
IF a = 7 THEN GOTO 2
PRINT "Wrong answer!"
PRINT "To try again – press 'y'."
INPUT a$
IF a$ = "y" THEN GOTO 1 ELSE END
2 score = 10
PRINT "It's the right answer!"
PRINT "Your score is now"; score; "!"
PRINT "Thanks for playing."
END

Let's analyse how this program works.
The first command, CLS, clears the screen so it's empty. Then QBasic makes the variable score to be equal to 0. Then computer shows the question "How many days there are in a week?" You type your answer (a number) and QBasic puts it in the variable a. Then QBasic checks if the number in this variable equals to 7 (because there are 7 days in a week). If it equals to 7, the program goes to the line 2, where the variable score gets equal to 10. You get the message "It's the right answer! Your score is now 10! Thanks for playing." and then the program ends. But if you gave the wrong answer (that is, the number in the variable a is not 7), QBasic bypasses the line with IF…THEN, and shows the message "Wrong answer! To try again – press 'y'." You can then press the key 'y' to try again or press any other key to end the game. The value of the key you pressed goes to the variable a$, which, if you remember, is a string variable (because of the $ symbol), and can contain only strings (letters, words or symbols). So the program checks if the key you pressed is really "y". If it is, the program takes you back to the line labelled 1, where the screen is cleared and the question is asked again. But if the key you pressed is some other key (not "y"), the program ends.

Sometimes you may want QBasic to execute more than two or three commands if the argument is true. Instead of putting all of them on one line, you can make an IF…THEN block:


IF a$ = "y" THEN
PRINT "OK, let's try again."
score = 0
GOTO 1
END IF

Note the END IF command at the end of this example. It tells QBasic that the commands, which should be executed if the argument is true, end here. This is important to separate the IF..THEN block from the rest of the program by putting END IF.
If you want QBasic to check more than one argument at once, use such words as AND and OR. For example – you want QBasic to execute commands in IF…THEN block if a is more than 12 but less than 50, somewhere in between. To program that, you can type:

IF a > 12 AND a < 50 THEN

Or, if you want commands to be executed if a equals either 6 or 12, you type:

IF a = 6 OR a = 12 THEN

Wow! So much said about that simple IF…THEN command in QBasic. It is indeed simple. IF you practise using this command in your programs, THEN you'll get the hang of it :-)

To make interesting and efficient programs, you can make QBasic to execute a part of a program more than once. This is called looping, when QBasic goes through a part of a program over and over again. This can be done with the GOTO command, but in QBasic there are some good ways to loop the program. One of them is FOR...NEXT command.

FOR...NEXT

This command allows you to execute a part of a program a certain number of times. It looks like this:

FOR i = 1 TO 4
PRINT "I am looping!"
NEXT i

This little stupid program will print on the screen:

I am looping!
I am looping!
I am looping!
I am looping!

The letter i can be anyother letter, c for example. It is actually a variable, which changes its value each time the program loops (in this example - from 1 to 3). So, if you make a program like this:

FOR a = 1 TO 5
PRINT "This is loop number"; a
NEXT a

this will print:

This is loop number 1
This is loop number 2
This is loop number 3
This is loop number 4
This is loop number 5

With FOR...NEXT you can use the STEP command, which tells QBasic how to count from one number to another. If you type:

FOR j = 0 TO 12 STEP 2
~ ~ ~
NEXT j

it will count by two:
0, 2, 4, 6, 8, 10, 12

FOR j = 0 TO 6 STEP 1.5
~ ~ ~
NEXT j

This will count:
0, 1.5, 3, 4.5, 6

You can also count backwards:

FOR d = 10 TO 1 STEP -1
~ ~ ~
NEXT d

When you want QBasic to count backwards, always put STEP -1 (or -whatever)!

DO...LOOP

Imagine that you have a program that works like an ordinary calculator: you enter numbers, QBasic calculates and shows the result, and the program ends. The program may be good, but one problem is that you have to run the program each time you want to calculate!
That’s where the handy DO...LOOP comes in. It’s a block of comands, where the program doesn’t have to loop a certain number of times, like in FOR...NEXT. It can loop indefinitely, while the condition is met (and when it’s not met, the loop stops), or until the condition is met (so, when it’s met, the loop stops). Condition is basically the same as an argument, for example f < 20

Here is an example:

DO
PRINT "Enter a number."
PRINT "When you want to quit, press 0."
INPUT n
r = n / 2
PRINT n; "/2 ="; r
LOOP WHILE n > 0
END

When you run this program, you can enter numbers and get the result as many times as you like. The program loops while numbers you enter are more than 0. Once you’ve entered 0, the program ends. The condition WHILE n > 0 is put by the LOOP command but you can stick it to the DO command, like that:

DO WHILE n > 0
~~~
LOOP

Or you can use the word UNTIL instead, and put it either by DO or LOOP, like that:

DO UNTIL n = 0
~~~
LOOP

All these examples have the same effect: the program loops while numbers you enter are more than 0 (or, you can say - until the number you’ve entered is 0). Then QBasic stops looping and goes to execute commands you put after the DO...LOOP block (if it’s END command, the program just ends).

So far you know that there are string variables (for holding text) and numeric variables (for holding numbers). But numbers can be very different, and in QBasic there are some different types of numeric variables:



Type of a variable


The number it can hold


Example of a number


Example of a variable

INTEGER


A whole number, from -32,767 to 32,767


5


a%

LONG INTEGER


A whole number, from more than -2 billion to more than 2 billion


92,345


a&

SINGLE PRESICION


A number with up to 6 digits after the decimal point.


3.725


a!

DOUBLE PRESICION


A number with up to 15 digits after the decimal point


3.1417583294


a#



As you see, those types of variables have a special symbol as part of their names:

%


INTEGER

&


The SINGLE type of variable is the most widely used in QBasic, and you don’t have to stick the ! symbol to it. If you put a variable without any symbol, QBasic will know that this is a SINGLE PRESICION variable.

LONG

!


SINGLE

#


DOUBLE


1


phew :D This Was A hard Work Well Hope U Like My Research Thats My 2yrs Works Hope Apricate Thanx..

Share this post


Link to post
Share on other sites

Umm even though i am also guilty of this myself... stating that you actually wrote this tutorial after hard work and research or from you learning so u can describe it to us... now thats taking things a bit far since u have just copied this from another website

https://smallbusiness.yahoo.com/
https://smallbusiness.yahoo.com/
https://smallbusiness.yahoo.com/
https://smallbusiness.yahoo.com/
https://smallbusiness.yahoo.com/

dont make the mistake i did and think this is helping users, you could just post the link to the site and they can learn from there... its not worth getting warnings and having ur account banned from posting :D

Share this post


Link to post
Share on other sites

Thats the reason he posted the actual thing in quotes, if it would be his original work, wouldn't he be wanting those points he would get from by not typing/copying the main tutorial in quotes! :D

Share this post


Link to post
Share on other sites

Well this Is My Hard Work On Qbasic So Please Don Underasstimate Me I Have Learned Many Thing And How To Describe Them Today I tell U My Experince Of Qbasic In That Way That Every One Can Understand This Its Easy And nice Simple So Concentrate now..

phew This Was A hard Work Well Hope U Like My Research Thats My 2yrs Works Hope Apricate Thanx..

I dont think posting in quotes covers the fact that he is saying he wrote it himself. IF he had said it was from another source and just qouted parts he wants to specify... u cant just quote the whole lot and say its your own...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.