Poll

Please rate this tutorial...

Good
2 (16.7%)
OK
10 (83.3%)
Needs Revision
0 (0%)
Inappropriate
0 (0%)

Total Members Voted: 12

  • Print

Author Topic: A Tutorial on using the PRINT command.  (Read 256 times)

fatman2021

  • Hero Member
  • *****
  • Posts: 978
  • Lord Jesus Christ, Son of God, have mercy on us.
    • Email
A Tutorial on using the PRINT command.
« on: February 20, 2013, 09:29:47 AM »
Try typing the following exactly as shown:

Code: [Select]
PRINT "QB64"

If you make a typing mistake, use the backspace key to erase the
character immediately to the left of the cursor. You can delete
as many characters as necessary.

Let's see what went on in the example above. First, you instructed
(commanded) the computer to PRINT whatever was inside the quote
marks. By hitting F5 you told the computer to do what you
instructed and QB64 was printed to the screen.

If the computer responded with:

Quote
   Syntax error on current line

ask yourself if you made a mistake in typing, or forgot the quote marks.

The computer is precise and expects instructions to be given in a specific

form.

But don't get worried; just remember to enter things as we present
them in the examples and you'll get along great with QB64.

Remember, you can't hurt the computer by typing on it, and the best
way to learn QB64 is to try different things and see what happens.

PRINT is one of the most useful and powerful commands in the QB64
language. With it, you can display just about anything wish.

Type the following and then press F5:

Code: [Select]
PRINT 12 + 12

What you've discovered is that QB64 is a calculator in its
basic form. The result of "24" was calculated and printed automatically.
In fact, you can also perform subtraction, multiplication, division,
exponentiation, and advanced math functions such as calculating square
roots, etc. And you're not limited to a single calculation on a line, but
more on that later.

Note that in the above form, PRINT behaved differently from the first
example. In this case, a value of result of a calculation in printed, rather than
the exact message you entered because the quote marks were omitted.

The plus sign (+) signals addition: we instructed the computer to print
the result of 12 added to 12. Other arithmetic operations take a similar
form to addition. Remember to always hit enter after typing print
and the calculation.

The press F5 when you are ready to run the program.

To subtract, use the conventional minus (-) sign. Type:

Code: [Select]
PRINT 12 - 9

If you wanted to multiply 12 times 12, use the asterisk (*) to represent
multiplication. You would type:

Code: [Select]
PRINT 12 * 12

Division uses the familiar "/". For example, to divide 144 by 12, type:

Code: [Select]
PRINT 144/12

In a like fashion, you can easily raise a number to a power (this is the
same as multiplying a number by itself a specified number of times).

The "^" signifies exponentiation.

Code: [Select]
PRINT 12 ^ 5

This is the same as typing:

Code: [Select]
PRINT 12 * 12 * 12 * 12 * 12


The last example brings up another important point: many
calculations may be performed on the same line, and they can be of mixed
types.

You could calculate this problem:

Code: [Select]
? 3 + 5 - 7 + 2

Up to this point we've just used small numbers and simple examples.
However, QB64 is capable of more complex calculations.

You could, for example, add a number of large figures together. Try
this, but don't use any commas, or you'll get an error:

Code: [Select]
? 123.45 + 345.78 + 7895.687

That looks fine, but now try this:

Code: [Select]
? 12123123.45 + 345.78 + 7895.687

If you tried to preform some mixed calculations different from the
examples we showed earlier, you might not have gotten the results that
you expected. The reason is that the computer performs calculations in a
certain order.

In this calculation:

20 + 8 / 2

you can't tell whether the answer should be 24 or 14 until you know in
which order to perform the calculations. If you add 20 to 8 divided by 2
(or 4), then the result in 24. But, if you add 20 plus 8 and then divide by
2 the answer is 14. Try the example and see what result you get.

The reason you get 24 is because QB64 performs
calculation left to right according to the foll‬owing:

First:  -   minus sign indicating negative numbers
Second: ^   exponentiation, left to right
Third:  */  multiplication and divisions, left to right
Fourth: +-  addition and subtraction, left to right

Follow along according to the order of precedence, and you will see
that in the above example the division was performed first and then the
addition to get a result of 24.

Make up some problems of your own and see if you can following long
and predict the results according to the rules set down above.

There's also an easy way to alter the precedence process by using
parentheses to set off which operations you want performed first.

For example, if you want to divide 35 by 5-plus-2 you type:

Code: [Select]
? 35 / 5 + 2

you will get 35 divided by 5 with 2 added to the answer, which is not
what you intended at all. To get what you really wanted, try this:

Code: [Select]
? 35 / (5 + 2)

What happens now is that the computer evaluates what is contained
in the parentheses first. If there are parentheses within parentheses, the
innermost parentheses are evaluated first.

Where there are a number of parentheses on a line, such as:

Code: [Select]
? (12 + 9) * (6 + 1)

the computer evaluates them left to right. Here 21 would be multiplied
by 7 for the result of 147.

Even though we've spent a lot of time in areas that might not seem
very important, the details presented here will make more sense once
you start to program and will prove invaluable.

To give you an idea how things fit in place, consider the following:
how could you combine the two types of print statements we've
examined so far to print something more meaningful on the screen?

We know that by enclosing something within quites marks prints that
information on the screen exactly as it was entered, and by using math
operators, calculations can be performed. So why not combine the two
types of PRINT statements like this:

Code: [Select]
? "5 * 9 = "; 5 * 9

Even though this might seem a bit redundant, what we've done is
simply use both types of print statements together. The first part prints
"5 * 9 =" exactly as it was typed. The second part does the actual work
and prints the result, with the semicolon separating the message part of
the statement from the actual calculation.

You can separate the parts of a mixed print statement with punctuation
for various formats. Try a comma in place of the semicolon and see
what happens.

For the curious, the semicolon causes the next part of the statement to
be printed immediately after the previous part, without any spaces. The
comma does something different. Even though it is an acceptable
separator, it spaces things out more. If you type:

Code: [Select]
? 2,3,4,5,6

the numbers will be printed across the screen.

The basic difference between the comma and semicolon in formatting
PRINT statement can be used to our advantage when creating more
complex displays: it will allow us to create some sophisticated results
very easily.
Woe to those who call evil good, and good evil;
Who put darkness for light, and light for darkness;
Who put bitter for sweet, and sweet for bitter!

Isaiah 5:20

  • Print