Computers are great at crunching piles of data, and piles of data usually takes the form of sequences, streams, tables and so on. Such data sets usually need to be iterated over. At the very least, some statements may need to be executed more than once. The following are the primary loops of every computer language.
While-Do loop
For loop
Repeat-Until Loop
While-Do loop
Description
While loop is utilized to iterate the code several times, in this scenario it will be used. Following is the conceptual figure which explains the while loop functionality.
The following is an example.
[c]program Whiledo;
var
a: integer;
begin
a := 5;
while a < 15 do
begin
writeln('value of a: ', a);
a := a + 1;
end;
end.
[/c]
In the above code a+1 means the value of a will be incremented till to 14 that should be less than 15.
Output: Now compile the code result will be as follows.
[c]value of a: 5
value of a: 6
value of a: 7
value of a: 8
value of a: 9
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14[/c]
For loop
Description
A for-do circle is a reiteration structure that permits the develope to productively compose a loop that necessities to execute a particular times. The following is the syntax for the for loop.
[c]for < variable-name > := < initial_value > to [down to] < final_value > do
S;[/c]
The following is the for loop structure.
The following is an example.
[c]program forloop;
var
x: integer;
begin
for x := 1 to 10 do
begin
writeln('value of x: ', x);
end;
end.[/c]
Output: Now compile the code result will be as follows.
[c]value of x: 1
value of x: 2
value of x: 3
value of x: 4
value of x: 5
value of x: 6
value of x: 7
value of x: 8
value of x: 9
value of x: 10[/c]
Repeat-Until Loop
Description
It test the condition at the highest point of the loop, this loop in Pascal checks the condition at the base of the circle. The following is the syntax.
[c]repeat
S1;
S2;
...
...
Sn;
until condition;[/c]
The following is an example.
[c]program repeatuntil;
var
x: integer;
begin
x := 10;
(* repeat until loop execution *)
repeat
writeln('value of x: ', x);
x := x + 1
until x = 20;
end.[/c]
Output: Now compile the code result will be as follows.
[c]value of x: 10
value of x: 11
value of x: 12
value of x: 13
value of x: 14
value of x: 15
value of x: 16
value of x: 17
value of x: 18
value of x: 19[/c]
Summary
Key Points
All the control Statements are logical statements
To repeat the flow of code for loop will be used.
While loop executes after the condition becomes true.