IF Statement Notation

Mar 20, 2014 15 Replies

Anyone know how to interpret this IF (I think) statement...



PMI=(PM>=0)?((PM==0)?1000:PM):PMC



it's in a notation-style I don't understand.



Thanks! ...Jim Thompson



-- | James E.Thompson | mens | | Analog Innovations | et | | Analog/Mixed-Signal ASIC's and Discrete Systems | manus | | San Tan Valley, AZ 85142 Skype: Contacts Only | | | Voice:(480)460-2350 Fax: Available upon request | Brass Rat | | E-mail Icon at

formatting link
| 1962 | I love to cook with wine. Sometimes I even put it in the food.


If PM = 0 PMI = 1000 IF PM > 0 PMI = PM If PM < 0 PMI = PMC

John

It looks like C-style conditional operator (whose use is frowned upon, particularly in cascade like that).

In C and C++,

ANSWER = THING ? THIS : THAT;

means that if THING is true, ANSWER = THIS. Of THING is false, ANSWER = THAT.

In more traditional if-else notation, your statement expands to:

if (PM >= 0) { if (PM == 0) { PMI = 1000; } else { PMI = PM; } } else { PMI = PMC; }

Which packs much less confusion per line, and is therefor much less desirable from a geeky programmer point of view.

Those limp-wristed busybodies at MISRA have ruled out its use in mission- critical software, if you can imagine that.

Tim Wescott Wescott Design Services http://www.wescottdesign.com

In English, does this mean:

If PM is exactly 0, PMI=1000 If PM is greater than 0, PMI=PM Otherwise (negative PM?) PMI=PMC

I'm confused by the stacking :-(

Thanks, Tim! C-notation fits... this is from an HSpice device library. ...Jim Thompson

| James E.Thompson | mens | | Analog Innovations | et | | Analog/Mixed-Signal ASIC's and Discrete Systems | manus | | San Tan Valley, AZ 85142 Skype: Contacts Only | | | Voice:(480)460-2350 Fax: Available upon request | Brass Rat | | E-mail Icon at http://www.analog-innovations.com | 1962 | I love to cook with wine. Sometimes I even put it in the food.

Oh gosh, why should something so clear be confusing? ;)

Yes, that's what it means (which is what JM said).

Tim Wescott Wescott Design Services http://www.wescottdesign.com

formatting link

The cascade can get ugly but other than that I don't see the as confusing, it's a simple multiplexer

-Lasse

Always use the freakin' curly brackets. Look at how Apple f***ed up with "goto fail."

Curly braces are a waste of perfectly good bytes that could be put to better purpose elsewhere.

Why, I bet that if you took the entire codebase for the Linux kernel and removed as many curly braces as possible, then you'd have enough disk space left over for one small, low-resolution photograph of a nekkid woman.

Tim Wescott Wescott Design Services http://www.wescottdesign.com

On a sunny day (Fri, 21 Mar 2014 11:41:54 -0500) it happened Tim Wescott wrote in :

The erro many nenid programers make is that they f*ck the indents

if(it_rains) { bring umbrella } else { well you were nekkid anyways, but use suntan }

It is silly to save on spaces or other attributes that make a program more readable (J.L. wants comments) in a langue, that has nothing to do with code space. These days bytes are free when programming. And they use integers (64 bit at that) as flags too.. And that DOES count. The nekkids should try writing code for a PIC with a few hundred bytes RAM and 8 or 16 kB memory, that will teach them.

And, a more natural way would be not to have tests for overlapping cases:

if (PM > 0) { PMI = PM; } else if (PM == 0) { PMI = 1000; } else { PMI = PMC; }

Which translates back to:

PMI = (PM > 0) ? PMI = PM : (PM == 0) ? 1000 : PMC;

formatting link

In spite of what the author of this page wrote, I think Perl is the worst for confusing code. They used to hold contests for the most confusing Perl code until people lost interest.

The 'Obfuscated C contest' has been at it the longest. It started in 1984 and is still going.

One can write obfuscated code in any language.

Jeroen --I like C-- Belleman

In APL, it's a requirement.

Gack.

Although to some extent it would be more readable for a non-programmer by rewriting it in order of ascending value of PM namely

if (PM

if (PM=0) doit // which never works

is not allowed in MISRA C:2012, Rule 13.4 "The result of an assignment operator should not be used"

The Keil ARM C compiler always whinges about 'if (assignment)' with a helpful "did you really mean that" kind of warning.

Michael Kellett

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required