Programmer's Calculator V2.73 for the Palm OS

Programming using looping constructs

This is a somewhat contrived example but should illustrate some of the more powerful uses of the programming language.

Lets say we need to get a number from the user that represents an 8 bit byte value to be propagated through a 32 bit value. For example, if the user enters 127 (\x7F) then the result must be \x7F7F7F7F. Also, if the user enters a value greater than a byte value (> \xFF) then the byte is masked to a byte.

First, we need to get the value. We do this using the statement V=Inp("Enter Value"); which assigns the variable V to the value the user enters.

Next, we need to mask the value using V&=\xff; This statement uses the new op-assign functions which is equivilent to doing V=V&\xff which means take the value of V And it with the hexadecimal value FF and assign it back to the variable V.

Next we use one of the new iteration statements for which has the form:
The initial expr is executed and then the relop (relational operation) is executed, if it is true, then the statement that follows the for is executed, if it is not, then the statement is skipped. After the statement is executed, the third argument within the for is executed and the relop is tested again and the process continues until the relop is false.

Essentially, the loop sets i to zero, and increments i until i is not less than three

The actual statement that's executed is:
R=R|V; }

Which is a statement that's really a group of statements. Whenever the grammar says that a stmt is needed, you can substitute the curly brace enclosed group of statements.

The statement sets R to R shifted over 8 bits. Then, it sets R to R Ored with the value the user typed in and is stored in V.

Finally, the program returns the result using the return(R) statement.

Now press the ? key on the calc and enter the name of the program you just created

The first statement is executed and you're prompted for input, you can enter a number, an expression or an entire program here. For now, just enter the hexadecimal value \x7F

Finally, the program ends and the result of the program is displayed. If you're in Hex mode, the value should be 7F7F7F7F.

A final note, the looping construct for could have been replaced with while using:
while(i < 3) {
i++; }

Which exactly matches the for loop.

Or, using the do statement:
do {
i++; }
while(i < 3);
Which executes the statement before checking the relop.