Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

QBASIC Tutorial

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8
At a glance
Powered by AI
The document provides an introduction to basic commands and concepts in QBASIC like PRINT, INPUT, mathematical operators, and conditional statements.

The PRINT command displays text or values on the screen.

Common mathematical operators in QBASIC include + for addition, - for subtraction, * for multiplication, / for division, ^ for exponents, SQR for square root, and MOD for modulo.

QBasic Tutorial

If you haven't already done so download the QB64 compiler from here
: http://www.qb64.net/. Select your operating system and install.

Your First Program

Try typing this into the qbasic compiler (It's a blue window) :

Print "Hello World"

Now, press 'F5' to run the program (Or go to the 'run' menu and click start)

What happened? It probably displayed the message 'Hello World' on the screen right?

Commands

PRINT
The first command you're going to learn about is the print command. Basically
what 'Print' does is tell Qbasic to write something to the screen. In the program above,
Qbasic printed the message "Hello World".

*HINT*
Instead of typing print into the blue window/compiler, just type ? "Hello World".
When you press enter/return, the '?' will be changed to PRINT
*HINT*

You can also print variables onto the screen.

Try this :

x=1
Print x

Now QBasic will print '1'.

You can also type multiple things, separated by semicolons (;).

Try this :

x=1
? "Hello World" ; x

Now, that will print 'Hello World 1'!

MATHEMATICAL OPERATORS

+ : adds two values


- : subtracts the second value from the first
* : (That's an asterisk) multiplies two values
/ : divides the first value by the second
^ : raises the first number by the second number. 3^2 would be 3 raised to 2, or 9
SQR(num) : Takes the square root of a number
(num) MOD (num2) : Gives you the remainder when you divide num by num2

REM
Rem doesn't actually do anything. It stands for remark and is used to add comments to
the code. The compiler COMPLETELY IGNORES anything typed after REM.

Try this

REM Print "Hello World"

Now, when you press 'F5', nothing happens.

So why do we need REM?

REM is used to put comments for other programmers to see.

For example :

REM Prints a message


Print "Hello World"

Now, just by looking at the REM statement, other programmers would know what the
program does, without having to read the code.

You can use the apostrophe (') as a shortcut for REM

For example :

? "Hello World" ' This is a comment

INPUT
What use is a computer if you can't tell it what to do?

That is where the Input command comes in handy!

With Input, you can ask the user to enter a value. Just like print, you can type many
things separated by semicolons (;).

Try this :

Input "Enter your name"


Now Press 'F5'. What happened? Probably Nothing! (Don't worry, you didn't type it in
wrong). That's because when you INPUT a value, you need to tell Qbasic where to put
it!

Try this now :

Input "Enter your name" ; name$ 'Whatever the user types is put in the variable
name$
Print name$

Run it and see what happens!

It probably printed whatever you typed!

IF...THEN...ELSE

If you've taken any science classes you've probably been taught to write your
hypotheses in "IF...THEN...BECAUSE" for. In QBASIC, the format is this :

IF (condition is met) THEN (do this task) ELSE (otherwise do this. NOTE : THE
"ELSE" PART IS OPTIONAL, THE "THEN" PART IS NOT!!)

These are the statements usually used in the (condition is met) part

> (greater than) : If the first value is more than the second, the condition IS met
otherwise it is NOT
< (less than) same as above except if the first value is LESS than the second, the
condition IS met otherwise it is NOT
>=(greater than OR equal to) if the first value is greater than or equal to the
second, the condition IS met otherwise it is NOT
<= (less than OR equal to) if the first value is less than the second, the condition
IS met otherwise it is NOT
= (equal to) (I think this one is pretty obvious)
<>(NOT equal to) (again, its quite obvious)

For example if I wrote the following :

Input "Enter a number"; num


IF num > 5 THEN Print "Your number is more than 5" ELSE print "Your number is less
than or equal to 5"

More conditionals

In an IF...THEN statement you can have multiple conditions."How" you say? I'll show
you...

OR

The OR statement tells QBASIC to do the action after "THEN" if either of the
conditions are met.

For example,

Input "Enter a number";num

If num>10 OR num <5 then print "That's a good number" 'If num is more than 10 OR
num is less than 5 then it will print the message

AND

The AND statement tells QBASIC to do the action after "THEN" if ALL of the
conditions are met.

For example,

Input "Enter a number";num


Input "Enter another number"; num2

If num>10 AND num2 <5 then print "That's a good number" 'If num is more than 10
AND num2 is less than 5 then it will print the message.

NOT

The NOT statement tells QBASIC to do the action after "THEN" if a condition is NOT
met.

For example,

Input "Enter a number";num

If NOT(num>5) then print "num is NOT greater than 5" ' If num is NOT greater than
5, then it will print the message
THERE ARE MORE CONDITIONAL, BUT FOR NOW, THESE ARE THE
IMPORTANT ONES. Some of the other conditionals include : XOR, NAND
(basically NOT + AND) , and NOR (basically NOT + OR).

Modifying a String

Modifying strings is very useful in a number of places. I'll show you some basic
commands which modify strings.

LEFT$

This command will take the left part of a string. For example, if i created a string called
str$ and stored "Hello,I am" in it and I wrote the following command :

modified$ = left$(str$,5)

modified$ would now be equal to the first 5 characters of the string "str$", which
means, modified$ would be "Hello".

RIGHT$

RIGHT$ is just like LEFT$ except it takes part of the string starting from the right.

In the previous example, if i had written :

modified$ = right$(str$,5)

modified$ would now be ",I am". (Spaces count as characters too!)

*Just a thought*
What would you do if you wanted to take the middle part of the string?

In the previous example with the string "Hello,I am" what would I do if I only wanted
the "o,I" part of the string?

I would take the left half of the string "o,I am"

But how would I get the string "o,I am"?

I would take the right part of the full string.

For example I could do this :

' By the way, remember comments? Anything after an apostrophe is a comment.


This is a comment!
temporary$ = right$(str$,6) 'Now temporary$ = "o,I am"

modified$ = left$(temporary$,3) ' Now modified$ = "o,I"

That's just one way to do it. Another way would be this :

modified$ = left$(right$(str$,6),3) ' Basically, I combined the two statements into


one. This will take the 3 letters on the left OF the 6 letters on the right

BUT WAIT! *drum roll please* THERE'S AN EVEN BETTER WAY TO DO THIS!
CHECK THE NEXT COMMAND!

*Just a thought*

MID$ (THIS IS THE NEXT COMMAND)

With mid$ you can take the middle of a string by specifying where you want to start
from and how many letters you want.

With the previous example (I will use a new example for the next command!) if I
wanted the string "o,I" I would do this :

modified$ = mid$(str$,5,3) ' This tells QBASIC to start at character 5 (which is "o")
and take 3 total characters ("o,I"). MUCH EASIER THAN USING RIGHT$ AND
LEFT$!

UCASE$

Have you ever wanted to make plain text seem like shouting? (It's alright,neither have I)

That's what UCASE$ is for!!

UCASE$ tells QBASIC to capitalize each letter in a string.

For example :

Print ucase$("I am not shouting right now. ")

This would print "I AM NOT SHOUTING RIGHT NOW."

LCASE$
Are you annoyed when people type in all caps?

If so, then LCASE$ is for you!

LCASE$ tells QBASIC to make every letter in a string lower case.

For example,

Print lcase$("I AM SHOUTING RIGHT NOW")

This would print "i am shouting right now".

*WAIT*
You might be thinking "Why would I ever need to use these commands?"

The answer to that is simple. Lets say that I wrote the follow program :

input "Type 'y' for yes or 'n' for no";yn$

if yn$ = "y" then print "you said YES" else print "you said NO"

At first glance, the program seems to work well.

BUT, what if the user entered capital letter "Y" instead of "y"?

Let's change the previous program to this :

input "Type 'y' for yes or 'n' for no";yn$

if LCASE$(yn$) = "y" then print "you said YES" else print "you said NO"

Now, it works even if the user types "Y" instead of "y"

*WAIT*

LOOPS

In most programs, we need to be able to do the same thing over and over again. For
example, if I wanted to make a program which tells me the square root of the number
entered by a user, I would want it to keep going until I closed the program. Loops allow
us to do this. There are two main types of loops in most programming languages : The
FOR...NEXT loop and the DO...LOOP. In QBASIC there are also modifications of the
DO...LOOP, which we'll discuss later.

*THERE WILL BE MORE POSTED TOMORROW. IF YOU HAVE ANY


QUESTIONS POST A COMMENT IS THE QUESTIONS AND COMMENTS
SECTION*

You might also like