Home » Archimedes archive » Acorn User » AU 1997-10 A.adf » Extras » Apple][e/PD/BOB/ARMBOB/doc/Tutorial/03
Apple][e/PD/BOB/ARMBOB/doc/Tutorial/03
This website contains an archive of files for the Acorn Electron, BBC Micro, Acorn Archimedes, Commodore 16 and Commodore 64 computers, which Dominic Ford has rescued from his private collection of floppy disks and cassettes.
Some of these files were originally commercial releases in the 1980s and 1990s, but they are now widely available online. I assume that copyright over them is no longer being asserted. If you own the copyright and would like files to be removed, please contact me.
Tape/disk: | Home » Archimedes archive » Acorn User » AU 1997-10 A.adf » Extras |
Filename: | Apple][e/PD/BOB/ARMBOB/doc/Tutorial/03 |
Read OK: | ✔ |
File size: | 1759 bytes |
Load address: | 0000 |
Exec address: | 0000 |
File contents
ArmBob v.1.02 Tutorial 3 GCW 06/06/94 Accounts 2 ------------ Now we look at Bob:main.account. In main, the variable 'account_no' is used to count the number of accounts that have been created. The maximum is given by the variable 'enough'. The statement client = newvector(enough); creates a vector called 'client' with enough components. The term "vector" is used, rather than "array", for two reasons. First,arrays in Basic, C, Pascal, etc are homogeneous - all their components must have the same type. Vectors are inhomogeneous - their components can have quite different types. Second, one is often tempted to think of arrays as occupying a sequential block of memory. This would be misleading for vectors. Strings in ArmBob are closer to arrays in this sense. In fact vectors correspond more closely with structs of pointers in C, or with records in Pascal. The components of an n-component vector v are denoted v[0], v[1], .... v[n-1] It is legitimate for a vector to be a component of itself! When a vector is first created, its components all have the value nil. The components of client are to be the instance objects of the class account. The function 'banner' informs the user of the services that the Toy Town Bank can offer. It produces the output Welcome to the Toy Town Bank. 0 Quit 1 Open an account 2 Make a withdrawal or deposit 3 Get a statement 4 Change your password Press one of the above numbers then press RETURN. Then follows the main loop of the program while ((i = input()) != "0") /* Top level interaction loop */ { ...... } which gets input from the user with the function 'input', and responds accordingly with a switch statement. Those new to C should realise that in C, and ArmBob, assignments, like i = input() are actually expressions, which return a value as well as produce a side-effect, and so this value can be part of a larger expression (i = input()) != "0" This is not the case in Basic. Remember that Basic uses '=' with two meanings - assignment and test for equality. In C, and ArmBob, these two uses are distinguished. Assignment uses '=', test for equality uses '=='. Another catch for the Basic programmer unused to C lurks in the switch statement. In Basic's CASE ... WHEN .... ENDCASE construction execution automatically jumps to after the ENDCASE from the end of the code following a WHEN condition. On the other hand in C's, and ArmBob's, switch (..){ .... case .... } construction, execution does NOT automatically jump to after the closing brace, unless specifically directed to do so with a 'break;' statement. Forgetting the 'break;' is a frequent source of error. The switch statement in main is used to dispatch execution to various alternative functions, the first of which is new_account. Note the line amount = val(input()); The 'input' function always produces a string. The function 'val' converts a string to a number, as far as possible. The next line client[account_no++] = new account(amount,name); uses the post-increment operator '++'. This is an example of a unary assignment operator. The effect of this one line is equivalent to client[account_no] = new account(amount,name); account_no = account_no + 1; The vector 'client' is being used to store all the accounts in a single structure. Note that we have functions 'withdraw', 'statement', 'change_password' despite the fact that these are names of methods for the 'account' class. There is no clash, because methods are private to the class they belong to. All these functions use the function 'get_account' which uses the 'has_owner' method to find which accounts, if any, have an owner 'name'. It returns 'the_account', so that the operator client[the_account]-> can be applied to the appropriate method to do what has been requested. The heart of get_account is the search for the account owned by 'name': while ((the_account<account_no) && !(client[the_account]->has_owner(name))) the_account++; The && operator corresponds to Basic's AND, but it has an important advantage - it is lazy! Compare these two programs, the first in Basic, the second in ArmBob. The Basic version: REM AND is strict IF FNf AND FNg THEN PRINT "yes" ELSE PRINT "no" END DEF FNf PRINT "<<Evaluating f>>" = 0 DEF FNg PRINT "<<Evaluating g>>" = 0 The ArmBob version: /* && is NOT strict */ main() { if ( f() && g() ) print("yes\n"); else print("no\n"); } f() { print("<<Evaluating f>>\n"); return 0; } g() { print("<<Evaluating g>>\n"); return 0; } The Basic program produces as output <<Evaluating f>> <<Evaluating g>> no whereas the ArmBob program produces <<Evaluating f>> no In other words, && does not bother to evaluate its right hand argument when it finds that its left hand argument is zero, whereas AND always evaluates both. This is smart of && and dumb of AND because you often need, as here in get_account, to test two conditions where error-free evaluation of the second condition is contingent upon the success of the first condition. This usually happens with vectors or arrays, where the first condition checks that an index lies within its bounds, and if it is, the second condition checks some expression involving the component given by the index. In the same way C's and ArmBob's || is lazy where Basic's OR is strict. If the left hand argument of || is true, the right hand argument does not get evaluated.
00000000 41 72 6d 42 6f 62 20 76 2e 31 2e 30 32 20 54 75 |ArmBob v.1.02 Tu| 00000010 74 6f 72 69 61 6c 20 33 20 20 20 20 20 20 20 20 |torial 3 | 00000020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 | | 00000030 20 20 20 20 20 20 20 47 43 57 20 30 36 2f 30 36 | GCW 06/06| 00000040 2f 39 34 0a 0a 41 63 63 6f 75 6e 74 73 20 20 20 |/94..Accounts | 00000050 32 0a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 4e |2.------------.N| 00000060 6f 77 20 77 65 20 6c 6f 6f 6b 20 61 74 20 42 6f |ow we look at Bo| 00000070 62 3a 6d 61 69 6e 2e 61 63 63 6f 75 6e 74 2e 20 |b:main.account. | 00000080 49 6e 20 6d 61 69 6e 2c 20 74 68 65 20 76 61 72 |In main, the var| 00000090 69 61 62 6c 65 20 27 61 63 63 6f 75 6e 74 5f 6e |iable 'account_n| 000000a0 6f 27 20 69 73 0a 75 73 65 64 20 74 6f 20 63 6f |o' is.used to co| 000000b0 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f |unt the number o| 000000c0 66 20 61 63 63 6f 75 6e 74 73 20 74 68 61 74 20 |f accounts that | 000000d0 68 61 76 65 20 62 65 65 6e 20 63 72 65 61 74 65 |have been create| 000000e0 64 2e 20 54 68 65 0a 6d 61 78 69 6d 75 6d 20 69 |d. The.maximum i| 000000f0 73 20 67 69 76 65 6e 20 62 79 20 74 68 65 20 76 |s given by the v| 00000100 61 72 69 61 62 6c 65 20 27 65 6e 6f 75 67 68 27 |ariable 'enough'| 00000110 2e 20 54 68 65 20 73 74 61 74 65 6d 65 6e 74 0a |. The statement.| 00000120 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 |. | 00000130 20 20 20 20 20 63 6c 69 65 6e 74 20 3d 20 6e 65 | client = ne| 00000140 77 76 65 63 74 6f 72 28 65 6e 6f 75 67 68 29 3b |wvector(enough);| 00000150 0a 0a 63 72 65 61 74 65 73 20 61 20 76 65 63 74 |..creates a vect| 00000160 6f 72 20 63 61 6c 6c 65 64 20 27 63 6c 69 65 6e |or called 'clien| 00000170 74 27 20 77 69 74 68 20 65 6e 6f 75 67 68 20 63 |t' with enough c| 00000180 6f 6d 70 6f 6e 65 6e 74 73 2e 20 54 68 65 20 74 |omponents. The t| 00000190 65 72 6d 0a 22 76 65 63 74 6f 72 22 20 69 73 20 |erm."vector" is | 000001a0 75 73 65 64 2c 20 72 61 74 68 65 72 20 74 68 61 |used, rather tha| 000001b0 6e 20 22 61 72 72 61 79 22 2c 20 66 6f 72 20 74 |n "array", for t| 000001c0 77 6f 20 72 65 61 73 6f 6e 73 2e 20 46 69 72 73 |wo reasons. Firs| 000001d0 74 2c 61 72 72 61 79 73 20 0a 69 6e 20 42 61 73 |t,arrays .in Bas| 000001e0 69 63 2c 20 43 2c 20 50 61 73 63 61 6c 2c 20 65 |ic, C, Pascal, e| 000001f0 74 63 20 61 72 65 20 68 6f 6d 6f 67 65 6e 65 6f |tc are homogeneo| 00000200 75 73 20 2d 20 61 6c 6c 20 74 68 65 69 72 20 63 |us - all their c| 00000210 6f 6d 70 6f 6e 65 6e 74 73 20 6d 75 73 74 20 0a |omponents must .| 00000220 68 61 76 65 20 74 68 65 20 73 61 6d 65 20 74 79 |have the same ty| 00000230 70 65 2e 20 56 65 63 74 6f 72 73 20 61 72 65 20 |pe. Vectors are | 00000240 69 6e 68 6f 6d 6f 67 65 6e 65 6f 75 73 20 2d 20 |inhomogeneous - | 00000250 74 68 65 69 72 20 63 6f 6d 70 6f 6e 65 6e 74 73 |their components| 00000260 0a 63 61 6e 20 68 61 76 65 20 71 75 69 74 65 20 |.can have quite | 00000270 64 69 66 66 65 72 65 6e 74 20 74 79 70 65 73 2e |different types.| 00000280 20 53 65 63 6f 6e 64 2c 20 6f 6e 65 20 69 73 20 | Second, one is | 00000290 6f 66 74 65 6e 20 74 65 6d 70 74 65 64 20 74 6f |often tempted to| 000002a0 20 74 68 69 6e 6b 0a 6f 66 20 61 72 72 61 79 73 | think.of arrays| 000002b0 20 61 73 20 6f 63 63 75 70 79 69 6e 67 20 61 20 | as occupying a | 000002c0 73 65 71 75 65 6e 74 69 61 6c 20 62 6c 6f 63 6b |sequential block| 000002d0 20 6f 66 20 6d 65 6d 6f 72 79 2e 20 54 68 69 73 | of memory. This| 000002e0 20 77 6f 75 6c 64 20 62 65 0a 6d 69 73 6c 65 61 | would be.mislea| 000002f0 64 69 6e 67 20 66 6f 72 20 76 65 63 74 6f 72 73 |ding for vectors| 00000300 2e 20 53 74 72 69 6e 67 73 20 69 6e 20 41 72 6d |. Strings in Arm| 00000310 42 6f 62 20 61 72 65 20 63 6c 6f 73 65 72 20 74 |Bob are closer t| 00000320 6f 20 61 72 72 61 79 73 20 69 6e 20 74 68 69 73 |o arrays in this| 00000330 0a 73 65 6e 73 65 2e 20 49 6e 20 66 61 63 74 20 |.sense. In fact | 00000340 76 65 63 74 6f 72 73 20 63 6f 72 72 65 73 70 6f |vectors correspo| 00000350 6e 64 20 6d 6f 72 65 20 63 6c 6f 73 65 6c 79 20 |nd more closely | 00000360 77 69 74 68 20 73 74 72 75 63 74 73 20 6f 66 20 |with structs of | 00000370 70 6f 69 6e 74 65 72 73 0a 69 6e 20 43 2c 20 6f |pointers.in C, o| 00000380 72 20 77 69 74 68 20 72 65 63 6f 72 64 73 20 69 |r with records i| 00000390 6e 20 50 61 73 63 61 6c 2e 0a 0a 54 68 65 20 63 |n Pascal...The c| 000003a0 6f 6d 70 6f 6e 65 6e 74 73 20 6f 66 20 61 6e 20 |omponents of an | 000003b0 6e 2d 63 6f 6d 70 6f 6e 65 6e 74 20 76 65 63 74 |n-component vect| 000003c0 6f 72 20 76 20 61 72 65 20 64 65 6e 6f 74 65 64 |or v are denoted| 000003d0 0a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 |.. | 000003e0 76 5b 30 5d 2c 20 76 5b 31 5d 2c 20 2e 2e 2e 2e |v[0], v[1], ....| 000003f0 20 76 5b 6e 2d 31 5d 20 20 20 20 0a 0a 49 74 20 | v[n-1] ..It | 00000400 69 73 20 6c 65 67 69 74 69 6d 61 74 65 20 66 6f |is legitimate fo| 00000410 72 20 61 20 76 65 63 74 6f 72 20 74 6f 20 62 65 |r a vector to be| 00000420 20 61 20 63 6f 6d 70 6f 6e 65 6e 74 20 6f 66 20 | a component of | 00000430 69 74 73 65 6c 66 21 20 57 68 65 6e 20 61 20 76 |itself! When a v| 00000440 65 63 74 6f 72 0a 69 73 20 66 69 72 73 74 20 63 |ector.is first c| 00000450 72 65 61 74 65 64 2c 20 69 74 73 20 63 6f 6d 70 |reated, its comp| 00000460 6f 6e 65 6e 74 73 20 61 6c 6c 20 68 61 76 65 20 |onents all have | 00000470 74 68 65 20 76 61 6c 75 65 20 6e 69 6c 2e 0a 0a |the value nil...| 00000480 54 68 65 20 63 6f 6d 70 6f 6e 65 6e 74 73 20 6f |The components o| 00000490 66 20 63 6c 69 65 6e 74 20 61 72 65 20 74 6f 20 |f client are to | 000004a0 62 65 20 74 68 65 20 69 6e 73 74 61 6e 63 65 20 |be the instance | 000004b0 6f 62 6a 65 63 74 73 20 6f 66 20 74 68 65 20 63 |objects of the c| 000004c0 6c 61 73 73 0a 61 63 63 6f 75 6e 74 2e 20 54 68 |lass.account. Th| 000004d0 65 20 66 75 6e 63 74 69 6f 6e 20 27 62 61 6e 6e |e function 'bann| 000004e0 65 72 27 20 69 6e 66 6f 72 6d 73 20 74 68 65 20 |er' informs the | 000004f0 75 73 65 72 20 6f 66 20 74 68 65 20 73 65 72 76 |user of the serv| 00000500 69 63 65 73 20 74 68 61 74 20 74 68 65 0a 54 6f |ices that the.To| 00000510 79 20 54 6f 77 6e 20 42 61 6e 6b 20 63 61 6e 20 |y Town Bank can | 00000520 6f 66 66 65 72 2e 20 49 74 20 70 72 6f 64 75 63 |offer. It produc| 00000530 65 73 20 74 68 65 20 6f 75 74 70 75 74 0a 0a 20 |es the output.. | 00000540 20 20 20 20 20 20 20 20 20 57 65 6c 63 6f 6d 65 | Welcome| 00000550 20 74 6f 20 74 68 65 20 54 6f 79 20 54 6f 77 6e | to the Toy Town| 00000560 20 42 61 6e 6b 2e 0a 0a 20 20 20 20 20 20 20 20 | Bank... | 00000570 20 20 20 20 20 30 20 51 75 69 74 0a 20 20 20 20 | 0 Quit. | 00000580 20 20 20 20 20 20 20 20 20 31 20 4f 70 65 6e 20 | 1 Open | 00000590 61 6e 20 61 63 63 6f 75 6e 74 0a 20 20 20 20 20 |an account. | 000005a0 20 20 20 20 20 20 20 20 32 20 4d 61 6b 65 20 61 | 2 Make a| 000005b0 20 77 69 74 68 64 72 61 77 61 6c 20 6f 72 20 64 | withdrawal or d| 000005c0 65 70 6f 73 69 74 0a 20 20 20 20 20 20 20 20 20 |eposit. | 000005d0 20 20 20 20 33 20 47 65 74 20 61 20 73 74 61 74 | 3 Get a stat| 000005e0 65 6d 65 6e 74 0a 20 20 20 20 20 20 20 20 20 20 |ement. | 000005f0 20 20 20 34 20 43 68 61 6e 67 65 20 79 6f 75 72 | 4 Change your| 00000600 20 70 61 73 73 77 6f 72 64 0a 0a 20 20 20 20 20 | password.. | 00000610 20 20 20 20 20 50 72 65 73 73 20 6f 6e 65 20 6f | Press one o| 00000620 66 20 74 68 65 20 61 62 6f 76 65 20 6e 75 6d 62 |f the above numb| 00000630 65 72 73 20 74 68 65 6e 20 70 72 65 73 73 20 52 |ers then press R| 00000640 45 54 55 52 4e 2e 0a 0a 54 68 65 6e 20 66 6f 6c |ETURN...Then fol| 00000650 6c 6f 77 73 20 74 68 65 20 6d 61 69 6e 20 6c 6f |lows the main lo| 00000660 6f 70 20 6f 66 20 74 68 65 20 70 72 6f 67 72 61 |op of the progra| 00000670 6d 0a 0a 20 77 68 69 6c 65 20 28 28 69 20 3d 20 |m.. while ((i = | 00000680 69 6e 70 75 74 28 29 29 20 21 3d 20 22 30 22 29 |input()) != "0")| 00000690 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 70 20 | /* Top | 000006a0 6c 65 76 65 6c 20 69 6e 74 65 72 61 63 74 69 6f |level interactio| 000006b0 6e 20 6c 6f 6f 70 20 2a 2f 0a 20 7b 20 2e 2e 2e |n loop */. { ...| 000006c0 2e 2e 2e 20 7d 0a 0a 77 68 69 63 68 20 67 65 74 |... }..which get| 000006d0 73 20 69 6e 70 75 74 20 66 72 6f 6d 20 74 68 65 |s input from the| 000006e0 20 75 73 65 72 20 77 69 74 68 20 74 68 65 20 66 | user with the f| 000006f0 75 6e 63 74 69 6f 6e 20 27 69 6e 70 75 74 27 2c |unction 'input',| 00000700 20 61 6e 64 20 72 65 73 70 6f 6e 64 73 0a 61 63 | and responds.ac| 00000710 63 6f 72 64 69 6e 67 6c 79 20 77 69 74 68 20 61 |cordingly with a| 00000720 20 73 77 69 74 63 68 20 73 74 61 74 65 6d 65 6e | switch statemen| 00000730 74 2e 20 54 68 6f 73 65 20 6e 65 77 20 74 6f 20 |t. Those new to | 00000740 43 20 73 68 6f 75 6c 64 20 72 65 61 6c 69 73 65 |C should realise| 00000750 0a 74 68 61 74 20 69 6e 20 43 2c 20 61 6e 64 20 |.that in C, and | 00000760 41 72 6d 42 6f 62 2c 20 61 73 73 69 67 6e 6d 65 |ArmBob, assignme| 00000770 6e 74 73 2c 20 6c 69 6b 65 0a 0a 20 20 20 20 20 |nts, like.. | 00000780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 | | 00000790 20 69 20 3d 20 69 6e 70 75 74 28 29 0a 0a 61 72 | i = input()..ar| 000007a0 65 20 61 63 74 75 61 6c 6c 79 20 65 78 70 72 65 |e actually expre| 000007b0 73 73 69 6f 6e 73 2c 20 77 68 69 63 68 20 72 65 |ssions, which re| 000007c0 74 75 72 6e 20 61 20 76 61 6c 75 65 20 61 73 20 |turn a value as | 000007d0 77 65 6c 6c 20 61 73 20 70 72 6f 64 75 63 65 20 |well as produce | 000007e0 61 0a 73 69 64 65 2d 65 66 66 65 63 74 2c 20 61 |a.side-effect, a| 000007f0 6e 64 20 73 6f 20 74 68 69 73 20 76 61 6c 75 65 |nd so this value| 00000800 20 63 61 6e 20 62 65 20 70 61 72 74 20 6f 66 20 | can be part of | 00000810 61 20 6c 61 72 67 65 72 20 65 78 70 72 65 73 73 |a larger express| 00000820 69 6f 6e 0a 0a 20 20 20 20 20 20 20 20 20 20 20 |ion.. | 00000830 20 20 20 20 20 20 20 28 69 20 3d 20 69 6e 70 75 | (i = inpu| 00000840 74 28 29 29 20 21 3d 20 22 30 22 0a 0a 54 68 69 |t()) != "0"..Thi| 00000850 73 20 69 73 20 6e 6f 74 20 74 68 65 20 63 61 73 |s is not the cas| 00000860 65 20 69 6e 20 42 61 73 69 63 2e 20 52 65 6d 65 |e in Basic. Reme| 00000870 6d 62 65 72 20 74 68 61 74 20 42 61 73 69 63 20 |mber that Basic | 00000880 75 73 65 73 20 27 3d 27 20 77 69 74 68 20 74 77 |uses '=' with tw| 00000890 6f 0a 6d 65 61 6e 69 6e 67 73 20 2d 20 61 73 73 |o.meanings - ass| 000008a0 69 67 6e 6d 65 6e 74 20 61 6e 64 20 74 65 73 74 |ignment and test| 000008b0 20 66 6f 72 20 65 71 75 61 6c 69 74 79 2e 20 49 | for equality. I| 000008c0 6e 20 43 2c 20 61 6e 64 20 41 72 6d 42 6f 62 2c |n C, and ArmBob,| 000008d0 20 74 68 65 73 65 0a 74 77 6f 20 75 73 65 73 20 | these.two uses | 000008e0 61 72 65 20 64 69 73 74 69 6e 67 75 69 73 68 65 |are distinguishe| 000008f0 64 2e 20 41 73 73 69 67 6e 6d 65 6e 74 20 75 73 |d. Assignment us| 00000900 65 73 20 27 3d 27 2c 20 74 65 73 74 20 66 6f 72 |es '=', test for| 00000910 20 65 71 75 61 6c 69 74 79 20 75 73 65 73 0a 27 | equality uses.'| 00000920 3d 3d 27 2e 20 41 6e 6f 74 68 65 72 20 63 61 74 |=='. Another cat| 00000930 63 68 20 66 6f 72 20 74 68 65 20 42 61 73 69 63 |ch for the Basic| 00000940 20 70 72 6f 67 72 61 6d 6d 65 72 20 75 6e 75 73 | programmer unus| 00000950 65 64 20 74 6f 20 43 20 6c 75 72 6b 73 20 69 6e |ed to C lurks in| 00000960 0a 74 68 65 20 73 77 69 74 63 68 20 73 74 61 74 |.the switch stat| 00000970 65 6d 65 6e 74 2e 20 49 6e 20 42 61 73 69 63 27 |ement. In Basic'| 00000980 73 20 43 41 53 45 20 2e 2e 2e 20 57 48 45 4e 20 |s CASE ... WHEN | 00000990 2e 2e 2e 2e 20 45 4e 44 43 41 53 45 20 63 6f 6e |.... ENDCASE con| 000009a0 73 74 72 75 63 74 69 6f 6e 0a 65 78 65 63 75 74 |struction.execut| 000009b0 69 6f 6e 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c |ion automaticall| 000009c0 79 20 6a 75 6d 70 73 20 74 6f 20 61 66 74 65 72 |y jumps to after| 000009d0 20 74 68 65 20 45 4e 44 43 41 53 45 20 66 72 6f | the ENDCASE fro| 000009e0 6d 20 74 68 65 20 65 6e 64 20 6f 66 0a 74 68 65 |m the end of.the| 000009f0 20 63 6f 64 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 | code following | 00000a00 61 20 57 48 45 4e 20 63 6f 6e 64 69 74 69 6f 6e |a WHEN condition| 00000a10 2e 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 |. On the other h| 00000a20 61 6e 64 20 69 6e 20 43 27 73 2c 20 61 6e 64 20 |and in C's, and | 00000a30 0a 41 72 6d 42 6f 62 27 73 2c 20 73 77 69 74 63 |.ArmBob's, switc| 00000a40 68 20 28 2e 2e 29 7b 20 2e 2e 2e 2e 20 63 61 73 |h (..){ .... cas| 00000a50 65 20 2e 2e 2e 2e 20 7d 20 63 6f 6e 73 74 72 75 |e .... } constru| 00000a60 63 74 69 6f 6e 2c 20 65 78 65 63 75 74 69 6f 6e |ction, execution| 00000a70 20 64 6f 65 73 0a 4e 4f 54 20 61 75 74 6f 6d 61 | does.NOT automa| 00000a80 74 69 63 61 6c 6c 79 20 6a 75 6d 70 20 74 6f 20 |tically jump to | 00000a90 61 66 74 65 72 20 74 68 65 20 63 6c 6f 73 69 6e |after the closin| 00000aa0 67 20 62 72 61 63 65 2c 20 75 6e 6c 65 73 73 20 |g brace, unless | 00000ab0 73 70 65 63 69 66 69 63 61 6c 6c 79 0a 64 69 72 |specifically.dir| 00000ac0 65 63 74 65 64 20 74 6f 20 64 6f 20 73 6f 20 77 |ected to do so w| 00000ad0 69 74 68 20 61 20 27 62 72 65 61 6b 3b 27 20 73 |ith a 'break;' s| 00000ae0 74 61 74 65 6d 65 6e 74 2e 20 46 6f 72 67 65 74 |tatement. Forget| 00000af0 74 69 6e 67 20 74 68 65 20 27 62 72 65 61 6b 3b |ting the 'break;| 00000b00 27 0a 69 73 20 61 20 66 72 65 71 75 65 6e 74 20 |'.is a frequent | 00000b10 73 6f 75 72 63 65 20 6f 66 20 65 72 72 6f 72 2e |source of error.| 00000b20 0a 0a 54 68 65 20 73 77 69 74 63 68 20 73 74 61 |..The switch sta| 00000b30 74 65 6d 65 6e 74 20 69 6e 20 6d 61 69 6e 20 69 |tement in main i| 00000b40 73 20 75 73 65 64 20 74 6f 20 64 69 73 70 61 74 |s used to dispat| 00000b50 63 68 20 65 78 65 63 75 74 69 6f 6e 20 74 6f 20 |ch execution to | 00000b60 76 61 72 69 6f 75 73 0a 61 6c 74 65 72 6e 61 74 |various.alternat| 00000b70 69 76 65 20 66 75 6e 63 74 69 6f 6e 73 2c 20 74 |ive functions, t| 00000b80 68 65 20 66 69 72 73 74 20 6f 66 20 77 68 69 63 |he first of whic| 00000b90 68 20 69 73 20 6e 65 77 5f 61 63 63 6f 75 6e 74 |h is new_account| 00000ba0 2e 20 4e 6f 74 65 20 74 68 65 20 6c 69 6e 65 0a |. Note the line.| 00000bb0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 |. | 00000bc0 20 61 6d 6f 75 6e 74 20 3d 20 76 61 6c 28 69 6e | amount = val(in| 00000bd0 70 75 74 28 29 29 3b 0a 0a 54 68 65 20 27 69 6e |put());..The 'in| 00000be0 70 75 74 27 20 66 75 6e 63 74 69 6f 6e 20 61 6c |put' function al| 00000bf0 77 61 79 73 20 70 72 6f 64 75 63 65 73 20 61 20 |ways produces a | 00000c00 73 74 72 69 6e 67 2e 20 54 68 65 20 66 75 6e 63 |string. The func| 00000c10 74 69 6f 6e 20 27 76 61 6c 27 20 0a 63 6f 6e 76 |tion 'val' .conv| 00000c20 65 72 74 73 20 61 20 73 74 72 69 6e 67 20 74 6f |erts a string to| 00000c30 20 61 20 6e 75 6d 62 65 72 2c 20 61 73 20 66 61 | a number, as fa| 00000c40 72 20 61 73 20 70 6f 73 73 69 62 6c 65 2e 20 54 |r as possible. T| 00000c50 68 65 20 6e 65 78 74 20 6c 69 6e 65 0a 0a 20 20 |he next line.. | 00000c60 20 20 20 20 20 63 6c 69 65 6e 74 5b 61 63 63 6f | client[acco| 00000c70 75 6e 74 5f 6e 6f 2b 2b 5d 20 3d 20 6e 65 77 20 |unt_no++] = new | 00000c80 61 63 63 6f 75 6e 74 28 61 6d 6f 75 6e 74 2c 6e |account(amount,n| 00000c90 61 6d 65 29 3b 0a 0a 75 73 65 73 20 74 68 65 20 |ame);..uses the | 00000ca0 70 6f 73 74 2d 69 6e 63 72 65 6d 65 6e 74 20 6f |post-increment o| 00000cb0 70 65 72 61 74 6f 72 20 27 2b 2b 27 2e 20 54 68 |perator '++'. Th| 00000cc0 69 73 20 69 73 20 61 6e 20 65 78 61 6d 70 6c 65 |is is an example| 00000cd0 20 6f 66 20 61 20 75 6e 61 72 79 0a 61 73 73 69 | of a unary.assi| 00000ce0 67 6e 6d 65 6e 74 20 6f 70 65 72 61 74 6f 72 2e |gnment operator.| 00000cf0 20 54 68 65 20 65 66 66 65 63 74 20 6f 66 20 74 | The effect of t| 00000d00 68 69 73 20 6f 6e 65 20 6c 69 6e 65 20 69 73 20 |his one line is | 00000d10 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 0a 0a 20 |equivalent to.. | 00000d20 20 20 20 20 20 20 63 6c 69 65 6e 74 5b 61 63 63 | client[acc| 00000d30 6f 75 6e 74 5f 6e 6f 5d 20 3d 20 6e 65 77 20 61 |ount_no] = new a| 00000d40 63 63 6f 75 6e 74 28 61 6d 6f 75 6e 74 2c 6e 61 |ccount(amount,na| 00000d50 6d 65 29 3b 0a 20 20 20 20 20 20 20 61 63 63 6f |me);. acco| 00000d60 75 6e 74 5f 6e 6f 20 3d 20 61 63 63 6f 75 6e 74 |unt_no = account| 00000d70 5f 6e 6f 20 2b 20 31 3b 0a 0a 54 68 65 20 76 65 |_no + 1;..The ve| 00000d80 63 74 6f 72 20 27 63 6c 69 65 6e 74 27 20 69 73 |ctor 'client' is| 00000d90 20 62 65 69 6e 67 20 75 73 65 64 20 74 6f 20 73 | being used to s| 00000da0 74 6f 72 65 20 61 6c 6c 20 74 68 65 20 61 63 63 |tore all the acc| 00000db0 6f 75 6e 74 73 20 69 6e 20 61 20 73 69 6e 67 6c |ounts in a singl| 00000dc0 65 0a 73 74 72 75 63 74 75 72 65 2e 0a 0a 4e 6f |e.structure...No| 00000dd0 74 65 20 74 68 61 74 20 77 65 20 68 61 76 65 20 |te that we have | 00000de0 66 75 6e 63 74 69 6f 6e 73 20 27 77 69 74 68 64 |functions 'withd| 00000df0 72 61 77 27 2c 20 27 73 74 61 74 65 6d 65 6e 74 |raw', 'statement| 00000e00 27 2c 20 27 63 68 61 6e 67 65 5f 70 61 73 73 77 |', 'change_passw| 00000e10 6f 72 64 27 0a 64 65 73 70 69 74 65 20 74 68 65 |ord'.despite the| 00000e20 20 66 61 63 74 20 74 68 61 74 20 74 68 65 73 65 | fact that these| 00000e30 20 61 72 65 20 6e 61 6d 65 73 20 6f 66 20 6d 65 | are names of me| 00000e40 74 68 6f 64 73 20 66 6f 72 20 74 68 65 20 27 61 |thods for the 'a| 00000e50 63 63 6f 75 6e 74 27 0a 63 6c 61 73 73 2e 20 54 |ccount'.class. T| 00000e60 68 65 72 65 20 69 73 20 6e 6f 20 63 6c 61 73 68 |here is no clash| 00000e70 2c 20 62 65 63 61 75 73 65 20 6d 65 74 68 6f 64 |, because method| 00000e80 73 20 61 72 65 20 70 72 69 76 61 74 65 20 74 6f |s are private to| 00000e90 20 74 68 65 20 63 6c 61 73 73 0a 74 68 65 79 20 | the class.they | 00000ea0 62 65 6c 6f 6e 67 20 74 6f 2e 20 41 6c 6c 20 74 |belong to. All t| 00000eb0 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 75 |hese functions u| 00000ec0 73 65 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 |se the function | 00000ed0 27 67 65 74 5f 61 63 63 6f 75 6e 74 27 0a 77 68 |'get_account'.wh| 00000ee0 69 63 68 20 75 73 65 73 20 74 68 65 20 27 68 61 |ich uses the 'ha| 00000ef0 73 5f 6f 77 6e 65 72 27 20 6d 65 74 68 6f 64 20 |s_owner' method | 00000f00 74 6f 20 66 69 6e 64 20 77 68 69 63 68 20 61 63 |to find which ac| 00000f10 63 6f 75 6e 74 73 2c 20 69 66 20 61 6e 79 2c 20 |counts, if any, | 00000f20 68 61 76 65 0a 61 6e 20 6f 77 6e 65 72 20 27 6e |have.an owner 'n| 00000f30 61 6d 65 27 2e 20 49 74 20 72 65 74 75 72 6e 73 |ame'. It returns| 00000f40 20 27 74 68 65 5f 61 63 63 6f 75 6e 74 27 2c 20 | 'the_account', | 00000f50 73 6f 20 74 68 61 74 20 74 68 65 20 6f 70 65 72 |so that the oper| 00000f60 61 74 6f 72 0a 0a 20 20 20 20 20 20 20 20 20 20 |ator.. | 00000f70 20 63 6c 69 65 6e 74 5b 74 68 65 5f 61 63 63 6f | client[the_acco| 00000f80 75 6e 74 5d 2d 3e 0a 0a 63 61 6e 20 62 65 20 61 |unt]->..can be a| 00000f90 70 70 6c 69 65 64 20 74 6f 20 74 68 65 20 61 70 |pplied to the ap| 00000fa0 70 72 6f 70 72 69 61 74 65 20 6d 65 74 68 6f 64 |propriate method| 00000fb0 20 74 6f 20 64 6f 20 77 68 61 74 20 68 61 73 20 | to do what has | 00000fc0 62 65 65 6e 20 72 65 71 75 65 73 74 65 64 2e 0a |been requested..| 00000fd0 0a 54 68 65 20 68 65 61 72 74 20 6f 66 20 67 65 |.The heart of ge| 00000fe0 74 5f 61 63 63 6f 75 6e 74 20 69 73 20 74 68 65 |t_account is the| 00000ff0 20 73 65 61 72 63 68 20 66 6f 72 20 74 68 65 20 | search for the | 00001000 61 63 63 6f 75 6e 74 20 6f 77 6e 65 64 20 62 79 |account owned by| 00001010 20 27 6e 61 6d 65 27 3a 0a 0a 20 77 68 69 6c 65 | 'name':.. while| 00001020 20 28 28 74 68 65 5f 61 63 63 6f 75 6e 74 3c 61 | ((the_account<a| 00001030 63 63 6f 75 6e 74 5f 6e 6f 29 0a 20 20 20 20 20 |ccount_no). | 00001040 20 20 20 26 26 20 21 28 63 6c 69 65 6e 74 5b 74 | && !(client[t| 00001050 68 65 5f 61 63 63 6f 75 6e 74 5d 2d 3e 68 61 73 |he_account]->has| 00001060 5f 6f 77 6e 65 72 28 6e 61 6d 65 29 29 29 0a 20 |_owner(name))). | 00001070 20 20 20 74 68 65 5f 61 63 63 6f 75 6e 74 2b 2b | the_account++| 00001080 3b 0a 0a 54 68 65 20 26 26 20 6f 70 65 72 61 74 |;..The && operat| 00001090 6f 72 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 |or corresponds t| 000010a0 6f 20 42 61 73 69 63 27 73 20 41 4e 44 2c 20 62 |o Basic's AND, b| 000010b0 75 74 20 69 74 20 68 61 73 20 61 6e 20 69 6d 70 |ut it has an imp| 000010c0 6f 72 74 61 6e 74 0a 61 64 76 61 6e 74 61 67 65 |ortant.advantage| 000010d0 20 2d 20 69 74 20 69 73 20 6c 61 7a 79 21 20 43 | - it is lazy! C| 000010e0 6f 6d 70 61 72 65 20 74 68 65 73 65 20 74 77 6f |ompare these two| 000010f0 20 70 72 6f 67 72 61 6d 73 2c 20 74 68 65 20 66 | programs, the f| 00001100 69 72 73 74 20 69 6e 20 42 61 73 69 63 2c 0a 74 |irst in Basic,.t| 00001110 68 65 20 73 65 63 6f 6e 64 20 69 6e 20 41 72 6d |he second in Arm| 00001120 42 6f 62 2e 0a 0a 54 68 65 20 42 61 73 69 63 20 |Bob...The Basic | 00001130 76 65 72 73 69 6f 6e 3a 0a 0a 20 20 20 20 20 20 |version:.. | 00001140 20 20 20 20 52 45 4d 20 41 4e 44 20 69 73 20 73 | REM AND is s| 00001150 74 72 69 63 74 0a 20 20 20 20 20 20 20 20 20 20 |trict. | 00001160 49 46 20 46 4e 66 20 41 4e 44 20 46 4e 67 20 54 |IF FNf AND FNg T| 00001170 48 45 4e 20 50 52 49 4e 54 20 22 79 65 73 22 20 |HEN PRINT "yes" | 00001180 45 4c 53 45 20 50 52 49 4e 54 20 22 6e 6f 22 0a |ELSE PRINT "no".| 00001190 20 20 20 20 20 20 20 20 20 20 45 4e 44 0a 0a 20 | END.. | 000011a0 20 20 20 20 20 20 20 20 20 44 45 46 20 46 4e 66 | DEF FNf| 000011b0 0a 20 20 20 20 20 20 20 20 20 20 50 52 49 4e 54 |. PRINT| 000011c0 20 22 3c 3c 45 76 61 6c 75 61 74 69 6e 67 20 66 | "<<Evaluating f| 000011d0 3e 3e 22 0a 20 20 20 20 20 20 20 20 20 20 3d 20 |>>". = | 000011e0 30 0a 0a 20 20 20 20 20 20 20 20 20 20 44 45 46 |0.. DEF| 000011f0 20 46 4e 67 0a 20 20 20 20 20 20 20 20 20 20 50 | FNg. P| 00001200 52 49 4e 54 20 22 3c 3c 45 76 61 6c 75 61 74 69 |RINT "<<Evaluati| 00001210 6e 67 20 67 3e 3e 22 0a 20 20 20 20 20 20 20 20 |ng g>>". | 00001220 20 20 3d 20 30 0a 0a 54 68 65 20 41 72 6d 42 6f | = 0..The ArmBo| 00001230 62 20 76 65 72 73 69 6f 6e 3a 0a 0a 20 20 20 20 |b version:.. | 00001240 20 20 20 20 20 20 2f 2a 20 26 26 20 69 73 20 4e | /* && is N| 00001250 4f 54 20 73 74 72 69 63 74 20 2a 2f 0a 20 20 20 |OT strict */. | 00001260 20 20 20 20 20 20 20 6d 61 69 6e 28 29 0a 20 20 | main(). | 00001270 20 20 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 | {. | 00001280 20 20 20 20 20 69 66 20 28 20 66 28 29 20 26 26 | if ( f() &&| 00001290 20 67 28 29 20 29 20 70 72 69 6e 74 28 22 79 65 | g() ) print("ye| 000012a0 73 5c 6e 22 29 3b 0a 20 20 20 20 20 20 20 20 20 |s\n");. | 000012b0 20 20 65 6c 73 65 20 70 72 69 6e 74 28 22 6e 6f | else print("no| 000012c0 5c 6e 22 29 3b 0a 20 20 20 20 20 20 20 20 20 20 |\n");. | 000012d0 20 7d 0a 0a 20 20 20 20 20 20 20 20 20 20 20 66 | }.. f| 000012e0 28 29 0a 20 20 20 20 20 20 20 20 20 20 20 7b 0a |(). {.| 000012f0 20 20 20 20 20 20 20 20 20 20 20 20 70 72 69 6e | prin| 00001300 74 28 22 3c 3c 45 76 61 6c 75 61 74 69 6e 67 20 |t("<<Evaluating | 00001310 66 3e 3e 5c 6e 22 29 3b 0a 20 20 20 20 20 20 20 |f>>\n");. | 00001320 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 | return 0;. | 00001330 20 20 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 | }.. | 00001340 20 20 20 20 20 20 20 20 67 28 29 0a 20 20 20 20 | g(). | 00001350 20 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 | {. | 00001360 20 20 20 20 20 70 72 69 6e 74 28 22 3c 3c 45 76 | print("<<Ev| 00001370 61 6c 75 61 74 69 6e 67 20 67 3e 3e 5c 6e 22 29 |aluating g>>\n")| 00001380 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 65 |;. re| 00001390 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 20 20 |turn 0;. | 000013a0 20 20 20 7d 0a 0a 54 68 65 20 42 61 73 69 63 20 | }..The Basic | 000013b0 70 72 6f 67 72 61 6d 20 70 72 6f 64 75 63 65 73 |program produces| 000013c0 20 61 73 20 6f 75 74 70 75 74 0a 0a 20 20 20 20 | as output.. | 000013d0 20 20 20 20 20 20 3c 3c 45 76 61 6c 75 61 74 69 | <<Evaluati| 000013e0 6e 67 20 66 3e 3e 0a 20 20 20 20 20 20 20 20 20 |ng f>>. | 000013f0 20 3c 3c 45 76 61 6c 75 61 74 69 6e 67 20 67 3e | <<Evaluating g>| 00001400 3e 0a 20 20 20 20 20 20 20 20 20 20 6e 6f 0a 0a |>. no..| 00001410 77 68 65 72 65 61 73 20 74 68 65 20 41 72 6d 42 |whereas the ArmB| 00001420 6f 62 20 70 72 6f 67 72 61 6d 20 70 72 6f 64 75 |ob program produ| 00001430 63 65 73 0a 0a 20 20 20 20 20 20 20 20 20 20 3c |ces.. <| 00001440 3c 45 76 61 6c 75 61 74 69 6e 67 20 66 3e 3e 0a |<Evaluating f>>.| 00001450 20 20 20 20 20 20 20 20 20 20 6e 6f 0a 20 20 20 | no. | 00001460 20 20 20 20 20 20 20 0a 49 6e 20 6f 74 68 65 72 | .In other| 00001470 20 77 6f 72 64 73 2c 20 26 26 20 64 6f 65 73 20 | words, && does | 00001480 6e 6f 74 20 62 6f 74 68 65 72 20 74 6f 20 65 76 |not bother to ev| 00001490 61 6c 75 61 74 65 20 69 74 73 20 72 69 67 68 74 |aluate its right| 000014a0 20 68 61 6e 64 20 61 72 67 75 6d 65 6e 74 0a 77 | hand argument.w| 000014b0 68 65 6e 20 69 74 20 66 69 6e 64 73 20 74 68 61 |hen it finds tha| 000014c0 74 20 69 74 73 20 6c 65 66 74 20 68 61 6e 64 20 |t its left hand | 000014d0 61 72 67 75 6d 65 6e 74 20 69 73 20 7a 65 72 6f |argument is zero| 000014e0 2c 20 77 68 65 72 65 61 73 20 41 4e 44 20 61 6c |, whereas AND al| 000014f0 77 61 79 73 0a 65 76 61 6c 75 61 74 65 73 20 62 |ways.evaluates b| 00001500 6f 74 68 2e 20 54 68 69 73 20 69 73 20 73 6d 61 |oth. This is sma| 00001510 72 74 20 6f 66 20 26 26 20 61 6e 64 20 64 75 6d |rt of && and dum| 00001520 62 20 6f 66 20 41 4e 44 20 62 65 63 61 75 73 65 |b of AND because| 00001530 20 79 6f 75 20 6f 66 74 65 6e 0a 6e 65 65 64 2c | you often.need,| 00001540 20 61 73 20 68 65 72 65 20 69 6e 20 67 65 74 5f | as here in get_| 00001550 61 63 63 6f 75 6e 74 2c 20 74 6f 20 74 65 73 74 |account, to test| 00001560 20 74 77 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 20 | two conditions | 00001570 77 68 65 72 65 20 65 72 72 6f 72 2d 66 72 65 65 |where error-free| 00001580 0a 65 76 61 6c 75 61 74 69 6f 6e 20 6f 66 20 74 |.evaluation of t| 00001590 68 65 20 73 65 63 6f 6e 64 20 63 6f 6e 64 69 74 |he second condit| 000015a0 69 6f 6e 20 69 73 20 63 6f 6e 74 69 6e 67 65 6e |ion is contingen| 000015b0 74 20 75 70 6f 6e 20 74 68 65 20 73 75 63 63 65 |t upon the succe| 000015c0 73 73 20 6f 66 0a 74 68 65 20 66 69 72 73 74 20 |ss of.the first | 000015d0 63 6f 6e 64 69 74 69 6f 6e 2e 20 54 68 69 73 20 |condition. This | 000015e0 75 73 75 61 6c 6c 79 20 68 61 70 70 65 6e 73 20 |usually happens | 000015f0 77 69 74 68 20 76 65 63 74 6f 72 73 20 6f 72 20 |with vectors or | 00001600 61 72 72 61 79 73 2c 20 77 68 65 72 65 0a 74 68 |arrays, where.th| 00001610 65 20 66 69 72 73 74 20 63 6f 6e 64 69 74 69 6f |e first conditio| 00001620 6e 20 63 68 65 63 6b 73 20 74 68 61 74 20 61 6e |n checks that an| 00001630 20 69 6e 64 65 78 20 6c 69 65 73 20 77 69 74 68 | index lies with| 00001640 69 6e 20 69 74 73 20 62 6f 75 6e 64 73 2c 20 61 |in its bounds, a| 00001650 6e 64 0a 69 66 20 69 74 20 69 73 2c 20 74 68 65 |nd.if it is, the| 00001660 20 73 65 63 6f 6e 64 20 63 6f 6e 64 69 74 69 6f | second conditio| 00001670 6e 20 63 68 65 63 6b 73 20 73 6f 6d 65 20 65 78 |n checks some ex| 00001680 70 72 65 73 73 69 6f 6e 20 69 6e 76 6f 6c 76 69 |pression involvi| 00001690 6e 67 20 74 68 65 20 0a 63 6f 6d 70 6f 6e 65 6e |ng the .componen| 000016a0 74 20 67 69 76 65 6e 20 62 79 20 74 68 65 20 69 |t given by the i| 000016b0 6e 64 65 78 2e 0a 0a 49 6e 20 74 68 65 20 73 61 |ndex...In the sa| 000016c0 6d 65 20 77 61 79 20 43 27 73 20 61 6e 64 20 41 |me way C's and A| 000016d0 72 6d 42 6f 62 27 73 20 7c 7c 20 69 73 20 6c 61 |rmBob's || is la| 000016e0 7a 79 20 77 68 65 72 65 20 42 61 73 69 63 27 73 |zy where Basic's| 000016f0 20 4f 52 20 69 73 20 73 74 72 69 63 74 2e 0a 49 | OR is strict..I| 00001700 66 20 74 68 65 20 6c 65 66 74 20 68 61 6e 64 20 |f the left hand | 00001710 61 72 67 75 6d 65 6e 74 20 6f 66 20 7c 7c 20 69 |argument of || i| 00001720 73 20 74 72 75 65 2c 20 74 68 65 20 72 69 67 68 |s true, the righ| 00001730 74 20 68 61 6e 64 20 61 72 67 75 6d 65 6e 74 20 |t hand argument | 00001740 64 6f 65 73 0a 6e 6f 74 20 67 65 74 20 65 76 61 |does.not get eva| 00001750 6c 75 61 74 65 64 2e 0a 0a |luated...| 00001759