SB BBC @ GBR Oldie hints/tips #058-063 (rep.) Hints and tips from the archives of Wakefield BBC Micro User Group... 58. While...do structure ~~~~~~~~~~~~~~~~~~~~ You may have missed the presence of a WHILE-DO or WHILE-ENDWHILE in BBC Basic (it's in version5 as per the Arc). Eg. WHILE X%<100 DO X%=X%+1:A%=A%*B% or WHILE X%<100:X%=X%+1:A%=A%*B%:ENDWHILE (as on BBC BASIC V) REPEAT-UNTIL and FOR-NEXT loops have a disadvantage compared to WHILE-DO or WHILE-ENDWHILE loops in that they always execute at least once, whether they need to or not. In the examples above, the loop would not execute at all if X% was >=100. Well, you can imitate WHILE-DO or WHILE-ENDWHILE with the following: REPEAT IF X%<100 THEN X%=X%+1:A%=A%*B%:UNTIL FALSE ELSE UNTIL TRUE It may not look very elegant, but it avoids GOTOs, so that makes it structured in my book! (Source ... Beebug). 59. Variable names ~~~~~~~~~~~~~~ When people start programming in Basic, there is a tendency for them to stick to variable names like A$, B%, FRED etc.. This has three drawbacks. First, it is not very easy to pick out variables from keywords like FOR, TO, PRINT etc., as everything is in capitals. Second, it is easy to accidentally use a reserved word as part of a variable; TODAY is the classic mistake, as it starts with TO. Third, when you return to the program after a few days or weeks break, you will find it hard to remember just what A$ meant. The solution is to use lower-case variables whenever possible, and use long, meaningful variable names like "name$", "year%", "degrees" etc.. If you are really pushed for memory space, then using the single-letter integer variables A% to Z% can help, as can shorter variable names such as "nd$" instead of "newdate$", but by and large you will not need to resort to that. 60. Opening disc files ~~~~~~~~~~~~~~~~~~ When you open files on a disc for input, using the commands OPENIN or OPENUP, the syntax is basically chan=OPENIN("myfile"). The first file to be opened is allocated a channel number like 1, (ie. the variable 'chan' becomes 1), and if you open another file without closing the first, it is allocated the next channel number, (eg 2), and so on. The numbers don't necessarily start at 1; that's just an example. On most DFSs and ADFS, an interesting thing happens when using OPENIN or OPENUP, where the filename should already exist on disc, but cannot be found, either because you have put the wrong disc in, or maybe because you have mistyped the filename. Instead of causing an error, as you might expect, it simply allocates channel number zero. Thus, you have a very useful way of trapping the mistake before it actually causes an error. Eg. you could add something like: IF chan=0 THEN PRINT "Oops!":GOTO 200 There is no need to use CLOSE#, as the file hasn't been opened in the first place. Only when you attempt to use INPUT#/PRINT#/BGET#/BPUT# on this non-existent file, does the DFS return the error "Channel", (ERR=222). 61. Restore statement ~~~~~~~~~~~~~~~~~ It is wise always to use the full RESTORE (+ line number) syntax in programs containing DATA statements, even if they theoretically don't need them. If you don't, then rather strange things can happen if you change screen Mode in between reading DATA statements. This is because the Basic stack is stored immediately below HIMEM, and is therefore lost when you change Mode. For the same reason, you must not use MODE or CLEAR within Functions and Procedures, as the program will 'forget' where it has come from! Using MODE will give an error message, but using CLEAR will not, and it can cause all sorts of infuriating and unpredictable errors! 62. Abbreviations ~~~~~~~~~~~~~ You can replace PRINT:PRINT:PRINT with PRINT'' . In fact, an apostrophe will force a new line at any time; try typing PRINT"HELLO"'"FRED" and see what happens. (See User Guide, bottom of page 324.) You can also replace NEXT A:NEXT B:NEXT C with NEXT A,B,C and NEXT:NEXT:NEXT with NEXT,, . 63. Underlining in Wordwise ~~~~~~~~~~~~~~~~~~~~~~~ Owners of many types of printer may experience problems when centering underlined text in WORDWISE. The underlining tends to extend all the way to the left margin, instead of starting at the beginning of the heading. This effect also be seen if you are using the 'ds' and 'de' codes in Wordwise Plus, and are previewing in 80 column mode. The cure for this is to insert one space after the centering code, but before the underline code. If exact centering is vital, then an additional space after the final white control code would "balance" the first space. Better still, use the pad character, ("|" by default), instead of a space, so that it is clearly visible in Edit Mode. The {G} and {W} represent the green and white control codes respectively, and the printer codes used are for Epson-type printers. (You can substitute 'ds' and 'de' if you have Wordwise-Plus.) Before: {G}ce{G}oc27,45,1{W}THIS IS A HEADING{G}oc27,45,0{W} After : {G}ce{W}|{G}oc27,45,1{W}THIS IS A HEADING{G}oc27,45,0{W}| ^ pad character or space ^ 73 Rick G4BLT @ GB7WRG