• Advertisement

Archived

This topic is now archived and is closed to further replies.

advanced use of the IF statement

This topic is 5120 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

Hey GDers! I have an extremely tedious task for you. I have an IF statement with a LOT of parentheses (29x2 pairs!). The problem is that my compiler (Borland C++ Builder 3) gives me errors ("Expression syntax", "If statement missing )" and "Compound statement missing }" ) Sound like a n00b problem? Perhaps it is and people use IF like this all the time, but is it possible to go overboard like I did here?
if( (
            ( (x1 <= x ) && (x2 => x) ) ||
            ( (x2 <= x) && (x1 => x) )
            ) && (
            ( (line->x1 <= x) && (line->x2 => x) ) ||
            ( (line->x2 <= x) && (line->x1 => x) )
            ) && (
            ( (y1 <= y) && (y2 => y) ) ||
            ( (y1 >= y) && (y2 <= y) )
            ) && (
            ( (line->y1 <= y) && (line->y2 >= y) ) ||
            ( (line->y1 >= y) && (line->y2 <= y) ) )
            ) {
                return true;
            }
This block of code checks that the bounds of x are within x1 and y1 and within line->x1 and line->x2, and that the bounds of y are within y1 and y2 and within line->y1 and line->y2. a) Is this a legal use of IF, and if so, why am I getting errors with it? b) Is there a simpler way of doing this? Cheers! Stay Clausal, Red Sodium

Share this post


Link to post
Share on other sites
Advertisement
I checked with UltraEdit, and all your braces match. Maybe the problem is the places where you''re using => instead of >=. Sometimes the error message that''s given isn''t always an indication of what the real problem is.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Take off the last ) and it should work

Share this post


Link to post
Share on other sites
I produced this:


if (
(
(
(
x1 <= x
)
&&
(
x2 => x
)
)
||
(
(
x2 <= x
)
&&
(
x1 => x
)
)
)
&&
(
(
(
line->x1 <= x
)
&&
(
line->x2 => x
)
)
||
(
(
line->x2 <= x
)
&&
(
line->x1 => x
)
)
)
&&
(
(
(
y1 <= y
)
&&
(
y2 => y
)
)
||
(
(
y1 >= y
)
&&
(
y2 <= y
)
)
)
&&
(
(
(
line->y1 <= y
)
&&
(
line->y2 >= y
)
)
)
||
(
(
line->y1 >= y
)
&&
(
line->y2 <= y
)
)
)
) //remove this one i think

{ return true;
};


edit: Damn what a waste of time, and I was wrong.

[edited by - honayboyz on April 15, 2004 7:04:15 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by red_sodium

b) Is there a simpler way of doing this?




If you could guarantee that x1 is always <= x2 (and same for the y's), then half of those cases would dissappear. You could always swap x1 and x2 to make it true.

You can break up the construction into some booleans to make it far more readable.

Here's my version of that thing:


bool within_x = ((x1 <= x ) && (x2 >= x)) || ( (x2 <= x) && (x1 >= x));
bool within_y = ((y1 <= y ) && (y2 >= y)) || ( (y2 <= y) && (y1 >= y));
bool within_line_x = ((line->x1 <= x ) && (line->x2 >= x)) || ( (line->x2 <= x) && (line->x1 >= x));
bool within_line_y = ((line->y1 <= y ) && (line->y2 >= y)) || ( (line->y2 <= y) && (line->y1 >= y));
return within_x && within_y && within_line_x && within_line_y;


[edited by - pinacolada on April 15, 2004 8:34:33 PM]

Share this post


Link to post
Share on other sites

  • Advertisement