Check out this example. To make things simple I am using old-school basic with line numbers - probably because I'm a retro-freak.
10 print "Hello World"
20 goto 10
Simpan kode di bawah ini di dalam postingan pada mode HTML setelah paragraf pertama atau kedua.
The best structure for writing any program code is to make it clear and easy to follow. Some programmers put multiple commands on one line which can make your code difficult to follow if you are trying to iron out bugs. Spreading your code over multiple lines actually makes the program work better and becomes more readable.
Another recommended practice is to separate each part of your program code using REM Statements. REM (short for Remark) allows you to put comments before each section of code to remind you what each part does. This is especially useful if you wish to edit your code at a later date.
10 rem Set Up Variables
20 let A=1: let B=2
30 rem *******
40 rem Print Variables to Screen
50 rem *******
60 print A,B
Anything after the REM command is ignored by the computer and you can use as many REM statements as you want to make bigger gaps in your code for easy reading. Other programming languages allow you to use blank lines or indent the first line of the routine.
Now I will show you how to structure the entire program code. Remember that the computer needs to follow step-by-step instructions so you need to write each instruction in the order you want it to run.
CONSTRUCTION OF CODE
Set up screen resolution and variables: The first section of your program would set the screen resolution and the variables.
Read information into arrays: If you have information you want to put into an array using the DIM command then you can use a For/Next loop and the READ command. It is best to place the data statements for the array to read from at the end of your program.
Set up main screen: This is the section where you would use a subroutine (GOSUB Command) to set up the main screen. In a shoot-em-up type game you would have a routine that draws the sprites and game screen and then returns to the next line of the code it came from.
Main Program Loop: Once the program is up and running the main program loop jumps to various routines using subroutines and then returns to the next line in the loop.
Program Routines: It is good structure to place all the programming routines after the main loop. You would have separate routines that update the screen, check for joystick input, check for collision detection and so on. After each check you return to the main loop.
Data Statements: Finally you can list all the data statements at the end of the program which makes it easier to find and correct if need be.
Creating your code with plenty of REM Statements and short lines makes your code look cleaner and easier to follow. There may be a time you want to improve the program or use a routine for another program.
Dean Sharples is a writer and programmer with many years experience. He has written articles on subjects such as Home Business, Retro, Religion and Programming. He is a Muslim and comes from Manchester, UK. You can find out more about Dean at: http://www.deansharples.com
Article Source: http://EzineArticles.com/10029278