December 09, 2023, 09:29:56 PM


Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........

Programming 4 - Variables and Definitions

Started by GWS, April 25, 2010, 10:30:50 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.


April 25, 2010, 10:30:50 AM Last Edit: April 29, 2010, 11:01:09 AM by GWS
Hi folks,

Let's press on with a few fundamentals ..

Programming 4.

Sorry about the scary little program in Programming 3 - but I wanted a bit of real code to refer back to.

We'll now take a look at some of the building blocks of a program.

Every program has a number of 'instructions' which tell the computer how to perform the steps needed for a particular application.

Let's pick one from the previous bakery simulation program ..

TotalCakes = pinktotal + yellowtotal

This is known as a 'statement'.  It looks like an equation in algebra, but it isn't quite the same.

The right-hand side of the statement is known as an 'expression'.
In this case, it says - add the two quantities 'pinktotal' and 'yellowtotal'.

The result of the addition is then placed in the quantity on the left-hand side.

Now how can that work ?  The three quantities in the example statement are what are known as 'Variables', and every variable makes use of a small amount of memory in your computer.  How much memory depends on the 'type' of variable.

Oh dear ! - yet another new word - 'type' of a variable. Don't worry, there are only a few more buzz words to know about ..

A variable can hold either whole numbers like 120, -6, or decimal numbers like 0.5, -122.67, or strings of characters like "apple", "My Name is :"

You can check in the Help files what all the different variable types are, and the amount of memory they require.

In order that the computer can set aside an appropriate amount of memory for each variable used in your program, it needs to know what type each variable is.

You tell it this by writing a suitable DEF(ine) statement (usually at the start of each program).  

In the bakery example program, we find:

def elapsedtime,start,TotalCakes:int
def pink,pinktotal,yellow,yellowtotal:int
def PercentPink,Percentyellow:int

All of these statements define integer (or whole number quantities) used in the program.
Notice you can write a number of variables on the same line provided they are all of the same type.

To define decimal numbers you would use:

DEF Number AS FLOAT .. or as an alternative DEF Number : Float

Notice my sloppy naming of variables - hey .. I'm a vandal .. Variable names are not case sensitive, and can be up to 30 characters long.

So all these are exactly the same quantity :  costprice, COSTPRICE, or CostPrice.

Use capital letters appropriately to make the names easier to read.  So 'PercentPink' is a goody - 'yellowtotal' is not so good ..

Nothing wrong with any of them, except for the readability issue.  Choose your own style.

To define strings of alphabetical characters, you would define variables such as:

DEF Name, Address AS STRING

OK, now we know about variables, lets do a couple of little programs to test them out ..
Here's one to show Integer variables in action ..

def i, j, total as int
i = 5
j = 3
total = i + j
print "The total is: ", total
print "Press Any Key to Continue"
do:until inkey$ <> ""


If you copy and paste this into the Creative editor and run it, you will see the result 'The total is: 8'
That was easy, so now let's try using decimals ..

def x,y,product as float
' setprecision 8
X = 5
Y = 3.303
product = x * y
print "The product is: ", product
do:until inkey$ <> ""


Close the previous example, and copy and paste this one into the editor.  If you run it, you will get the result:
'The product is: 16.51'

No problems there ..
Finally, let's try out the strings ..

def name as string
name = "Donald Duck"
print name, " and Minnie Mouse"
do:until inkey$ <> ""


If you run this one you get a little surprise .. what's going on here ?

We defined the variable 'name' as a string, and used the statement:

name = "Donald Duck"

to load the variable with the famous duck's name.

Then we print out the contents of variable name, and append to it a bit more text. The extra text is just a string of characters in inverted commas.  This has the effect of printing the result as:

'Donald Duck and Minnie Mouse'.

So there you are - you now know quite a lot about Statements, Variables and their definitions.

End of Programming 4

Tomorrow may be too late ..