-2

I am trying to rewrite a while do loop as a for do loop in Pascal, please is there a way to rewrite the below code into a for loop statement... And determine which is better and why ?

Thanks in advance..

Here is the code

program Quadractics;
uses crt;

const n=10;

VAR
  a,b,c,d,e,x1,x2:real;
  k:Integer;

BEGIN
  k:= 0;
  
  while k<n DO 
  
  begin
  
  writeln('Input the values for variables a,b,c');
  readln(a,b,c);
  e:=2*a;
  d:=sqr(b)-(4.0*a*c);
  
  if d>=0 then
    begin
  
  x1:=(-b+sqrt(d)) /e;
  
  x2:=(-b-sqrt(d)) /e;
  
  writeln('The resulting roots of the above equation are x1= ', x1 , ' and x2= ', x2);
  
    end
  
  else
  
  begin
  
  write('complex root values are  ');
  write('x1 = ', '(',-b, '+√',d,')','/',e ,', ');
  
  write('and',';  ');
  
  write('x2 = ', '(',-b, '-√',d,')','/',e ,', ');

  end; 
  
  k:=k+1;
  
  end;
  
END.
Browyn Louis
  • 218
  • 3
  • 14
  • 2
    *I am trying* - Where? I don't see any effort by you to rewrite the code at all. SO is not a homework completion service. – Ken White Feb 22 '21 at 04:02
  • Does this answer your question? [Rewrite Pascal for do loop using while do loop](https://stackoverflow.com/questions/66291271/rewrite-pascal-for-do-loop-using-while-do-loop) – asd-tm Mar 11 '21 at 10:06

1 Answers1

0

As I don't want to spoil your homework, I give you the following clue:

The while loop in pseudocode

loopcounter = startvalue
while loopcounter < endvalue do // could also be loopcounter <= endvalue
begin
  code to perform on every loop
  loopcounter = loopcounter + 1
end

The for loop in pseudocode

for loopcounter = startvalue to endvalue // could also be to endvalue-1
begin
  code to perform on every loopcounter
end

Think about and compare the difference of how the endvalue is expressed.

And finally a tip to help you: Always follow the indentation rules, it's much easier to read the structure and functionality of the code.

Tom Brunberg
  • 20,312
  • 8
  • 37
  • 54