Program Structure/es

From Lazarus wiki
Revision as of 20:41, 7 May 2010 by Iskraelectrica (talk | contribs) (New page: Bases 1A - Estructura de un Programa (author: Tao Yue, state: unchanged)    La estructura básica de un programa Pascal es: <delphi> PROGRAM NombrePrograma (ListaArchivos);...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Bases 1A - Estructura de un Programa (author: Tao Yue, state: unchanged)

   La estructura básica de un programa Pascal es: <delphi> PROGRAM NombrePrograma (ListaArchivos);

CONST
 (* Declaración de Constantes *)
TYPE
 (* Declaración de Tipos *)
VAR
 (* Declaración de Variables *)
(* Definiciones de Subprogramas *)
BEGIN
 (* Sentencias ejecutables *)
END.</delphi>

   Los elementos del programa deben aparecer en el orden correcto, aunque pueden ser omitidos si no se necesitan.     Aquí hay un programa que no hace nada, pero tiene todos los elementos necesarios: <delphi> program HacerNada;

begin
end.</delphi>

Los comentarios son partes del código que no se compilan ni ejecutan. Los comentarios en Pascal comienzan con (* y terminan con *). No se puede anidar comentarios. <delphi> { (* (* *) *)</delphi> will yield an error because the compiler matches the first '(*' with the first '*)', ignoring the second '(*' which is between the first set of comment markers. The second '*)' is left without its matching '(*'. This problem with begin-end comment markers is one reason why many languages use line-based commenting systems.

Turbo Pascal and most other modern compilers support brace comments, such as {Comment}. The opening brace signifies the beginning of a block of comments, and the ending brace signifies the end of a block of comments. Brace comments are also used for compiler directives.

Commenting makes your code easier to understand. If you write your code without comments, you may come back to it weeks, months, or years later without a guide to why you coded the program that way. In particular, you may want to document the major design of your program and insert comments in your code when you deviate from that design for a good reason.

In addition, comments are often used to take problematic code out of action without deleting it. Remember the earlier restriction on nesting comments? It just so happens that braces {} take precedence over parentheses-stars (* *). You will not get an error if you do this: <delphi> { (* Comment *) } </delphi>

Whitespace (spaces, tabs, and end-of-lines) are ignored by the Pascal compiler unless they are inside a literal string. However, to make your program readable by human beings, you should indent your statements and put separate statements on separate lines. Indentation is often an expression of individuality by programmers, but collaborative projects usually select one common style to allow everyone to work from the same page.

previous contents next