PROGRAM my_first;
{short demonstration
program} VAR {by A Programmer} index : INTEGER; BEGIN FOR index := 1 TO 10 DO BEGIN WRITELN(index) END END. |
When you've written a program and typed it in, then the next stage is to compile it. A compiler checks through your program looking for syntax errors - any obvious mistakes that you might have made, or instructions that Pascal can't understand. If there are syntax errors, you'll need to correct them before going any further. This is sometimes the most difficult part of getting a program to work properly - check spellings and punctuation carefully! When your program compiles successfully you are ready to run the program and see if it does what it is supposed to...
Now for some comments - read them carefully and don't be tempted to skip over this part because it will help you understand what's coming next. We'll look at each line in turn: PROGRAM my_first; {short demonstration program} All Pascal programs start with the keyword PROGRAM followed by a name and then a semicolon. The rest of this line is enclosed by curly brackets. Anything in curly brackets is ignored by the computer - the reason it is there, though, is to act as a comment and to help the reader see what each part of the program does. When you write your own programs you should always include such comments. VAR {by A Programmer} VAR is short for variable. If your program uses any numbers - or variables - then this is where we say what they are called and what sort of variables they are. As before, the comment in curly brackets is ignored by the computer. index : INTEGER; ...our program uses just one variable. It is called index and it is a whole number, or integer. Now we come to the start of the main program. It begins, surprisingly enough, with... BEGIN Notice that the lines are now set in a little way from the left margin. This is called indenting and helps to make the program easier to read. FOR index := 1 TO 10 DO This is the start of what is called a loop. A loop is an instruction (or set of instructions) which is carried out repeatedly - this is exactly what computers are good at! BEGIN ...marks the start of the instruction(s) in the loop WRITELN(index) ...is the only instruction in the loop. It prints out the value of the variable index. Notice, again, that it has been indented. END ...marks the end of the loop and END. ...marks the end of the program. Note that there is only ever one full stop in a Pascal program, right at the end. Let's make one or two changes to the program:
So far, we've seen that a Pascal program is made up of a number of instructions. The file containing these instructions is compiled before the program can be run. Right now, you shouldn't worry too much if some of this seems a little strange. We'll start with some simple examples and take it from there... |
![]() ![]() Or Click Here! |
© 2001 by Mike Hardy |