<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" MadCap:lastBlockDepth="6" MadCap:lastHeight="94" MadCap:lastWidth="853" MadCap:disableMasterStylesheet="true" MadCap:tocPath="PBASIC Language Reference" MadCap:InPreviewMode="false" MadCap:PreloadImages="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" lang="en-us" xml:lang="en-us" MadCap:PathToHelpSystem="../../../" MadCap:HelpSystemFileName="Default.xml" MadCap:SearchType="Stem">
    <head><title>Constants</title>
        <link href="../../SkinSupport/MadCap.css" rel="stylesheet" />
        <link href="../../Resources/Stylesheets/BSE_Help.css" rel="stylesheet" />
        <script src="../../SkinSupport/MadCapAll.js" type="text/javascript">
        </script>
    </head>
    <body>
        <div class="MCBreadcrumbsBox_0"><span class="MCBreadcrumbsPrefix">You are here: </span><a class="MCBreadcrumbsLink" href="AlphaRef.htm">PBASIC Language Reference</a><span class="MCBreadcrumbsDivider"> &gt; </span><span class="MCBreadcrumbs">Constants</span>
        </div>
        <h1>Constants</h1>
        <div class="ImagePlusCaption">
            <div class="Col2">
                <p>
                    <img src="../../graphics/pgm_icon1.gif" border="0" alt="BS1 icon" title="BS1 icon" />
                    <img src="../../graphics/pgm_icon2.gif" border="0" alt="BS2 icon" title="BS2 icon" />
                    <img src="../../graphics/pgm_icon2e.gif" border="0" alt="BS2e icon" title="BS2e icon" />
                    <img src="../../graphics/pgm_icon2sx.gif" border="0" alt="BS2sx icon" title="BS2sx icon" />
                    <img src="../../graphics/pgm_icon2p.gif" border="0" alt="BS2p icon" title="BS2p icon" />
                    <img src="../../graphics/pgm_icon2pe.gif" border="0" alt="BS2pe icon" title="BS2pe icon" />
                    <img src="../../graphics/pgm_icon2px.gif" border="0" alt="BS2px icon" title="BS2px icon" />
                </p>
            </div>
            <p style="text-align: right;">&#160;</p>
            <p>&#160;</p>
        </div>
        <p class="clear">&#160;</p>
        <p>&#160;</p>
        <p class="PlainText">Suppose you’re working on a program called "Three Cheers" that flashes LEDs, 
makes hooting sounds, and activates a motor that crashes cymbals together, all in
sets of three. A portion of your PBASIC program might contain something like:</p>
        <p>
            <img align="absmiddle" src="../../graphics/stamp_all.gif" border="0" alt="" />
        </p><pre class="BScode" xml:space="preserve">
  FOR counter = 1 TO 3
    GOSUB Make_Cheers
  NEXT

  FOR counter = 1 TO 3
    GOSUB Blink_LEDs
  NEXT

  FOR counter = 1 TO 3
    GOSUB Crash_Cymbals
  NEXT
</pre>
        <p class="PlainText">The numbers 1 and 3 in the code above are called constants. They are constants 
because, while the program is running, nothing can happen to change those numbers. 
This distinguishes constants from variables, which can change while the program 
is running.</p>
        <p class="PlainText">PBASIC allows you to use several numbering systems. By default, it assumes that
numbers are in decimal (base 10), our everyday system of numbers. But you can also
use binary and hexadecimal (hex) numbers by identifying them with prefixes. And 
PBASIC will automatically convert quoted text into the corresponding ASCII code(s). 
For example:</p>
        <table cellpadding="0" cellspacing="0" border="0">
            <tr>
                <td width="60">99</td>
                <td>decimal</td>
            </tr>
            <tr>
                <td>%1010</td>
                <td>binary</td>
            </tr>
            <tr>
                <td>$FE</td>
                <td>hex</td>
            </tr>
            <tr>
                <td>"A"</td>
                <td>ASCII code for A (65)</td>
            </tr>
        </table>
        <p>&#160;</p>
        <p class="PlainText">You can assign names to constants in a similar fashion to how variables are 
declared. On a BS1, it is identical to variable declarations and on the other 
BASIC Stamp modelss you use the <span class="keyword_in_text">CON</span> directive. Here is the syntax:</p>
        <p>
            <img align="absmiddle" src="../../graphics/bs1_inline.gif" border="0" alt="" />
        </p><pre class="BScode" xml:space="preserve">
SYMBOL  Name    = ConstantValue
</pre>
        <p>
            <img align="absmiddle" src="../../graphics/bs2all_inline.gif" border="0" alt="" />
        </p><pre class="BScode" xml:space="preserve">
Name    CON     ConstantValue
</pre>
        <p class="PlainText">Once created, named constants may be used in place of the numbers they
represent. For example:</p>
        <p>
            <img align="absmiddle" src="../../graphics/bs1_inline.gif" border="0" alt="" />
        </p><pre class="BScode" xml:space="preserve">
SYMBOL  Cheers  = 3

SYMBOL  idx     = B2

Main:
  FOR idx = 1 TO Cheers
    GOSUB Make_Cheers
  NEXT
</pre>
        <p>
            <img align="absmiddle" src="../../graphics/bs2all_inline.gif" border="0" alt="" />
        </p><pre class="BScode" xml:space="preserve">
Cheers  CON     3

idx     VAR     Nib

Main:
  FOR idx = 1 TO Cheers
    GOSUB Make_Cheers
  NEXT
</pre>
        <p class="PlainText">That code would work exactly the same as the previous <span class="keyword_in_text">FOR...NEXT</span> loops. The 
editor software would substitute the number 3 for the constant named Cheers 
throughout your program. Like variable names, labels and instructions, constant 
names are not case sensitive; CHEERS, and ChEErs would all be processed as 
identical to Cheers.</p>
        <p class="PlainText">Refer to the <a href="ElementsStyle.htm" target="" title="" alt="" class="MCXref_0">Elements of PBASIC Style</a> for 
suggested guidelines on naming constant values.</p>
        <p class="PlainText">Using named constants does not increase the amount of code downloaded to the 
BASIC Stamp, and it often improves the clarity of the program. Weeks after a 
program is written, you may not remember what a particular number was supposed to
represent—using a name may jog your memory (or simplify the detective work needed
to figure it out).</p>
        <p class="PlainText">Named constants also have another benefit. Suppose the "Three Cheers" program 
had to be upgraded to "Five Cheers." In the original example you would have to 
change all of the 3s to 5s. Search and replace would help, but you might 
accidentally change some 3s that weren't numbers of cheers, too. However, if you
made smart use of a named constant, all you would have to do is change 3 to 5 in
one place, the constant's declaration:</p>
        <p>
            <img align="absmiddle" src="../../graphics/bs1_inline.gif" border="0" alt="" />
        </p><pre class="BScode" xml:space="preserve">
SYMBOL  Cheers  = 5
</pre>
        <p>
            <img align="absmiddle" src="../../graphics/bs2all_inline.gif" border="0" alt="" />
        </p><pre class="BScode" xml:space="preserve">
Cheers  CON     5
</pre>
        <p class="PlainText">Now, assuming that you used the constant cheers wherever your program needed
'the number of cheers,' your upgrade would be done.</p>
        <p>
            <img align="absmiddle" src="../../graphics/bs2all_inline.gif" border="0" alt="" />
        </p>
        <p class="PlainText">On the BS2-family you can take this idea a step further by defining constants
with expressions; groups of math and/or logic operations that the editor software 
solves (evaluates) at compile time (the time right after you start the download 
and before the BASIC Stamp starts running your program). For example, suppose the 
"Cheers" program also controls a pump to fill glasses with champagne. Perhaps the
number of glasses to fill is always twice the number of cheers, minus 1 (another
constant):</p><pre class="BScode" xml:space="preserve">
Cheers  CON     5                       ' # of cheers
Glasses CON     Cheers * 2 - 1          ' # of glasses
</pre>
        <p class="PlainText">As you can see, one constant can be defined in terms of another. That is, the
number glasses depends on the number cheers.</p>
        <div class="Col2">
            <div class="MasterFoot">
                <p MadCap:conditions="BSEconditions.BSEWebHelp (Primary)-INCLUDE"><a href="../../HomeTopics/HomePage.htm">Go to Welcome page</a>
                </p>
            </div>
        </div>
        <div class="Col2">
            <div class="MasterFoot">
                <p style="text-align: right;"><span class="ContactInfoProjectName">BASIC Stamp Help</span> <![CDATA[ ]]><span class="ContactInfoVersion#">Version 2.5.4</span> <![CDATA[ ]]></p>
                <p style="text-align: right;">Copyright ©&#160;<span class="ContactInfoCompanyName">Parallax Inc.</span></p>
                <p style="text-align: right;"><span class="SystemShortDate">8/8/2012</span>
                </p>
            </div>
        </div>
        <script type="text/javascript">/* <![CDATA[ */

			var _gaq = _gaq || [];
			_gaq.push(['_setAccount', 'UA-285614-1']);
			_gaq.push(['_trackPageview']);

			(function() {
			var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
			ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
			var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
			})();

		/* ]]> */</script>
        <script type="text/javascript" src="../../SkinSupport/MadCapBodyEnd.js">
        </script>
    </body>
</html>