Why is my if statement not starting?

I am switching from C # to Delphi 2009, I really like it.

I wrote a binary search procedure that works great. I added a simple if-else statement at the end of my program, and it just doesn't work! I see nothing wrong with that, and I am ashamed to say that I am stuck. Please, help!

procedure BinSearch;
var
  min,max,mid, x: integer;
  A : array[0..4] of integer;
  rslt : integer;

begin

  writeln('binary search');
  A[0] := 34; A[1] := 65; A[2] := 98; A[3] := 123; A[4] := 176;
  listarray(a);
  x := 62;
  min := 0;
  max := 4;

  repeat
    begin
    mid := (min + max) div 2;
    if x > A[mid] then
      min := mid + 1
    else
      max := mid - 1;
    end;
  until (A[mid] = x) or (min > max);

  writeln(mid);
  writeln(a[mid]);

  if A[mid] = x then
    rslt := mid
  else
    rslt := not mid;

  if 54 = 65 then
    rslt := mid
  else
    rslt := not mid;

end;

This if A[mid] = x thenthat does not work. when debugging neither true nor false branches the fire, the debugger just jumps right above them. Also a tag if 54 = 65 then, which is just a test, does the same.

If inside the repeat loop it works fine.

- proc, proc, , - proc, ;, - , . , !

+5
4

Delphi , . , : ", " rslt ", ". , .

Writeln(rslt); , , if.

+14

, , . , . Delphi 7 "\" "".

+4

"" "" . "" . , , .

0

"rslt" . Delphi .

, . :

procedure BinSearch(var rslt: integer); 

, :

function BinSearch: integer;

:

Result := rslt;

, , , rslt.

But you will find that you have a problem with your expression:

rslt := not mid;

because the middle is an integer. I’m not sure if you want to come back here, but I know that you don’t want the “not” operator to be applied to the “middle”.


Take a look at this code I received from wikibooks . This can help you figure it out.

(* Returns index of requested value in an integer array that has been sorted
in ascending order -- otherwise returns -1 if requested value does not exist. *)

function BinarySearch(const DataSortedAscending: array of Integer;
 const ElementValueWanted: Integer): Integer;
var
    MinIndex, MaxIndex: Integer;
    { When optimizing remove these variables: }
    MedianIndex, MedianValue: Integer;
begin
    MinIndex := Low(DataSortedAscending);
    MaxIndex := High(DataSortedAscending);
    while MinIndex <= MaxIndex do begin
        MedianIndex := (MinIndex + MaxIndex) div 2; (* If you're going to change
         the data type here e.g. Integer to SmallInt consider the possibility of
         an overflow. All it needs to go bad is MinIndex=(High(MinIndex) div 2),
         MaxIndex = Succ(MinIndex). *)
        MedianValue := DataSortedAscending[MedianIndex];
        if ElementValueWanted < MedianValue then
            MaxIndex := Pred(MedianIndex)
        else if ElementValueWanted = MedianValue then begin
            Result := MedianIndex;
            Exit; (* Successful exit. *)
        end else
            MinIndex := Succ(MedianIndex);
    end;
    Result := -1; (* We couldn't find it. *)
end;
0
source

All Articles