Recent

Author Topic: RC2 bug? No [Solved]  (Read 2356 times)

bobc50

  • New Member
  • *
  • Posts: 30
RC2 bug? No [Solved]
« on: November 09, 2023, 03:46:03 pm »
I wanted to make programs more maintainable. I created coded with same structure as below. My intent is to execute one of two while loops based on a condition. The code, other than the while statement is identical for both.

The code below compiles and, IMHO, is a clear representation of what I want to do. If I set a breakpoint at the start and single step the program goes off the deep end on the
"while I = 0" do statement and appears to simply hang.
  • Is that a Lazarus bug and, if so, how do I report it?
  • I worked around this problem by repeating the code starting with the begin line with the comment to the end statement in a straightforward way. It is possible that I can make the code between that begin end another function. What is the suggested approach?
Code: Pascal  [Select][+][-]
  1.    
  2. program project1;
  3. var
  4.   i: integer;
  5.   b: boolean;
  6. begin
  7.   i := 0;
  8.   b := true;
  9.   if b = true then
  10.     while i = 0 do
  11.   else
  12.     while i = 1 do
  13.     begin // this code might be quite long
  14.       i := (i + 1) mod 2
  15.     end;
  16. end.
  17.  
   
« Last Edit: November 09, 2023, 09:07:49 pm by bobc50 »

wp

  • Hero Member
  • *****
  • Posts: 11760
Re: RC2 bug?
« Reply #1 on: November 09, 2023, 03:54:58 pm »
the "while I = 0" do statement and appears to simply hang.
You do not provide a condition for terminating the loop.

[EDIT]
Sorry - this is expressed badly. The correct words should be: You do not change i inside the while loop so that the initial condition (i=0) always remains true.
« Last Edit: November 09, 2023, 04:19:23 pm by wp »

bobc50

  • New Member
  • *
  • Posts: 30
Re: RC2 bug?
« Reply #2 on: November 09, 2023, 04:08:42 pm »
the "while I = 0" do statement and appears to simply hang.
You do not provide a condition for terminating the loop.
Thanks, but I don't understand. I'm still learning Pascal, but isn't i = 0 a condition? The following code compiles and executes with no issue. Can I rewrite my original code without making two copies of the stuff I want to execute when the initial conditon is meet?

program project1;
var
  i: integer;
  b: boolean;
begin
  i := 0;
  b := true;
  if b = true then
    while i = 0 do
    begin // some stuff
      i := (i + 1) mod 2;
      writeln(i);
    end
  else
    while i = 1 do
    begin // a copy of the stuff
      i := (i + 1) mod 2;
      writeln(i);
    end;
end.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9663
  • Debugger - SynEdit - and more
    • wiki
Re: RC2 bug?
« Reply #3 on: November 09, 2023, 04:12:06 pm »
Well the "while i = 0" will run forever.

Your code is the same as the following (with extra begin end for more clarity)

Code: Pascal  [Select][+][-]
  1. program project1;
  2. var
  3.   i: integer;
  4.   b: boolean;
  5. begin
  6.   i := 0;
  7.   b := true;
  8.   if b = true then
  9.   BEGIN
  10.     while i = 0 do
  11.        {empty statement};
  12.   END
  13.   else
  14.   BEGIN
  15.     while i = 1 do
  16.     begin // this code might be quite long
  17.       i := (i + 1) mod 2
  18.     end;
  19.   END;
  20. end.
  21.    

Since "b=true" the first loop, with the "empty statement" is executed. And since "i=0" and i never changes, that loop runs forever.

If you are in the debugger, you can use pause to interrupt => however, (OS dependent), pause may create a temporary thread, in which case you must open the thread window and switch to your main thread.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9663
  • Debugger - SynEdit - and more
    • wiki
Re: RC2 bug?
« Reply #4 on: November 09, 2023, 04:16:30 pm »
Please use "code tags"

Ah...

No, you can't "detach" the "while" from the code that runs in the while. (you can't have 2 while conditions for a single loop-code).

But you can
Code: Pascal  [Select][+][-]
  1. if b then
  2.   ConditionVal := 1
  3. else
  4.   ConditionVal := 0;
  5. while c = ConditionVal do begin
  6. // ....
  7. end;


Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9663
  • Debugger - SynEdit - and more
    • wiki
Re: RC2 bug?
« Reply #5 on: November 09, 2023, 04:22:27 pm »
Mind, in Pascal the semicolon ";" is a statement separator.

So for the last statement before an "end" you do not need a semicolon (but for various reasons, better put it there).

"ELSE" also terminates the ("else sub-") statement (and in front of else you are not allowed a semicolon).

So that is why your first "while" loop ends at the "else". And because you have nothing between "do else", the while loop executes an "empty statement".

bobc50

  • New Member
  • *
  • Posts: 30
Re: RC2 bug?
« Reply #6 on: November 09, 2023, 04:45:18 pm »
Thanks to all for your help. I understand where I went wrong. I will use the approach suggested by Martin_fr.
« Last Edit: November 09, 2023, 09:08:11 pm by bobc50 »

 

TinyPortal © 2005-2018