PDA

View Full Version : Evaluation of Logical Expression



TreeFinger
September 21st, 2007, 02:05 PM
I am wondering how this statement will execute:


if ( a < c - 2 )

Will 2 be subtracted from c first? then the logical expression will be evaluated?

LaRoza
September 21st, 2007, 02:11 PM
if ( a < c - 2 )
Will 2 be subtracted from c first? then the logical expression will be evaluated?

Yes, in most languages I know.

You should make precedence clear, however, using ().


if ( a < (c - 2) )

TreeFinger
September 21st, 2007, 02:19 PM
Yes, if I was writing the code I would definitely include parentheses. I just have a test today and wanted to double check :) Thanks.

LaRoza
September 21st, 2007, 02:26 PM
Yes, if I was writing the code I would definitely include parentheses. I just have a test today and wanted to double check :) Thanks.

Rats, I didn't mean to help you cheat.

In case you ever have a quick question like that, try it out and see, Python in interactive mode works wonders.