Jump to content
  • Advertisement
Sign in to follow this  
phil67rpg

if statement problem

This topic is 2484 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

for some reason this if statement never executes even if all arrays are true.let me know if you need more code
[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]if[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] (((board[0,0]) == [/font][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]true[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]) && ((board[0,1]) == [/font][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]true[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]) && ((board[0,2]) == [/font][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]true[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]))[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]{[/font][/font]
[font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Pen[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] pn = [/font][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]new[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/font][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Pen[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]([/font][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Color[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"].Blue, 3);[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]g.DrawLine(pn, 0, 50, 200, 50);[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]}[/font][/font]

Share this post


Link to post
Share on other sites
Advertisement
[sup]How is the array declared? Is it passed around to multiple classes? I once had a problem that was caused by actually having 2 versions of the array kicking around. Also how do you knwo that the statements are actually true, did you look at the array with a debugger?[/sup]

[sup]Just a side note the brackets around your array access are just taking up space.[/sup]

Share this post


Link to post
Share on other sites
1. It's easier to just use the value, rather than comparing to true (do you say "is it true that we have stopped?" or "have we stopped?" - albeit the names aren't quite so nice in this case)
2. Place a breakpoint on the if, run with debugging enabled and inspect the values of board[0,0] etc.

Share this post


Link to post
Share on other sites
here is some more code for clarification of my problem.
[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]int[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] pX = e.X;[/font][/font]
[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]int[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] pY = e.Y;[/font][/font]
[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]int[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"][] board = [/font][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]new[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/font][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]int[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"][9];[/font][/font]

[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]if[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] (pX >= 10 && pX <= 20 && pY >= 30 && pY <= 40)[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]{[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]board[0] = 1;[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]}[/font][/font]
[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]if[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] (pX >= 80 && pX <= 90 && pY >= 30 && pY <= 40)[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]{[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]board[1] = 1;[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]}[/font][/font]
[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]if[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] (pX >= 160 && pX <= 170 && pY >= 30 && pY <= 40)[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]{[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]board[2] = 1;[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]}[/font][/font]
[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]if[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] (board[0]==1 & board[1]==1 & board[2]==1)[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]{[/font][/font]
[font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Pen[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] pn = [/font][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]new[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/font][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Pen[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]([/font][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Color[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"].Blue, 3);[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]g.DrawLine(pn, 0, 50, 200, 50);[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]}[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]this one has only a 1d array and it still does not work[/font][/font]

Share this post


Link to post
Share on other sites
That's because your 2nd statement can never be true if the fisrt one is;

[font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]if[/font][/font][/font][font=Consolas][size=2][font=Consolas][size=2] (pX >= 10 && pX <= 20 [/font][/font]...

[font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]if[/font][/font][/font][font=Consolas][size=2][font=Consolas][size=2] (pX >= 80 && pX <= 90[/font][/font]


From the sounds of it I think you might be looking for an OR statement not an AND statement, if that is the case than change it to this;
[font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]if[/font][/font][/font][font=Consolas][size=2][font=Consolas][size=2] (board[0]==1 || board[1]==1 || board[2]==1)[/font][/font]


Also using the & operator forces it to equate the whole statement its a binary operation, you want to use && 99% of the time instead of &.

Share this post


Link to post
Share on other sites
It depends what you are trying to achieve. A switch structure will not achieve anything that your if structure isn't already doing. The problem is this code;
[font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]if[/font][/font][/font][font=Consolas][size=2][font=Consolas][size=2] (board[0]==1 & board[1]==1 & board[2]==1)[/font][/font]
[font=Consolas][size=2][font=Consolas][size=2]{[/font][/font]
[font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af]Pen[/font][/font][/font][font=Consolas][size=2][font=Consolas][size=2] pn = [/font][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]new[/font][/font][/font][font=Consolas][size=2][font=Consolas][size=2] [/font][/font][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af]Pen[/font][/font][/font][font=Consolas][size=2][font=Consolas][size=2]([/font][/font][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af]Color[/font][/font][/font][font=Consolas][size=2][font=Consolas][size=2].Blue, 3);[/font][/font]
[font=Consolas][size=2][font=Consolas][size=2]g.DrawLine(pn, 0, 50, 200, 50);[/font][/font]
[font=Consolas][size=2][font=Consolas][size=2]}[/font][/font]
If you change the &'s to || (OR's) than you will at least be drawing that static position you have defined (0,50,200,50). Then you can go from there.

Share this post


Link to post
Share on other sites

If you change the &'s to || (OR's) than you will at least be drawing that static position you have defined (0,50,200,50). Then you can go from there.

I have tried the || operator.but it draws the line before all values in the array are filled.

Share this post


Link to post
Share on other sites
Of course it does, we already stated that all of your if statements cannot be true at the same time. So again, what are you trying to achieve?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!