Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why i cant check if a variable have a value using == true?

Tags:

c++

c

if(10) it is true, but if(10 == true) is false. Can someone tell me why the first case convert the number to bool but the second case didnt?

like image 904
shadow00 Avatar asked Jun 04 '16 01:06

shadow00


People also ask

What is the right way to check if the variable A is equal to true?

To sum up: To check if a variable is equal to True/False (and you don't have to distinguish between True / False and truthy / falsy values), use if variable or if not variable . It's the simplest and fastest way to do this.

How do you check if a value is true or false in Python?

You can check if a value is either truthy or falsy with the built-in bool() function. According to the Python Documentation, this function: Returns a Boolean value, i.e. one of True or False .

How do you check if a boolean is true or false?

An alternative approach is to use the logical OR (||) operator. To check if a value is of boolean type, check if the value is equal to false or equal to true , e.g. if (variable === true || variable === false) . Boolean values can only be true and false , so if either condition is met, the value has a type of boolean.

How do you check if a variable is boolean in Python?

We can evaluate values and variables using the Python bool() function. This method is used to return or convert a value to a Boolean value i.e., True or False, using the standard truth testing procedure.


2 Answers

if (10) is equivalent to if (10 != 0), whereas if (10 == true) is if (10 == 1) (since true is promoted to the value 1 of type int).

In layman's terms: Two things that both happen to satisfy some property aren't automatically the same thing.

(E.g. doughnuts and frisbees are both round, but that doesn't mean a doughnut is equal to a frisbee. Integers and booleans can both be evaluated in a boolean context, but that doesn't mean that every integer that evaluates as true is equal to every true boolean.)

like image 178
Kerrek SB Avatar answered Oct 26 '22 00:10

Kerrek SB


if( ... )
{
     // if statement
}

To execute if statement in C++, ... should have a true value.

When you wrote if( 10 ){//something useful}

I think 10 treats as int but not bool variable. The following logic should be applied then

if( 10 ) -> if( bool( 10 ) ) -> if( true )

When you write if( 10 == true ){//something useful}, according to C++ standard, there should be the following logic behind the scene

if( 10 == true ) -> if( 10 == int( true ) ) -> if( 10 == 1 ) -> if( false )

You may write something like

if( 10 != false )

or

if( !!10 == true )

also

if( ( bool ) 10 == true ) // alternatively, if( bool ( 10 ) == true )

In old C (before C99), there is no false or true, but there are 0 or non-0 values.

In modern C (from C99), there is false or true (<stdbool.h>), but they are syntactic sugar for 0 and 1, respectively.

if( 10 ) // evaluates directly since 10 is non-zero value

if( 10 == true ) -> if( 10 == 1 ) -> if( 0 )
like image 42
lllllllllll Avatar answered Oct 25 '22 22:10

lllllllllll