if (x) means???

Jul 31, 2013 at 6:13pm
#include<iostream.h>
int main()
{
int x=0,y=0,z=0;
if (x)
{
x--;
y=x+2;
cout<<x--<<endl;
cout<<y;
}
else cout<<x++;
return 0;
}

what does if(x) mean here?
Jul 31, 2013 at 6:15pm
closed account (Dy7SLyTq)
c style version of bool ie false is < 1 and true > 0
Jul 31, 2013 at 6:20pm
if ( x )

is equivalent to

if ( x != 0 )

Jul 31, 2013 at 8:19pm
Vlad is correct and DTSCode is not correct.

In C and C++, zero is false, and non-zero values are true.
Last edited on Jul 31, 2013 at 8:24pm
Jul 31, 2013 at 9:14pm
closed account (Dy7SLyTq)
actually no im correct. my compiler accepts <= 0 as false
Jul 31, 2013 at 9:28pm
actually no im correct. my compiler accepts <= 0 as false

Then it must not be a C or C++ compiler.
Jul 31, 2013 at 9:29pm
@DTSCode

actually no im correct. my compiler accepts <= 0 as false


I am sure you are wrong.
Jul 31, 2013 at 9:32pm
closed account (Dy7SLyTq)
im sure im right
Jul 31, 2013 at 9:36pm
Certainty notwithstanding, you are wrong.
Jul 31, 2013 at 9:37pm
@DTSCode


Boy read books on C/C++ for beginners. It will be useful for you.
Last edited on Jul 31, 2013 at 9:38pm
Jul 31, 2013 at 9:41pm
closed account (Dy7SLyTq)
i have. my compiler lets me use negative ints as false. but vlad, you just like to say im wrong so im going to stop talking to you now
Jul 31, 2013 at 9:44pm
Then, as has been said, your compiler does not conform to the C or C++ standards and I would recommend getting one that does.
Jul 31, 2013 at 9:46pm
@DTSCode

i have. my compiler lets me use negative ints as false. but vlad, you just like to say im wrong so im going to stop talking to you now


No problem.:) I only do not know what you are doing here with your personal compiler and giving others wrong advices because others have no and never will have your compiler.:)
Jul 31, 2013 at 10:36pm
closed account (z05DSL3A)
DTSCode wrote:
actually no im correct. my compiler accepts <= 0 as false
Now there's a lesson to never trust your compiler. Compilers are not always correct or compliant. What compiler is it?

edit: the OPs code is also non-standard.
Last edited on Jul 31, 2013 at 10:43pm
Aug 1, 2013 at 12:11am
@DTSCode
See http://en.cppreference.com/w/cpp/language/implicit_cast scroll down to boolean conversions.
Aug 1, 2013 at 6:08am
le standard wrote:
4.12 Boolean conversions [conv.bool]

A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true. A prvalue of type std::nullptr_t can be converted to a prvalue of type bool; the resulting value is false.
Aug 1, 2013 at 8:22am
Topic archived. No new replies allowed.