Basic Pascal Tutorial/Chapter 1/Program Structure/ar

From Free Pascal wiki
Jump to navigationJump to search

العربية (ar) български (bg) Deutsch (de) English (en) español (es) français (fr) italiano (it) 日本語 (ja) 한국어 (ko) русский (ru) 中文(中国大陆)‎ (zh_CN)

 ◄   ▲   ► 

الأساسيات 1A - هيكلية البرنامج (المؤلف: Tao Yue, الحالة: تغيرت)

الهيكل الأساسي لبرنامج باسكال هو:

PROGRAM ProgramName (FileList);

CONST
  (* التصريح بالثوابت *)

TYPE
  (* التصريح بالأنواع *)

VAR
  (* التصريح بالمتغيرات *)

(* تعريفات الإجراءات والوظائف *)

BEGIN
  (* تعليمات قابلة للتنفيذ *)
END.

عبارة PROGRAM اختيارية في Free Pascal.

يجب أن تكون عناصر البرنامج بالترتيب الصحيح، على الرغم من إمكانية حذف بعضها إذا لم تكن هناك حاجة إليها. إليك هذا البرنامج الذي لا يفعل شيئًا، ولكنه يحتوي على جميع العناصر المطلوبة:

program DoNothing;
begin
end.

التعليقات هي أجزاء من التعليمات البرمجية التي لا يتم تجميعها أو تنفيذها. يدعم Free Pascal نوعين من التعليقات، التعليقات ذات الشكل الحر والتعليقات القائمة على الخطوط. تبدأ التعليقات ذات الشكل الحر إما بـ (* وتنتهي بـ *)، أو بشكل أكثر شيوعًا، تبدأ بـ { وتنتهي بـ }. يجب أن تكون حذرًا إذا قمت بدمج تعليقات النموذج الحر لأنها قد تؤدي إلى خطأ، على سبيل المثال:

  (* (* *) *)

على حسب المترجم سيتم، إما قبوله أو ينتج عنه خطأ لأنه،

  • إذا كان المترجم لا يسمح بالتعليقات المتداخلة, يطابق الأول '(*' مع الأول '*)', ويتجاهل الثاني '(*' الذي هو بين المجموعة الأولى من علامات التعليق, هذا يعني أن كل شيء بعد '*)' is treated as code. If there was nothing but blanks there, the second '*)' is not recognized and throws an error. OR
  • The second '(*' starts a nested comment, everything up to the next '*)' is in the nested comment, then the last '*)' closes the comment.

The same thing applies to '{' comments. Each '{' must be closed by '}' and if the compiler supports nested comments, all comments must be closed with the same marker as the one that opened it. So if you have code consisting of:

        (* {  *)

OR

        { (*  }

Will cause and "unexpected end of file" error, because the inner comment was not closed.

It's usually best to use one type of comment exclusively, and save the other for when you want to mark off a piece of code you (temporarily) don't want to use, like:

(*
    bad code that does't work {comment}
    more bad code // and comments
*)

This problem with comment markers is one reason why many languages offer line-based commenting systems.

Free Pascal also supports // as a line-based comment. When two slashes appear, other than in a quoted string or a free-form comment, the rest of the line is ignored.

Turbo Pascal and most other modern compilers support free-form 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:

{ (* Comment *) }

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.

 ◄   ▲   ►