______________________________________________________________________________ | SECTION 2 SUBPART 3 | Structured Programming | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The point of this article is to make your programs flexible, easier to read and have a better style. Having your programs more structured will enable you to cross the path into the world of C, C++ and PASCAL far easier. It will make basic gurus look at your work in a different light. Anyway, that aside, at least you'll have the satisfaction of knowing you can program in a tidy structured way. A good first example would be the question 'Are you sure you want to exit?' at the end of a program. If you answer Y then the program would exit but if you answer N then the program will restart. You may use this method to accomplish that: 'METHOD1 lp2: 'main program goes in here lp1: print "Are you sure you want to exit?" a$ = input$(1) if a$ = "Y" then system if a$ = "N" then goto lp2: goto lp1: As you can see that looks better than the first method but in structured programming GOTO commands are *rarely* used. I cannot even remember how to do gotos in some structured languages! ;-) So how can we get rid of them? Well if we go back to one of the earliest things you learn when you study (and learn) BASIC, DO..WHILE loops... They can be used to return control to earlier points in the program. 'METHOD2 do 'main program goes in here do print "Are you sure you want to exit?" a$ = input$(1) loop until a$ = "Y" or a$ = "N" loop until a$ = "Y" That is a structured program although there is one thing we could amend to make the program have a small boost of quality. Change the line: a$ = input$(1) to: a$ = ucase$(input$(1)) So now the person doesn't have to press Y . They can press Y or just y. Or N and n. So let me go over a few dos and don'ts (please, it doesn't mean you're a bad programmer): Use of DO..WHILE loops Jumping out of FOR loops with GOTO Nesting loops cleverly Nesting loops very deeply Use of SYSTEM and not of END Unneccersary use of GOTO Just a few points there. I hope this all helps you see that it can be profitable to use more structured programming, the structured code even uses less lines (of proper code) than the original method shown! 8-) In processor (or graphic) intensive programs this is very important to save time and have the program running at its best. Another important thing that should be used in programs is 'proper subroutines'. In BASIC the way was always to go like this: print "This will say hello" GOSUB sayhello: print "Bye!" system sayhello: print "Hello!" return Nowadays this type of code should not be used. It originated in the first early basics and has only continued to be supported due to GWBASIC and also due to tradition. There is a better alternative (in QBASIC+up) now. You can create and edit subroutines separate from the main module (main part of program). If you select New Sub.. from the Edit menu (in QBASIC) then a dialog box will appear asking for what you want the subroutine to be called. In this case we want it to be called sayhello so type that and press ENTER. We are then shown a new piece of program starting with 'SUB sayhello' and ending with 'END SUB'. All of your code goes between these lines. If now in here we type: print "Hello!" We can go back to the main program by pressing F2 and going up to the main routine. If you press ENTER then it is flashed back up. If you replace GOSUB sayhello with CALL sayhello then it will call the new procedure you have created. So it will look like this: print "This will say hello" CALL sayhello print "Bye!" system SUB sayhello print "Hello!" END SUB That program is easier to edit, easier to understand and more stylish than the first method. If you have had any problems with this then mail me and I'll give you a hand. More examples on this next issue. Cheerio! -------------------------------------------------------- * EDITOR'S NOTE: * This article was originally printed in Peter Cooper's BASIX Fanzine, * Issue #4 from January 1996.