[Pascal – TUT] Posts 3: The command structure

1. Branch statement

the. Incomplete form
Syntax:

if <dieu_kien> then <cong_viec>;

If the condition is true, then perform the job (opposite condition is false does not do the job).

b. Full format
Syntax:

if <dieu_kien> then <cong_viec_1>
else <cong_viec_2>;

If the condition is true, then perform the job 1, opposite condition is false, the execution of work 2.
Note before ELSE no sign ; (semicolon).

* If the job done from 2 one or more commands to put them in the BEGIN and END keywords pair;

2. Select statement

the. Incomplete form
Syntax:

case <bieu_thuc> of
	Hang_1: <cong_viec_1>;
	Hang_2: <cong_viec_1>;
	...
	Hang_n: <cong_viec_1>;
end;

If the value of the constant expression does fall into the respective construction will be carried out before the end of the case statement.
If the value of the expression does not equal a constant case of the command ends without doing anything.

b. Full format
Syntax:

case <bieu_thuc> of
	Hang_1: <cong_viec_1>;
	Hang_2: <cong_viec_2>;
	...
	Hang_n: <cong_viec_n>
       	else <cong_viec_n+1>;
end;

If the value of the constant expression does fall into the respective construction will be carried out before the end of the case statement.
If the value of the expression does not equal a constant will perform the first n 1 and exit.
Noted:
+ The value hang_1, hang_2,…,hang_n be counted style (not the type of real).
+ The value hang_1, hang_2,…,hang_n can be enumerated type or the paragraph style

Example:
enumeration: 1,3,5,7
the,c,d
the paragraph style: 1..10 (2 dots)
a..z

3. The statement said the number of times previous iteration

the. Type 1:

for <bien>:=<gia_tri_dau> to <gia_tri_cuoi> do
	<cong_viec>;

– Step 1: Check first with values <= (less than or equal) end value or not. If true, then assign values ​​to variables and execute the first task.
– Step 2: Check the value of the variable <> (other) end value or not. If true, the increased processing unit (well:= NEXT(well)) and perform the work.
– Repeat steps 2, until the value of the variable with the value of the end of the end of the statement.
Noted:Turn right after the keyword for the counter and started to value <= Value end.

b. Type 2:

for <bien>:=<gia_tri_dau> downto <gia_tri_cuoi> do
	<cong_viec>;

– Step 1: Check first with values >= (less than or equal) end value or not. If true, then assign values ​​to variables and execute the first task.
– Step 2: Check the value of the variable <> (other) end value or not. If true, then turn down to a reduction unit(well:PRE =(well)) and perform the work.
– Repeat steps 2, until the value of the variable with the value of the end of the end of the statement.
Noted:Turn right after the keyword for the counter and started to value >= Value end.

Noted: Unlike other languages, Pascal is not checked (variable>final) in command FOR ... TO ... DO loop to the end that the test (variable = end) to perform the final iteration. Therefore the intervention on the counter can cause problems "endless loop". Even when the amplitude was browsing through all the range of data types (Instant values 255) Back Dynamics of value 0 ... And everything again ... unless typing Ctrl – Break.

4. The command loop with unknown number of times before

the. WHILE loop
Syntax:

while <dieu_kien> do
	<cong_viec>;

When met loop program will check conditions, if the condition is true, then execute the job, then back to check conditions. Just continue to do so until the condition is false, the end.
{While the condition is true, the work}.

b. REPEAT loop

Syntax:

repeat
	writeln('i =',i);
	i:=i+1;
until i>10;

When meeting program loop will execute the work, then check the condition, if the condition is false, then continue to do the work then check the condition. Just continue to do so until the condition is true, the end. {Work until the condition is true}.

Noted:

+ Unlike the loop Both loops While loops and Repeat are not predetermined number of iterations. Need a command to change the value of the variable control loop to exit the loop.
+ Within the while statement, the condition will be checked before, if the condition is true, then perform the job. And in the opposite order repeat, work is done first and then test conditions, if the condition is true, the loop ends. So to never repeat loop the loop body is also carried out at least once, while the body while loop can not be done any time.
+ If the user 2 order to solve the same problem, the same algorithm as the same condition after a while and then off until conditions are different negative.
+ The statements in the loop repeat do not need to put in the keywords BEGIN and END pairs;

pascal

* Some other commands related

the. Goto
– Syntax: goto label;
In that label is a label, label name is set by naming {known} or an integer from 0 to 9999
– Action: Upon meeting the program goto unconditional jump to the statement after the label.
– Noted: Goto allowed to jump from one location to another location within the same 1 function body, procedures, jumping out of the loop, not allowed to jump from outside to inside the loop, jaw, procedures, block command

b. Break statement
– Syntax: break;
– Action: break statement works when placed in the body of the loop, while, repeat. When having a break statement; the machine will exit the loop cycle, If there are multiple nested loop will exit loop containing the break statement;

c. Exit
– Syntax: Exit;
– Action: Exit command will terminate the program if it is placed in ctc, terminate the program if it is in the main program

d. Halt command
– Syntax: just;
– Action: Upon meeting the halt command, the machine will stop running programs. This command is often used when the algorithm meet 1 TH can not continue to be.


Original article : vietsource.net