please dont rip this site

Learning to Program with the Cybiko Handheld Computer Using B2C

Chapter 8 : Variables and DIM

Variables

As we saw in Chapter 7, variables hold a value.  The value can be a number (like 1) or the result of an operation (like a+1).  You may think of variable as a shoebox with a label on the outside.  Inside the shoebox is a value.  On the front of the shoebox is a label with a variable name (like ‘a’, or ‘b’ or ‘avg’). 

We also saw that before using a variable we have to declare it.  This is done with the ‘Dim’ statement (short for Dimension).  The Dim statement alerts B2C to the fact that we are about to use a variable.  The variable is initially set (initialized) to zero.

Data Types

It turns out that B2C has 4 different variable types.  These are Int, Char, Long and Double.  Currently, Double (or floating point) data types are not supported.  Using the Double type will coerce the variable to Int.

‘Long’ variables are integer variables (no decimal point) containing 4 bytes.  They can range from –2,147,483,648 to +2,147,483,647.  Because Longs are integer variables, they are fast variables when it comes to computation.  But, since they are 4 bytes long, they still take up a lot of Cybiko computing power when processing.  Use the Long data type when you need large values but you don’t need a decimal point.  To declare a Long variable place the words “as long” after the declaration:

dim a as long

‘Int’ (or integer) variables are integer variables which means they cannot hold a decimal point and contain only 2 bytes of data.  They can range from -32,768 to +32,767.  As you will see, the range is much smaller than a Long.  And because this is the favorite size of numbers for the Cybiko’s internal microprocessor, Int variables perform the fastest.  To declare an Int variable place the words “as int” after the declaration:

dim a as int

‘Char’ variables are also integer variables, but contain only 1 byte of data.  Char variables range from -128 to +127.  They are not very useful in computations – and since they are smaller than the Cybiko’s favorite word size (2 bytes) it actually takes longer to process a Char than an Int.  As we’ll see shortly, Char variables are usually used in an array as a character string for input and output, rather than as a numeric quantity for computation.  To declare a Char variable place the words “as char” after the declaration

dim a as char

Names of Variables

The name of a variable can be any length with no spaces in it.  The first letter must be alphabetic (a-z).  Subsequent letters may be alphanumeric (a-z or 0-9).  You may also use the underscore (_) character in variable names.  As mentioned before, upper and lower case do not matter.

Arrays

An array is a list, or grouping, of variables.  If you have a list of something you will want to declare an array of them.  Returning to our shoebox example, suppose you have an exam grade.  You can mark the outside of a shoebox with the name “grade” and put the exam inside the box.  If you ever want to refer to the grade, you find the shoebox marked “grade” and take out the exam and look at the grade you received.  This use of a single variable is called a ‘scalar’.  But you cannot put more than one grade inside the shoebox.

Now, let’s assume that you have many students and you want to keep all their grades separated.  You might get a set of shoeboxes and line them up in a row.  On the front of each box put the student’s number (0, 1, 2, 3, etc…).  Each student exam goes into the box with that student’s number on it.  From then on, you access the boxes as “grade[0]”, “grade[1]”, etc…  This is called an array of variables.  The part between the brackets is called the subscript.

To create an array of variables, use the Dim statement as before and put the number of elements of the array in brackets after the name.  Here is an example of an array of 10 grades:

Dim grades[10] as int

Humans are taught to count starting from one.  If I asked you to count to ten, you might reply “1, 2, 3, …10" But computers prefer to count starting from zero.  (Technically speaking, computers use an offset and it is convenient for computation and memory layout if the offset starts at zero).  So, the first grade in our example is grade[0] (Does this explain why I used grade0 earlier?  I hope so!).  The next one is grade[1]. And so on up to grade[9]. (Notice that we cannot use grade[10] – since that would be the eleventh grade and we defined 10 integers.).

All data types may be arrays.  The Char array is special in that it is treated like a string of text by B2C.  When you want to input a name, use the Char array:

Dim name[32] as char
Input name
Print "your name is ", name

Example Program

DO THIS

Copy the file "c:\…\B2Cv5\tutorial\ch8.b2c" to "C:\…\B2Cv5\ch8.b2c".  Then execute the command "build ch8.b2c".  Download the ch8.app file to the cybiko.
Here we return to our grade-averaging problem, only this time we use an array of grades.  In the next chapter we will see the real power of arrays when we introduce loops.
' chapter 8 example program
 ' sum and average of 4 grades
 ' grades are from 0-100
 dim grade[4]  ' an array of 4 grades
 dim sum as int 'this variable will store the sum of the 4 grades
 dim avg as int 'this variable will store the average of the 4 grades
  
 ' ---INPUT ---
 print "Enter grade 0" 
 input grade[0]        'get the grades from the student
 print "Enter grade 1"
 input grade[1]
 print "Enter grade 2"
 input grade[2]
 print "Enter grade 3"
 input grade[3]
  
 '--- PROCESS ---
 ' compute the sum and the average
 sum = grade[0]+grade[1]+grade[2]+grade[3]
 avg = sum/4
  
 '--- OUTPUT ---
 print "The average of your"
 print "4 grades is", avg
  
 print "Press <Enter> to continue"
 dim tmp ' a temporary variable
 input tmp 'wait for the user to press enter
      


file: /Techref/cybiko/b2c/ch8.htm, 7KB, , updated: 2008/6/13 17:01, local time: 2024/11/26 17:56,
TOP NEW HELP FIND: 
18.224.59.107:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://techref.massmind.org/Techref/cybiko/b2c/ch8.htm"> cybiko b2c ch8</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

 

Welcome to massmind.org!

 

Welcome to techref.massmind.org!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .