1. Preliminary In some cases, a program sometimes have to do the same thing over and over again. In order for a program does not become too long because every process into line program, it is necessary automation capabilities in program code. With the automation in the program, it can do the same work processes that are many times in recent statements by writing only once. Because repetition is something that is often encountered in the program, the Pascal language also provides that ability. In Pascal there are 3 ways to do the loop, by using the reserve word For .. to .. do, said reserve While .. do, and reserve the word Repeat .. Until. As with branching, looping the third type also allows a nested loop, and can run a block contains a few statements for each perulangannya process. For .. to .. do commonly used when it is known with certainty counting loop that will be done either from a line statement or a block of statements. Loop / loop using the words of this reserve can be either positive looping (For .. to .. do) or negative loops (For downto .. .. do).
2. The word used reserve Program: The specification name of the program and its parameters, is decorative and not affect the overall program itself Uses: define the name of the unit to be referenced by the program Var: linking an identifier and its type with a location in memory where the values of these types can be stored Begin: starting a program block End: end a program block For: defines the initial value iteration To: defines the value of the final iteration (iteration positive) Downto: define the value end of the loop (negative loop) While: defines the expression that controls loop execution of a statement or block of statements Do: used to describe the action to perform looping Repeat: define statement or block of statements that will be repeated until the state stops filled Until: defines the state of rest for the loop that begins with the Repeat If: mention the conditions for a statement that can be executed Then: mention the statement of action must be executed when the conditions are met Else: mention the statement of actions that can be executed while another action statement is not executed because the condition is not fulfilled
3. The procedure used Write: writing a variable into a component file in a text file used to write one or more values to a file Writeln: executing the write procedure, then pulled out a marker of end-of-line (EOL) to the program files Read: read a file into a variable component, in a text file used to read one or more values into one or more variables Readln: executes a read procedure, and then move to the next line in program files
a. Contoh pertama
Program m4c1;
Uses wincrt;
Var
x,i,j : integer;
Begin
j := 0;
Readln (x);
For i := 1 to x do
Writeln (i:4, i*i:4);
Readln;
For i := 1 to x do
j := j + i;
Writeln (j);
End.
b. Contoh ke-dua
Program m4c2;
Uses wincrt;
Var
i, x : integer;
j : real;
p : string;
Begin
J := 0;
Readln (x);
For i := 1 to x do
Begin
If i <> x then p := ' + '
else p := ' = ';
j := j + (1/i);
Write ('1/',i,p);
End;
Write (j:4:2);
End.
c. Contoh ke-tiga
Program m4c3;
Uses wincrt;
Var
d,j,i : integer;
e : real;
Begin
j := 0;
i := 0;
Readln (d);
While d >= 0 do
Begin
j := j + d;
i := i +1;
Readln (d);
End;
e := j/i;
Writeln ('Jumlah: ',j);
Writeln ('Rerata: ',e:4:1);
End.
d. Contoh ke-empat
Program m4c4;
Uses wincrt;
Var
c, f, r : real;
Begin
writeln(' C F R');
writeln(' ---------------------');
c := 0;
repeat
f := (9/5) * c + 32;
r := (4/5) * c;
writeln (c:8:1, f:8:1, r:8:1);
c := c + 0.5;
until c > 10;
writeln(' ---------------------');
End.
Tidak ada komentar:
Posting Komentar