CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Condition problem

  1. #1
    Join Date
    Dec 2007
    Location
    France
    Posts
    322

    Condition problem

    What is wrong with this condition?
    Why the else statement never gets executed?

    Code:
    #include <iostream>
    #include <stdlib.h>
    using namespace std;
    
    
    int main(int argc, char** argv)
    {
            for (int size = 0; size < 65; size++)
    
            if  (0<size<7 || 14<size<21 || 28<size<35 || 42<size<49)
                 cout<<"1";
            else cout<<"0";
    
            cout<< "\n ";
    
    system("pause");
    return 0;
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,768

    Re: Condition problem

    You probably want to write 0 < size && size < 7 instead of 0<size<7.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Condition problem

    Yes, 0<size<7 is evaluated as

    (0<size)<7

    in other words, the two possible outcomes are
    0 < 7 ===> True
    1 < 7 ===> True

  4. #4
    Join Date
    Dec 2007
    Location
    France
    Posts
    322

    Re: Condition problem

    Ah ok. I thought 0 < size && size < 7 and 0<size<7 was the same.

    God bless both of you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)