1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

C++ examining conditions

Discussion in 'Scripting & Programming' started by Mof, Aug 18, 2008.

  1. Mof

    Mof Megabyte Poster

    526
    2
    49
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    int a,b,max;  // integer values a=0 b=1 max=2
    a=1,b=2;     // changing integer values of a and b assuming max integer is 2
    
    cout <<"Variable a is:";
    cout <<((a!=1)?"not one,":"one,");// a!(logical not)=1 reversing a to false
                                      // 0 which i make "not one" but this is incorrect
                                      // as the program runs as "one"
    can someone please explain where my thinking has gone wrong.
     
    WIP: C++ and A+
  2. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    This code:

    Code:
    cout <<((a!=1)?"not one,":"one,");
    
    is equivalent to

    Code:
    if(a != 1)                // This is FALSE since a = 1.
      cout "not one,";     // Only print this if the above is TRUE, so is ignored since it's false.
    else                       // For any other value of 'a' print the following...
      cout "one,";          // Print this since 'a' is not equal to 1.
    
    and since you have set 'a' to 1 in your code it will print out "one".
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  3. Mof

    Mof Megabyte Poster

    526
    2
    49


    so the ?(ternary) if true do this :if false do this as it was false it printed second option which was "one"
     
    WIP: C++ and A+
  4. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    Ye, but it would be better to visualise it as:

    <conditional expresstion> ? <True statement> : <False statement>
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  5. Mof

    Mof Megabyte Poster

    526
    2
    49

    I have read somewhere conditional is more widely used than ternary. feel we have just gone up a gear, went
    from first back to neutral back to first now up to second, expect we will go back to first again soon:rolleyes:
     
    WIP: C++ and A+
  6. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    Yep, use conditional. 'Ternary' is actually so old and out-of-use that I've forgotten what it means beyond counting in base 3. :biggrin
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  7. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,623
    115
    224
    Nobody has yet pointed out your error is saying that a! is the logical not of a. To take a logical not you *prefix* with !, not postfix.

    So the parsing of that bit is the same as (a != 1).

    Note that, as I have said before, using spaces in your code makes it much easier to read, and can avoid some types of errors!

    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+
  8. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,623
    115
    224
    This may be so in C++, but the term is alive and well in other languages that use this operator! <grin>

    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+
  9. Mof

    Mof Megabyte Poster

    526
    2
    49
    point taken:study. It was one of my early question too.
     
    WIP: C++ and A+
  10. Mof

    Mof Megabyte Poster

    526
    2
    49
    thats excatly what we are saying the statemant is false, so if we said a = 1 that would be true.
     
    WIP: C++ and A+
  11. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    Just went off to find its exact meaning. Now that is sending me back quite a while! Even though I'm one of the C++ guys here we use a variety of languages (like the Python I had to learn sharpish, amoungst others) and we don't use that term. Thanks for the reminder, though! :biggrin
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  12. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,623
    115
    224
    In a conditional it would have to be (a == 1). Saying a = 1 is initializing a to 1. This is a classic mistake that everyone makes from time to time. :p

    Good compilers can often catch this mistake though.

    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+
  13. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    You've lost me. Did I make a mistake somewhere? :blink
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  14. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,623
    115
    224
    No - sorry. I was replying to Mof, and forgot to quote the section I was talking about.

    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+
  15. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    Who can explain this bit of code.

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int a = 0;
    
    	if(a = 1)
    		printf("I'm true.\n");
    	else
    		printf("I'm false.\n");
    
    	return 0;
    }
    
    It's perfectly valid code even though I typed 'if(a = 1)'. :biggrin
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  16. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,623
    115
    224
    <Grin> It is indeed valid - but sufficiently confusing as to be bad style! And many compilers will query it.

    From memory - the assignment operator will return the value assigned, which is how things like a = b = c = d = 1 work.

    So the conditional will see the value 1, which is 'true'.

    Harry (hoping he is right!)
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+
  17. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    It is bad style, and that is what makes C++ sometimes so difficult. It is perfectly valid C/C++ and shouldn't raise any issues unless you have very strict warnings enabled (if they will indeed work!)

    Are you sure? Run this

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int a = 1;
    
    	if(a = 0)
    		printf("I'm true.\n");
    	else
    		printf("I'm false.\n");
    
    	return 0;
    }
    
    :twisted:
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  18. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    And there I was expecting you to be up-in-arms, hbroomhall, asking why I didn't accept the correct answer. :D
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  19. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,623
    115
    224
    <Grin>

    Actually - I was interrupted just after you posted that (I was at work) and then had to run for the train.

    I *did* check my reply on the train home though! :)

    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+
  20. Mof

    Mof Megabyte Poster

    526
    2
    49
    you dont expect me to get this do you ?
    the bit I cant work out is why it returns "Im true" being int a = 1; If(a = 0) which is false must be something to do with the printf or <stdio.h> in the header
     
    WIP: C++ and A+

Share This Page

Loading...