Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is if statement evaluated in c++?

Is if ( c ) the same as if ( c == 0 ) in C++?

like image 904
derrdji Avatar asked Sep 25 '09 19:09

derrdji


People also ask

How if condition is evaluated?

The conditions are evaluated to a Boolean value, which should be either TRUE or FALSE. If the condition is found to be TRUE, the corresponding code will be executed, and there will be no other conditions to be evaluated.

Are IF statements evaluated left to right?

You can combine two or more conditional expressions on the IF statement. ISPF evaluates the conditional expressions on the IF statement from left to right, starting with the first expression and proceeding to the next and subsequent expressions on the IF statement until processing is complete.

What must the condition in an if statement evaluate to?

The block of code inside the if statement is executed is the condition evaluates to true.

What is the syntax of if statement in C?

Syntax. In both forms of the if statement, the expressions, which can have any value except a structure, are evaluated, including all side effects. In the first form of the syntax, if expression is true (nonzero), statement is executed. If expression is false, statement is ignored.


2 Answers

No, if (c) is the same as if (c != 0). And if (!c) is the same as if (c == 0).

like image 119
Jesper Avatar answered Sep 23 '22 16:09

Jesper


I'll break from the pack on this one... "if (c)" is closest to "if (((bool)c) == true)". For integer types, this means "if (c != 0)". As others have pointed out, overloading operator != can cause some strangeness but so can overloading "operator bool()" unless I am mistaken.

like image 37
D.Shawley Avatar answered Sep 23 '22 16:09

D.Shawley