Be careful while nesting Brackets

Posted: July 4, 2013 in Programming
Tags: ,

CPP:

int x = (a > b) ? 1 : 0;

The condition a > b will be evaluated and if it is true x will get 1 or else 0. we can also write the same statement without conditional operator

int x = (a > b);

In my project i was about to do something lyk this

if ( getValue(id) > 0 ) 

but , i misplaced the bracket like this

if ( getValue(id > 0 ))

it doesn’t give the syntax error. But at the same time , it wont give us the desired result since the inner expression evaluates like a conditional operator and returns either 0 or 1. So , be careful when nesting brackets

Advertisements

Comments are closed.