?
Solved

If statement is wrong!!

Posted on 2003-03-09
14
Medium Priority
?
221 Views
Last Modified: 2010-04-04
this is my code.


  for i := 0 to 15 do
  begin
    for j := 0 to 2 do
    begin
      if ((Computer[i].Rows[j].Pos.x = iOld) and (Computer[i].Rows[j].Pos.y = jOld)) then
      begin
        // main part
      end;
    end;
  end;


For some reason delphi *sometimes* executes the main part even though the if statement is false! I'm using similar code in other places and there it works, what am I doing wrong here??

Any help is really appreciated...


Fran
0
Comment
Question by:WizardsHat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +5
14 Comments
 
LVL 7

Expert Comment

by:billious
ID: 8097069
If you'd show us what the code is in //main part, then we just might be able to help you - don't keep it under your hat, regardless of what you're doing in it!

...Bill
0
 

Expert Comment

by:jumail11
ID: 8097175
The codes you given us, doesn't seems any logical error. I expect the problem was caused by outside codes. You'd show us the rest of the code.

-jumail-
0
 
LVL 2

Expert Comment

by:steve_hsk
ID: 8097293
Hi Fran ...

These guys are right ... need to see more of your code.

The only suggestion I have is to break the statement down, to enable bug tracking, breakpoints, and variable watches to mean more :-

if (Computer[i].Rows[j].Pos.x = iOld) then
begin
     if (Computer[i].Rows[j].Pos.y = jOld) then
     begin
       // main part
     end;
end;

This isn't a permanant solution, will make no difference, but may help in debugging.

Steve
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:sfock
ID: 8097460
have you changed the if line? probably delphi has not recomplied it if it is not part of the dpr file or par of a package ....

0
 

Expert Comment

by:JamesLondon
ID: 8098027
Hi there,

Sometimes strange things can happen if for example your are doing something wrong elsewhere, i.e. outside that code you should check all accessing of objects, i.e. you are not freeing something then accessing it later. In some cases Access Violations can be suppressed thus causing the strange behaviour.

Also check that you are not exceeding the bounds of your arrays by making sure the "Range Checking" option is turned on.

Best Regards,
- James
0
 

Expert Comment

by:JamesLondon
ID: 8098033
Hi again,

The reason for my last statement is experience!

- I once had a problem with the MOD operator and I was convinced that it was Delphi's fault though the perculier behaviour was caused by something completely different.

James
0
 

Author Comment

by:WizardsHat
ID: 8101107
Okay this is the full code...
Its for a game called "Muehle" in German but I don't know the English name. I test if the position I want to place the stone is near the position it currently has, because the game only allows to move the stone to adjacent fields. The rows are all three places long and each place exists in two rows. Well I guess you can't really understand what I'm talking about unless you know the game, but maybe you find some technical error...

Steve I tried this as well, usually the first if is wrong (so it works out like 3 = 5 for example) but delphi seems to think its correct.

James I have RangeChecking enabled, and I'll look now for possible access violations.


function TFoMuehle.IsInVicinity(iOld, jOld, iNew, jNew : integer): boolean;
var i, j, k, m : integer;
begin
  for i := 0 to 15 do
  begin
    for j := 0 to 2 do
    begin
     if ((Computer[i].Rows[j].Pos.x = iOld) and (Computer[i].Rows[j].Pos.y = jOld)) then
      begin
        k := j;
        Inc(k);        // if it is the next in the row
        if k < 3 then
        begin
          if ((Computer[i].Rows[k].Pos.x = iNew) and (Computer[i].Rows[k].Pos.Y = jNew)) then
          begin
            Result := true;
            exit;
          end;
        end;
        Dec(k);       // if it is the previous in the row
        Dec(k);
        if k > -1 then
        begin
          if ((Computer[i].Rows[k].Pos.x = iNew) and (Computer[i].Rows[k].Pos.Y = jNew)) then
          begin
            Result := true;
            exit;
          end;
        end;
       //maybe its in the other row
        m := Computer[i].Rows[k].ORowField.x;
        k := Computer[i].Rows[k].ORowField.y;
        Inc(k);        // if it is the next in the row

        if k < 3 then
        begin
          if ((Computer[m].Rows[k].Pos.x = iNew) and (Computer[m].Rows[k].Pos.Y = jNew)) then
          begin
            Result := true;
            exit;
          end;
        end;
        Dec(k);       // if it is the previous in the row
        Dec(k);
        if k > -1 then
        begin
          if ((Computer[m].Rows[k].Pos.x = iNew) and (Computer[m].Rows[k].Pos.Y = jNew)) then
          begin
            Result := true;
            exit;
          end;
        end;

      end;
    end;
  end;
  Result := false;
end;
0
 

Author Comment

by:WizardsHat
ID: 8101110
The game is called "Nine men's morris" in English.
0
 
LVL 17

Expert Comment

by:geobul
ID: 8101122
Hi,

If your i and j values are OK then the problem is caused by:

1. iOld and/or jOld values:
- They must have correct initial values before the loops.
- Are you changing their values inside the loop? If yes, how?

2. Computer[i].Rows[j].Pos.x and/or Computer[i].Rows[j].Pos.y values:
- check their values inside the loop

Add something like:

ShowMessage('i='+IntToStr(i)+' j='+IntToStr(j) + #13#10 +
'PosX='+IntToStr(Computer[i].Rows[j].Pos.x) + ' iOld='+ IntToStr(iOld) + #13+#10 +
'PosY='+IntToStr(Computer[i].Rows[j].Pos.y) + ' jOld='+ IntToStr(jOld));

inside the second loop and see what' happening.

Regards, Geo
0
 

Author Comment

by:WizardsHat
ID: 8101200
Okay now I only get into the loop when I should,  but then I get a rangecheck exception here

m := Computer[i].Rows[k].ORowField.x;



because suddenly k is some ridiculously large number, but i can't see why it should be?
0
 

Author Comment

by:WizardsHat
ID: 8101217
Weeeeeeell... I now removed the part in the if- statement starting from

m := Computer[i].Rows[k].ORowField.x;


to the end of it since I realized that it isn't necessary ( I go through all rows anyway), and now everything seems to work fine.

But I'll still be grateful to anyone who could explain why!!
0
 
LVL 17

Accepted Solution

by:
geobul earned 450 total points
ID: 8102086
Hi,

Let j = 0
then k = j = 0
Inc(k) : k=1
Dec(k) : k=0
Dec(k) : k=-1
m := Computer[i].Rows[k].ORowField.x;
// error Rows[-1] out of bounds !!!

Regards, Geo
0
 

Expert Comment

by:CleanupPing
ID: 9316879
WizardsHat:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 17

Expert Comment

by:geobul
ID: 9317683
Comment = I do care :-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month8 days, 5 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question