John.Wiley.and.Sons.Rapid.Mobile.Enterprise.Development.for.Symbian.OS.An.Introduction.to.OPL.Application.Design.and.Programming.May.2005.eBook-LinG (779881), страница 5
Текст из файла (страница 5)
An integer has tobe a whole number (i.e. you can’t have HighScore%=2.5, but youcan have HighScore%=2 or HighScore%=3). The maximum valuea short integer variable can store is 32767, the minimum −32768.• A larger number (or ‘long integer’) is followed by a & sign. Forexample, BigNumber& is a large integer that stores a number, and canbe referred to as BigNumber& in your code. Just like small integers,these large integers can only be whole numbers.
The maximumvalue a long integer variable can store is 2147483647, the minimum−2147483648.• Numbers that contain decimals or fractions are implemented as ‘floats’(short for ‘floating point numbers’). These are easiest to code – yousimply type the variable name with no suffix and a float type isimplied, for example MyFloat. The maximum value a float variable can store is 1.7976931348623157E+308 and the minimum is2.2250738585072015E−308.• A string of letters and numbers is called (funnily enough) a string,and is followed by a $ symbol, For example, Name$ could be set tocontain "Ewan Spence".
A string variable can be a maximum of 255characters. Note that a string can hold any valid characters, so couldalso represent a number – for example, Name$="1138" is a validstring. You can’t, however, do any arithmetic on a string like this.You may have a simple question at this point regarding the different typesof numerical variable – ‘‘if float lets me store any type of number and toa much larger size than the other two, why don’t I just declare all mynumbers as floats?’’ The answer is simple – floats use more memory. Thinkof variables as ‘boxes’ or ‘pigeon holes’ – the bigger the box, the morespace (or memory) it requires.
It is therefore wasteful to always use a float.Defining VariablesBefore you can use a variable, you need to have told your program thatyou are going to use it by defining it. Think of it as reserving a little12PROGRAMMING PRINCIPLESspace in memory to store the information. There are two ways of defininga variable.A GLOBAL variable is a variable that any procedure in the programcan use, at any time.A LOCAL variable is a variable that only the specific procedure inwhich it is defined can use. When you leave the procedure, the littlespace in memory for the number is destroyed.
Local variables are goodfor temporary counters and bits of info that are only needed for a fewmoments (rather like a scrap of paper).The command to define (or ‘declare’) a variable needs to be the firstcommand of a procedure (although remember that rem comments donot count as commands, so these are acceptable before your variables).GLOBAL definitions must be the first lines in your first procedure, andLOCAL variables must be the first lines in the relevant procedure.You can put more than one definition on a line – simply separate themwith a comma, and they can be different types of variables.PROC Main:GLOBAL HighScore%,BigNumber&,MyFloatAnd.
. .PROC LocalExample:LOCAL HouseNumber%,Name$(20)Note that after defining Name$ we have a bracketed number. This tellsthe computer the maximum length (in characters) this particular stringwill ever need to be. You need to define the maximum length of all stringsor when you translate the program to run, you’ll get an error message.All numerical variables do not need this length definition, as theyalready have a ‘largest number’ limit implied (see above).Setting VariablesVariables are easy to set.
Simply use the equals sign. For example:HighScore%=56BigNumber&=1383512467MyFloat=559.8798Name$="Ewan Spence"Setting a variable should be done on a separate line for each command.Note that when setting a string, you have to use quotation marks. Thesedon’t appear as part of the string, they are used in the source code toshow where the string starts and ends.Once a variable has been set, you can of course change its value.LEARNING THE VOCABULARY13For example:HighScore%=56HighScore%=58results in HighScore% first being assigned the value of 56, then thevalue of 56 is thrown away and HighScore% is assigned the valueof 58 instead.
You can also have a variable set to equal the value ofanother variable:NewHighScore%=LastScoreInGame%1.3.4ArraysOne other tool you have in variables is an array. An array is a list ofthings. For example, if we wanted to have a table of 10 numbers, wecould do:GLOBAL Table1%,Table2%,Table3%This would work, but imagine now doing a list of a hundred things – thatwould be a lot of typing! This is where an array comes in useful. If youthink of variables as being ‘boxes’ in memory, each with a name (thevariable name), an array is a line of boxes, and that line has the name,each box has a number. It’s a bit like a spreadsheet – the array variableis ‘Row A’ and the individual ‘elements’ are columns 1, 2, 3, etc.
onthat row.To illustrate, let’s make an array called Table% to handle thosehundred things. Firstly, as with any variable, we need to define it:GLOBAL Table%(100)This will create an array of 100 items (the number in the brackets)and call it Table%. The array will hold short integers, signified by the% sign.The first ‘box’ can be addressed as Table%(1), the next asTable%(2), and so on. Smart-eyed readers will see that this bearsa striking resemblance to how we define a string – which has a maximumlength.
This is because each character in a string effectively takes up one‘box’ of memory.So can we make an array with strings? Yes, we can. We will need todefine the maximum length of the string inside the brackets, and we alsoneed to say how large the array will be (how many strings are in thearray). This becomes a second number inside the brackets, separated bya comma.14PROGRAMMING PRINCIPLESSo defining a string array looks like this:GLOBAL NameTable$(100,20)which is an array of 100 strings, each a maximum of 20 characters long,and the array is called NameTable$.So why use Table%(100) and not Table1%, Table2%, Table3%,and so on? Because if you have another variable (e.g. LookUpThisNumber%) then you can simply refer to Table%(LookUpThisNumber%)to get its value, rather than having to actually write in code ‘‘Well,if LookUpThisNumber%=1, return Table1%.
If LookUpThisNumber%=2, return Table2%, and so on’’ – which, for 100 elements, wouldbe very, very wasteful!We will illustrate this further shortly.1.3.5ConstantsConstants are exactly what they sound like – things that don’t change!They are a bit like variables (so they can be numbers or strings), but theycan never change value. You can use them so code is easier to read.Rather than having to type the number 103782376 every time you needto use it (assuming it is something that appears a lot in your program),you can put the following at the start of your code:KBigNumber&=103782376Each time you need to use this number, you can write KBigNumber&instead. By convention, you should prefix any constant name with K tohelp differentiate it from normal variables.
Constants should be definedat the start of your source code, before the first procedure.CONST KBigNumber&=103782376PROC Main:GLOBAL HighScore%,HighScoreName$(100)etc...Once you see some example code, constants should be a lot clearer. Onething to point out now is that there are a lot of default constants for thingsyou will use a lot. If you have the lineINCLUDE "Const.oph"at the top of your code, then these default constants can all be used inyour own code too.
A list of these constants can be found in the OPLDocumentation.LEARNING THE VOCABULARY151.3.6 LoopsDo. . .UntilLoops are a great way to make your program do something over and overagain, maybe with a few changes. Look at this code:PROC DoUntilLoop:LOCAL Foo%Foo%=0DOFoo%=Foo%+1Table%(Foo%)=Foo%UNTIL Foo%=100ENDPTo make sure you understand this code, let’s look at it line by line. TheDO...UNTIL loop is a primary building block of source code, becauselots of programming involves repetition. Firstly we create a variable toact as a counter, in this case Foo%. This is a LOCAL variable so will onlybe accessible to the DoUntilLoop: procedure, and the space reservedfor it in memory is reclaimed once the procedure is finished.You’ll see me use Foo% (and Gnu% and Zsu%) as a temporary variablename a lot.
If you do see it, you can assume it’s a counter or anothertemporary local variable.First of all we set the temporary variable Foo% to be zero. Wethen reach the start of the loop (DO). The code will then DO whatevercomes up next – here, add one to the current value of Foo% first of all(Foo%=Foo%+1) and then set the array element of the Table% array tobe equal to the new value of Foo%. We will keep doing this UNTIL thevalue of Foo% is 100.
Thinking ahead, what this code means is that at theend of the loop, our Table% array will have 100 sequential numbers ineach box, and the box position will match the value.While. . .EndWhileDO...UNTIL is the primary loop that you will see in this book. Onething you need to note is that the code inside a DO...UNTIL loopwill always be carried out a least once – think for a minute to work outwhy this is the case (Hint: remember OPL code is executed line-by-linein order).There will be circumstances where you might want to check thecondition before the code in the loop is reached.
If this is the case, youwould use the WHILE...ENDWH (EndWhile) construction.PROC WhileEndWhileLoop:LOCAL Foo%,Table%(100)16PROGRAMMING PRINCIPLESFoo%=0WHILE Foo%<100Foo%=Foo%+1Table%(Foo%)=Foo%ENDWHENDPHere, the code inside the loop is only carried out if the comparison inthe WHILE statement is true (i.e. the reverse of the UNTIL statement). Thecode in the loop will continue to be run until the statement is false.
At thispoint the commands after the ENDWH will be read. Here, we are checkingfor Foo% being not equal to 100 (thus the WHILE condition will becomefalse once Foo%=100).You’ll note that although these two loop styles work logically indifferent ways, the end result is the same. This is something that happensin code a lot, where you can achieve the same end result through differentmethods. Don’t worry if you do something slightly different to somebodyelse – the end result is the most important thing to consider.BreakSo we can come out of these loops at the start of the loop, or at the end ofthe loop.