0

Dear Sir,

Could you mind tell me what about the difference between While..loop, for..loop and repeat.. until??

Can you give me some example??

Cheers,

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by FlamingClaw
1

Look at these,I hope these helps to you :D

Program Loops;

Var i:Integer;{counter variable}

Begin {main}
{
Loops are used when you want to repeat code a lot of times.
For example, if you wanted to print "Hello" on the screen
10 times you would need 10 Writeln commands.
You could do the same thing by putting 1 Writeln command
inside a loop which repeats itself 10 times.
There are 3 types of loops which are the for loop,
while loop and repeat until loop.
}

{-= The FOR loop =-}
{
The for loop uses a loop counter variable,
which it adds 1 to each time, to loop from
a first number to a last number.}
   WriteLn('-= The FOR loop =-');
   Write('Results: ');
   For i:=1 To 10 Do Write('*');
{the above line will write 10 star to the screen.
i's value is incrementing by 1 automatically...}
   WriteLn;
   WriteLn;
   WriteLn('-= The FOR loop DownTo =-');
   Write('Results: ');
   For i:=10 DownTo 1 Do Write(i,' ');
{the above line will write i's value to the screen.
i's value is decrementing by 1 automatically...}
   WriteLn;
   WriteLn;

{-= The WHILE loop =-}
{
The while loop repeats while a condition is true.
The condition is tested at the top of the loop
and not at any time while the loop is running
as the name suggests. A while loop does not need
a loop variable but if you want to use one then
you must initialize its value before entering the loop.
} WriteLn('-= The WHILE loop =-');
  Write('Results: ');
  i:=1;
  While (i<=10) Do Begin{while (the condition is true) do the commands}
     Write('o ');
     i:=i+3;  {<-this is important,we need to increment the value of 'i'}
  End;
  WriteLn;
  WriteLn;


{-= The REPEAT UNTIL loop =-}
{
The repeat until loop is like the while loop except
that it tests the condition at the bottom of the loop.
It also doesn't have to have a 'begin' and an 'end' if
it has more than one command inside it.
}  WriteLn('-= The REPEAT UNTIL loop =-');
   Write('Results: ');
   i:=50;
   Repeat      {so repeat while...}
      Write(i:3);
      i:=i-10;
   Until i=0;  {analize here,the condition becomes true}
   WriteLn;
   WriteLn;
{
If you want to use more than one condition
for either the 'while' or 'repeat' loops
then you have to put the conditions between brackets
}
   WriteLn('-= The REPEAT UNTIL loop with 2 Conditions=-');
   Write('Results: ');
   i:=50;
   Repeat      {so repeat while...}
      Write(i:3);
      i:=i-10;
   Until (i=20) Or (i=0);  {Or first or second condition becomes true}
   WriteLn;
   WriteLn;

{-= BREAK and CONTINUE =-}
{The Break command will exit a loop at any time.
The following program will not print anything
because it exits the loop before it gets there.}
   WriteLn('-= The REPEAT UNTIL loop with Break =-');
   Write('Results: ');
   i:=1;
   Repeat
      i:=i+1;
      Break;
      Write('Hello from Pascal.');
   Until i=3;
   WriteLn;
   WriteLn;

{
The Continue command will jump back to the top of a loop.
This example will also not print anything
but unlike the Break example, it will count all the way to 3.
}
   WriteLn('-= The REPEAT UNTIL loop with Continue =-');
   Write('Results: ');
   i:=1;
   Repeat
      i:=i+1;
      Continue;
      Write('Hello from Pascal.');
   Until i=3;
   ReadLn;
End.{end of main}

{
when you write a program,you can jump into the program
Run your  console apps with F7 (Trace into).Then you will see
into the loop's rounds one by one.
}

{-=Created By FlamingClaw 2009.05.07=-}
Votes + Comments
He express it very clear to me.
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.