Hi, its smee again!
Im having a few probs with using If, with my boundary checking which needs to see if 4 conditions are within range:
In Basic:
if (x>0) and (x<wwidth-1) and (y>0) and (y<height-1) then
perform_action()
end if
My attempt in CPP:
// is this correct?. if it is then something else is a foot, should be about 12 inches ;)
if (x>0)
if (x<wwidth-1)
if (y>0)
if (y<height-1)
perform_action();
Thanks alot,
Mc Clyde.
Jim already kinda answer this with example code, you also should be able to get information from the help files installed with VC either local or via MSDN. You really should also check out some tutorials this link is to an example of what if statements should look like in C++, and ofc you can always nest your if statements if you want all the clauses to be met before doing something.
http://www.cplusplus.com/doc/tutorial/control/ (http://www.cplusplus.com/doc/tutorial/control/)
Even if you cant find the and operator you should have been able to make code like this
if (x>0)
{
if (x<wwidth-1)
{
if (y>0)
{
if (y<height-1)
{
perform_action();
}
}
}
}
if (x>0) and (x<wwidth-1) and (y>0) and (y<height-1) then
perform_action()
end if
is equivalent to
if (x>0 && x<wwidth-1 && y>0 && y<height-1)
{
perform_action();
}
Doing it in 4 separate ifs is exactly the same.
Jim
Jim's example is my preferred method, though I've found it's a bit smaller for some reason using clyde's proposed method.
I think the code comes out smaller if you have got the nested if's because you only have in this case 4 checks, were as when you do the line with && it generates code that has to check each experession inside the braces and the check for the final(or part) result of the &&'s. It probably depends on the compiler and number of opertations your &&'ing as to what the compiler actually generates, well thats my theory on it, havent really done stuff on compilers and code generation since i was in college in the 90's and things have moved on abit since then :D
In C, it will 'early out'. That is, if you have
if (a && b)
Then if 'a' is false it won't evaluate 'b' at all.
That would be the same as doing
if (a)
{ if (b)
{...
}
}
I can't see why it would be smaller to do it in bits - perhaps giving the compiler some idea of the scoping of any variables inside the sub-ifs means the optimisation pans out slightly differently. Semantically ("in meaning") both methods are exactly equivalent. Would be interesting to see this in action and in which compilers it happens on.
Jim
Compilers sometimes make weird decisions - right is one byte shorter:
bool test1(int a, int b, int c) { if (a>0) if (b>0) if (c>0) return true; return false; }
00401000 xor eax,eax 00401002 cmp dword ptr [esp+4],eax 00401006 jle test1+16h (401016h) 00401008 cmp dword ptr [esp+8],eax 0040100C jle test1+16h (401016h) 0040100E cmp dword ptr [esp+0Ch],eax 00401012 jle test1+16h (401016h) 00401014 mov al,1 00401016 ret
|
bool test2(int a, int b, int c) { return (a>0 && b>0 && c>0); }
0040101A xor eax,eax 0040101C cmp dword ptr [esp+4],eax 00401020 jle test2+15h (40102Fh) 00401022 cmp dword ptr [esp+8],eax 00401026 jle test2+15h (40102Fh) 00401028 cmp dword ptr [esp+0Ch],eax 0040102C jle test2+15h (40102Fh) 0040102E inc eax 0040102F ret
|
(produced by msvc 2008 express)
Nice example, thanks very much!
Jim
Looking at that output code brings back some memories, having homework were I had to write stuff like that for the 6502 in a BBC micro, those were fun times but was never my strong point. Jump if less or equal :D
Thanks for the example hellfire :) I remember GCC being the opposite, probably not now with the newer versions etc :)