Question

Was looking on Google if there was a major difference betweeen while and repeat..until loops, I didn't find anything useful or what I was looking for.

I noticed that the while loop makes my programs unresponsive, and the repeat works better than while. Is there any specific reason for this, or should one use one another in different scenarios?

Here's my code in a while loop:

var
  m,val : Integer;
  i,k   : Real;
begin
  val := StrToInt(edtShot.Text);
  i := sqr(k);
  k := sqrt(i+val);
  m := 0;

    while i <= 0 and k <= 10 do
      begin
        inc(m);
      end;
end;

and here it is with a repeat loop:

var
  m,val : Integer;
  i,k   : Real;
begin
  val := StrToInt(edtShot.Text);
  i := sqr(k);
  k := sqrt(i+val);
  m := 0;
    repeat
      inc(m);
    until i > 0 and k > 10;
end;

BTW I know the code above doesn't really make much sense, just an example.

Was it helpful?

Solution

A 'repeat' loop is guaranteed to execute at least once as the terminating condition is checked only after the loop has executed once. A 'while' loop may not even execute once as the condition is checked before the loop is executed.

Other than that, they should take exactly the same amount of time to execute.

OTHER TIPS

This is complete nonsense code, even for the purposes of testing. Both examples seem to neglect to initialize the value k before using it in an assignment statement. This makes the values of i and k essentially random. Since the loops do not modify the values in the loop condition this means one of two things will happen randomly; upon entering the loop, either the loop condition will be satisfied or it will not. This means the loop will run either once for repeat (or not at all for while) or the loop will never terminate and your application will hang. Consider the cases :

var
  m,val : Integer;
  i,k   : Real;
begin
  val := StrToInt(edtShot.Text);  // I don't know what this value is
  i := sqr(k);       // k is random, uninitialized - could be anything
  k := sqrt(i+val);  // maybe 3.23452E-14...maybe 1.9234e12 
  m := 0;            // Since i is a square, it WILL be positive!
                     // same for k   
    while (i <= 0) and (k <= 10) do  //need to enclose these to compile!
      begin                          // i will never be <= 0 unless k was 
        inc(m);                      // 0 to start - extremely unlikely
      end;                          
end;

The important point is that k (and in fact, all the local variables) has whatever random garbage value was in its memory location when this method was entered. If you do not assign it a value before using it, you are using a random, probably quite large or extremely small value. This makes your program unpredictable. The compiler also should warn you about this! Don't ignore compiler warnings.

Consider now :

repeat
  inc(m);
until (i > 0) and (k > 10);  //need to enclose these to compile!
                             // i almost certain to be >0, likewise k very
                             // likely to be > 10

By the same logic as above, both k and i are likely to have values that are very large or very small . Depending on the starting value of k your loops will either repeat forever or not run at all. Since the loop conditions are fundamentally different, one or the other may be more likely to fall under either category. This is why you are observing this behaviour - it has nothing to do with while or repeat loops, it has to do with the fact that you are testing nonsense code with different conditions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top