Difference between revisions of "Basic Pascal Tutorial/Chapter 1/Punctuation and Indentation"

From Lazarus wiki
Jump to navigationJump to search
m (Fixed syntax highlighting)
(7 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 +
{{Punctuation_and_Indentation}}
 +
{{TYNavigator|Standard_Functions|Programming_Assignment}}
 +
 
1G - Punctuation and Indentation (author: Tao Yue, state: unchanged)
 
1G - Punctuation and Indentation (author: Tao Yue, state: unchanged)
  
Line 13: Line 16:
  
 
Indenting is not required. However, it is of great use for the programmer, since it helps to make the program clearer. If you wanted to, you could have a program look like this:
 
Indenting is not required. However, it is of great use for the programmer, since it helps to make the program clearer. If you wanted to, you could have a program look like this:
<font color="#000000">  1:</font> <font color="#006699"><strong>program</strong></font> Stupid<font color="#000000"><strong>;</strong></font> <font color="#006699"><strong>const</strong></font> a<font color="#000000"><strong>=</strong></font><font color="#ff0000">5</font><font color="#000000"><strong>;</strong></font> b<font color="#000000"><strong>=</strong></font><font color="#ff0000">385</font><font color="#000000"><strong>.</strong></font><font color="#ff0000">3</font><font color="#000000"><strong>;</strong></font> <font color="#006699"><strong>var</strong></font> alpha<font color="#000000"><strong>,</strong></font>beta<font color="#000000"><strong>:</strong></font><font color="#0099ff"><strong>real</strong></font><font color="#000000"><strong>;</strong></font> <font color="#006699"><strong>begin</strong></font>
+
 
<font color="#000000">  2:</font> alpha <font color="#000000"><strong>:=</strong></font> a <font color="#000000"><strong>+</strong></font> b<font color="#000000"><strong>;</strong></font> beta<font color="#000000"><strong>:=</strong></font> b <font color="#000000"><strong>/</strong></font> a <font color="#006699"><strong>end</strong></font><font color="#000000"><strong>.</strong></font>
+
<syntaxhighlight lang=pascal>
 +
program Stupid; const a=5; b=385.3; var alpha,beta:real; begin  
 +
alpha := a + b; beta:= b / a end.
 +
</syntaxhighlight>
  
 
But it's much better for it to look like this:
 
But it's much better for it to look like this:
<font color="#000000">  1:</font> <font color="#006699"><strong>program</strong></font> NotAsStupid<font color="#000000"><strong>;</strong></font>
+
 
<font color="#000000">  2:</font>
+
<syntaxhighlight lang=pascal>
<font color="#000000">  3:</font> <font color="#006699"><strong>const</strong></font>
+
program NotAsStupid;
<font color="#000000">  4:</font>   a <font color="#000000"><strong>=</strong></font> <font color="#ff0000">5</font><font color="#000000"><strong>;</strong></font>
+
 
<font color="#990066">  5:</font>   b <font color="#000000"><strong>=</strong></font> <font color="#ff0000">385</font><font color="#000000"><strong>.</strong></font><font color="#ff0000">3</font><font color="#000000"><strong>;</strong></font>
+
const
<font color="#000000">  6:</font>
+
   a = 5;
<font color="#000000">  7:</font> <font color="#006699"><strong>var</strong></font>
+
   b = 385.3;
<font color="#000000">  8:</font>   alpha<font color="#000000"><strong>,</strong></font>
+
 
<font color="#000000">  9:</font>   beta <font color="#000000"><strong>:</strong></font> <font color="#0099ff"><strong>real</strong></font><font color="#000000"><strong>;</strong></font>
+
var
<font color="#990066">  10:</font>
+
   alpha,
<font color="#000000">  11:</font> <font color="#006699"><strong>begin</strong></font> <font color="#cc0000">(*</font><font color="#cc0000"> </font><font color="#cc0000">main</font><font color="#cc0000"> </font><font color="#cc0000">*)</font>
+
   beta : real;
<font color="#000000">  12:</font>   alpha <font color="#000000"><strong>:=</strong></font> a <font color="#000000"><strong>+</strong></font> b<font color="#000000"><strong>;</strong></font>
+
 
<font color="#000000">  13:</font>   beta <font color="#000000"><strong>:=</strong></font> b <font color="#000000"><strong>/</strong></font> a
+
begin (* main *)
<font color="#000000">  14:</font> <font color="#006699"><strong>end</strong></font><font color="#000000"><strong>.</strong></font> <font color="#cc0000">(*</font><font color="#cc0000"> </font><font color="#cc0000">main</font><font color="#cc0000"> </font><font color="#cc0000">*)</font>
+
   alpha := a + b;
 +
   beta := b / a;
 +
end. (* main *)
 +
</syntaxhighlight>
 +
 
 
In general, indent each block. Skip a line between blocks (such as between the const and var blocks). Modern programming environments (IDE, or Integrated Development Environment) understand Pascal syntax and will often indent for you as you type. You can customize the indentation to your liking (display a tab as three spaces or four?).
 
In general, indent each block. Skip a line between blocks (such as between the const and var blocks). Modern programming environments (IDE, or Integrated Development Environment) understand Pascal syntax and will often indent for you as you type. You can customize the indentation to your liking (display a tab as three spaces or four?).
  
 
Proper indentation makes it much easier to determine how code works, but is vastly aided by judicious commenting.
 
Proper indentation makes it much easier to determine how code works, but is vastly aided by judicious commenting.
  
{|style=color-backgroud="white" cellspacing="20"
+
{{TYNavigator|Standard_Functions|Programming_Assignment}}
|[[Standard_Functions|previous]] 
 
|[[op_contents|contents]]
 
|[[Programming_Assignment|next]]
 
|}
 

Revision as of 12:14, 23 February 2020

български (bg) Deutsch (de) English (en) français (fr) 日本語 (ja) 한국어 (ko) русский (ru) 中文(中国大陆)‎ (zh_CN)

 ◄   ▲   ► 

1G - Punctuation and Indentation (author: Tao Yue, state: unchanged)

Since Pascal ignores end-of-lines and spaces, punctuation is needed to tell the compiler when a statement ends.

You must have a semicolon following:

  • the program heading
  • each constant definition
  • each variable declaration
  • each type definition (to be discussed later)
  • almost all statements

The last statement in a BEGIN-END block, the one immediately preceding the END, does not require a semicolon. However, it's harmless to add one, and it saves you from having to add a semicolon if suddenly you had to move the statement higher up.

Indenting is not required. However, it is of great use for the programmer, since it helps to make the program clearer. If you wanted to, you could have a program look like this:

program Stupid; const a=5; b=385.3; var alpha,beta:real; begin 
alpha := a + b; beta:= b / a end.

But it's much better for it to look like this:

program NotAsStupid;

const
  a = 5;
  b = 385.3;

var
  alpha,
  beta : real;

begin (* main *)
  alpha := a + b;
  beta := b / a;
end. (* main *)

In general, indent each block. Skip a line between blocks (such as between the const and var blocks). Modern programming environments (IDE, or Integrated Development Environment) understand Pascal syntax and will often indent for you as you type. You can customize the indentation to your liking (display a tab as three spaces or four?).

Proper indentation makes it much easier to determine how code works, but is vastly aided by judicious commenting.

 ◄   ▲   ►