Author Topic: [C++] The IF statement: Using And.  (Read 5101 times)

0 Members and 1 Guest are viewing this topic.

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
[C++] The IF statement: Using And.
« on: August 19, 2009 »
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:
Code: [Select]
if (x>0) and (x<wwidth-1) and (y>0) and (y<height-1) then
perform_action()
end if

My attempt in CPP:
Code: [Select]
// 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.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline TinDragon

  • Pentium
  • *****
  • Posts: 644
  • Karma: 24
    • View Profile
    • J2K's blog
Re: [C++] The IF statement: Using And.
« Reply #1 on: August 19, 2009 »
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/

Even if you cant find the and operator you should have been able to make code like this
Code: [Select]
if (x>0)
{
     if (x<wwidth-1)
     {
             if (y>0)
             {
                   if (y<height-1)
                   {
                             perform_action();
                    }
              }
      }
}
« Last Edit: August 19, 2009 by TinDragon »

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: [C++] The IF statement: Using And.
« Reply #2 on: August 19, 2009 »
Code: [Select]
if (x>0) and (x<wwidth-1) and (y>0) and (y<height-1) then
  perform_action()
end if
is equivalent to
Code: [Select]
if (x>0 && x<wwidth-1 && y>0 && y<height-1)
{
  perform_action();
}
Doing it in 4 separate ifs is exactly the same.

Jim
Challenge Trophies Won:

Offline ferris

  • Pentium
  • *****
  • Posts: 841
  • Karma: 84
    • View Profile
    • Youth Uprising Home
Re: [C++] The IF statement: Using And.
« Reply #3 on: August 19, 2009 »
Jim's example is my preferred method, though I've found it's a bit smaller for some reason using clyde's proposed method.
http://iamferris.com/
http://youth-uprising.com/

Where the fun's at.
Challenge Trophies Won:

Offline TinDragon

  • Pentium
  • *****
  • Posts: 644
  • Karma: 24
    • View Profile
    • J2K's blog
Re: [C++] The IF statement: Using And.
« Reply #4 on: August 20, 2009 »
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

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: [C++] The IF statement: Using And.
« Reply #5 on: August 20, 2009 »
In C, it will 'early out'.  That is, if you have
Code: [Select]
if (a && b)
Then if 'a' is false it won't evaluate 'b' at all.
That would be the same as doing
Code: [Select]
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
Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: [C++] The IF statement: Using And.
« Reply #6 on: August 20, 2009 »
Compilers sometimes make weird decisions - right is one byte shorter:
Code: [Select]
bool test1(int a, int b, int c)
{
   if (a>0)
      if (b>0)
         if (c>0)
            return true;
   return false;
}
Code: [Select]
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             
Code: [Select]
bool test2(int a, int b, int c)
{
   return (a>0 && b>0 && c>0);
}




Code: [Select]
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)
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: [C++] The IF statement: Using And.
« Reply #7 on: August 20, 2009 »
Nice example, thanks very much!

Jim
Challenge Trophies Won:

Offline TinDragon

  • Pentium
  • *****
  • Posts: 644
  • Karma: 24
    • View Profile
    • J2K's blog
Re: [C++] The IF statement: Using And.
« Reply #8 on: August 20, 2009 »
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   


Offline ferris

  • Pentium
  • *****
  • Posts: 841
  • Karma: 84
    • View Profile
    • Youth Uprising Home
Re: [C++] The IF statement: Using And.
« Reply #9 on: August 21, 2009 »
Thanks for the example hellfire :) I remember GCC being the opposite, probably not now with the newer versions etc :)
http://iamferris.com/
http://youth-uprising.com/

Where the fun's at.
Challenge Trophies Won: