Re: [PIC] Learning C

smplx <[email protected]>
Newsgroups gmane.comp.hardware.microcontrollers.pic
Message-ID <[email protected]>
Hi Spehro,

In SOME cases I agree with you ternary does seem to makes code easier to 
follow but in others it can actually cause problems. I only use it where 
the condition is VERY simple.

I recently had a situation in PHP where something that looked like a 
VERY simple ternary caused major havoc with a web server and was causing 
IP addresses to be locked out by the firewall.

Something like:

echo "<img src='", x ? "LED_ON.png" : "LED_OFF.png", "' />\n";

The server couldn't find LED_ON.png and so PHP was throwing up an error 
message wich was huge problems for the server.

The nice thing about PHP is that the sytax closely follows C so it is 
very easy to pick up - another good reason for learning C :-)

Friendly Regards
Sergio Masci


On Thu, 14 Nov 2024, Spehro Pefhany wrote:

> At 03:56 AM 2024-11-14, you wrote:
>> Ans 404 to you :-)
>> 
>> Onew of the problems I have with trying to understand code examples in C is
>> that so many of them are incomprehensibly dense.
>> What would be five or six lines in Pascal or Basic is a single line in C.
>> I appreciate that that is down to the programmer not the language but it is
>> off putting
>> i have to remind my self of the old adage:  You can write Pascal in any
>> language
>
> I do like the ternary operator '?' in C (and in some other languages)
> which reduces several lines to one and is still very readable, maybe more
> so.
>
> variable = condition ? value_if_true : value_if_false
>
> instead of
>
> if (a > b) {
>    result = x;
> }
> else {
>    result = y;
> }
>
>
> just write
>
> result = a > b ? x : y;
>
> Best regards, Spehro Pefhany 
> -- 
> http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
> View/change your membership options at
> https://mailman.mit.edu/mailman/listinfo/piclist
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.