473,406 Members | 2,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

short if else

Hy,

I've got an short ifelse notation, but can give only one action like:

$test == 1 ? $do = true : $do = false;

But this gives an parse error:

$test == 1 ? $do = true $who = "me" : $do = false;
or
$test == 1 ? $do = true; $who = "me"; : $do = false;

All gives a parse error.

How can i do this?

Tnx.
Dec 28 '05 #1
16 24227
"Moneypenny" <ge******@planet.nl> wrote in message
news:43***********************@text.nova.planet.nl ...
Hy,

I've got an short ifelse notation, but can give only one action like:

$test == 1 ? $do = true : $do = false;

But this gives an parse error:

$test == 1 ? $do = true $who = "me" : $do = false;
or
$test == 1 ? $do = true; $who = "me"; : $do = false;

$test==1 ? $do = true : $do = false;
$test==1 ? $who = 'me' : 0 ;

(in this particular case it could be expressed as:
($do = $test==1) ? $who = 'me' : '';
in order to make it really short)

You _could_ do it the way you intended to do it, but that would be bad
coding practise, so I'm not gonna say how.

--
"En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö
an*******************@gmail.com.NOSPAM.invalid
Dec 28 '05 #2
iuz
Kimmo Laine wrote:
"Moneypenny" <ge******@planet.nl> wrote in message
news:43***********************@text.nova.planet.nl ...
Hy,
[..]
$test==1 ? $do = true : $do = false;
$test==1 ? $who = 'me' : 0 ;

(in this particular case it could be expressed as:
($do = $test==1) ? $who = 'me' : '';
in order to make it really short)

You _could_ do it the way you intended to do it, but that would be bad
coding practise, so I'm not gonna say how.


but this way the third part of the ternary operator is not required so..
$do = $test == 1 && $who = 'me';
IMHO is the shortest way to make it..

--
www.iuz-lab.info
Dec 28 '05 #3
Why? Why not go with standard if/else?

Dec 28 '05 #4
iuz wrote:
Kimmo Laine wrote:
"Moneypenny" <ge******@planet.nl> wrote in message
news:43***********************@text.nova.planet.nl ...
Hy,

[..]
$test==1 ? $do = true : $do = false;
$test==1 ? $who = 'me' : 0 ;

(in this particular case it could be expressed as:
($do = $test==1) ? $who = 'me' : '';
in order to make it really short)

You _could_ do it the way you intended to do it, but that would be bad
coding practise, so I'm not gonna say how.


but this way the third part of the ternary operator is not required so..
$do = $test == 1 && $who = 'me';
IMHO is the shortest way to make it..

Leaving issues such as maintainability and clarity aside...

$do = $test && $who = 'me';

Since $do = $test will be resolved as boolean by the && operator.

-david-

Dec 28 '05 #5
try:

$do = ($test == 1) ? true : false;

regards.

Dec 28 '05 #6
black francis said the following on 28/12/2005 14:37:
try:

$do = ($test == 1) ? true : false;


Well, that can be shortened to:

$do = ($test == 1);
--
Oli
Dec 28 '05 #7
David Haynes said the following on 28/12/2005 12:31:
iuz wrote:
Kimmo Laine wrote:
"Moneypenny" <ge******@planet.nl> wrote in message
news:43***********************@text.nova.planet.nl ...

Hy,


[..]
$test==1 ? $do = true : $do = false;
$test==1 ? $who = 'me' : 0 ;

(in this particular case it could be expressed as:
($do = $test==1) ? $who = 'me' : '';
in order to make it really short)

You _could_ do it the way you intended to do it, but that would be bad
coding practise, so I'm not gonna say how.


but this way the third part of the ternary operator is not required so..
$do = $test == 1 && $who = 'me';
IMHO is the shortest way to make it..

Leaving issues such as maintainability and clarity aside...

$do = $test && $who = 'me';

Since $do = $test will be resolved as boolean by the && operator.


This will only work, of course, if we are assuming that the failure
condition is (0 == $test), and not (1 != $test).
--
Oli
Dec 28 '05 #8
Oli Filth wrote:
black francis said the following on 28/12/2005 14:37:
try:

$do = ($test == 1) ? true : false;


Well, that can be shortened to:

$do = ($test == 1);

Which can be shortened to:

$do = $test;

unless $test = 0, $do will evaluate as false.

-david-

Dec 28 '05 #9
Moneypenny said the following on 28/12/2005 10:51:
Hy,

I've got an short ifelse notation, but can give only one action like:

$test == 1 ? $do = true : $do = false;

But this gives an parse error:

$test == 1 ? $do = true $who = "me" : $do = false;
or
$test == 1 ? $do = true; $who = "me"; : $do = false;

All gives a parse error.

How can i do this?


Use a standard if-else. The ternary operator ?: is *not*, repeat *not*,
a short-hand form for if() {...} else {...}.

It's designed to be used to evaluate a value that's dependent on some
condition, i.e.:

$result = ($test_expression) ? $val_if_true : $val_if_false;

Now, it just so happens that assignments (e.g. $do = true in your
example above) resolve to a value, and so can be used as $val_if_true or
$val_if_false. Don't let that fool you into thinking that you can
execute arbitrary blocks of code inside a ternary statement.

--
Oli
Dec 28 '05 #10
David Haynes said the following on 28/12/2005 14:42:
Oli Filth wrote:
black francis said the following on 28/12/2005 14:37:
try:

$do = ($test == 1) ? true : false;

Well, that can be shortened to:

$do = ($test == 1);

Which can be shortened to:

$do = $test;

unless $test = 0, $do will evaluate as false.


Technically, it's not the same. In the first two examples, $do will
*always* be a boolean. But in your new version, $do could be anything,
dependent on what type $test is.
--
Oli
Dec 28 '05 #11
Oli Filth said the following on 28/12/2005 14:53:
David Haynes said the following on 28/12/2005 14:42:
Oli Filth wrote:
black francis said the following on 28/12/2005 14:37:

try:

$do = ($test == 1) ? true : false;

Well, that can be shortened to:

$do = ($test == 1);

Which can be shortened to:

$do = $test;

unless $test = 0, $do will evaluate as false.


Technically, it's not the same. In the first two examples, $do will
*always* be a boolean. But in your new version, $do could be anything,
dependent on what type $test is.


Oh, and another thing. With:

$do = ($test == 1);

$do will resolve to true *only* if ($test == 1), i.e. will resolve to
false for any other value of $test.
With:

$do = $test;

$do will resolve to false *only* if ($test == 0), i.e. will resolve to
true for any other value of $test.
--
Oli
Dec 28 '05 #12
Oli Filth wrote:
David Haynes said the following on 28/12/2005 14:42:
Oli Filth wrote:
black francis said the following on 28/12/2005 14:37:

try:

$do = ($test == 1) ? true : false;
Well, that can be shortened to:

$do = ($test == 1);

Which can be shortened to:

$do = $test;

unless $test = 0, $do will evaluate as false.


Technically, it's not the same. In the first two examples, $do will
*always* be a boolean. But in your new version, $do could be anything,
dependent on what type $test is.

I disagree. My version simply relies on the casting rules for int to
boolean. The example where ($test == 1) implicitly says that $test is of
type integer not boolean since we know it would be $do = ($test == true)
if $test was boolean.

Ah! The joys of loosely typed languages...

-david-

Dec 28 '05 #13
David Haynes said the following on 28/12/2005 15:44:
Oli Filth wrote:
David Haynes said the following on 28/12/2005 14:42:
Oli Filth wrote:

black francis said the following on 28/12/2005 14:37:

> try:
>
> $do = ($test == 1) ? true : false;

Well, that can be shortened to:

$do = ($test == 1);

Which can be shortened to:

$do = $test;

unless $test = 0, $do will evaluate as false.


Technically, it's not the same. In the first two examples, $do will
*always* be a boolean. But in your new version, $do could be anything,
dependent on what type $test is.

I disagree. My version simply relies on the casting rules for int to
boolean. The example where ($test == 1) implicitly says that $test is of
type integer not boolean since we know it would be $do = ($test == true)
if $test was boolean.


I'm not sure what you're disagreeing with.

$do = ($test == 1) ? true : false;
and
$do = ($test == 1);

are exactly equivalent in every respect.

Whereas:

$do = $test;

not only results in $do being an integer (or whatever type $test is),
but when evaluated as a boolean (e.g. if ($do) {...}), does not result
in the same success/failure conditions. In fact, all you're doing is
copying the value, so this version achieves nothing!

--
Oli
Dec 28 '05 #14
On Wed, 28 Dec 2005 11:51:29 +0100, Moneypenny wrote:
All gives a parse error.

How can i do this?

Tnx.


Well, everything else aside, there is a trick you can use:

$ cat /tmp/ttt
#!/usr/local/bin/php
<?php
$test=1;
$test == 1 ? eval('$do = true; $who = "me";') : $do = false;
echo "Do: $do Who:$who\n";
?>

$ php /tmp/ttt
Do: 1 Who:me
$

Of course, the code would be much more readable with if/then,
just as the others have told you.

--
http://www.mgogala.com

Dec 28 '05 #15
"Mladen Gogala" <go****@sbcglobal.net> kirjoitti
viestissä:pa****************************@sbcglobal .net...
On Wed, 28 Dec 2005 11:51:29 +0100, Moneypenny wrote:
All gives a parse error.

How can i do this?

Tnx.


Well, everything else aside, there is a trick you can use:

$ cat /tmp/ttt
#!/usr/local/bin/php
<?php
$test=1;
$test == 1 ? eval('$do = true; $who = "me";') : $do = false;
echo "Do: $do Who:$who\n";
?>

$ php /tmp/ttt
Do: 1 Who:me
$

Yep, this is the one I was thinking. But NO. Let me say that just one more
time, NOOOO! As I said this would be very bad coding practise, since there
is no reason to use eval to write a shorthand if-else. It's just stupid. How
I wish you hadn't brought it up. And yet you did... It is said that if you
need to use eval, you're not doing it right. Using eval and the ternary
operator instead of if and else is not the choice of a sane person.

--
SETI @ Home - Donate your cpu's idle time to science.
Further reading at <http://setiweb.ssl.berkeley.edu/>
Kimmo Laine <an*******************@gmail.com.NOSPAM.invalid>
Dec 28 '05 #16
On Wed, 28 Dec 2005 21:53:08 +0200, Kimmo Laine wrote:
Using eval and the ternary
operator instead of if and else is not the choice of a sane person.


I agree, it is bad coding, but it was a nice problem to solve.
I had fun.

--
http://www.mgogala.com

Dec 28 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: marco_segurini | last post by:
Hi, when the following code is executed bool Test1(); bool Test2(); .... if (Test1() || Test2()) { ... } ....
12
by: Alan | last post by:
how to convert double to short ? for example, I want to convert double doubleVal1 = 15000.1; double doubleVal2 = 12000.0; short shortVal; shortVal = doubleVal1 - doubleVal2; I...
99
by: Glen Herrmannsfeldt | last post by:
I was compiling a program written by someone else about six years ago, and widely distributed at the time. It also includes makefiles for many different systems, so I know it has been compiled...
34
by: Andy | last post by:
Hi, Are 1 through 4 defined behaviors in C? unsigned short i; unsigned long li; /* 32-bit wide */ 1. i = 65535 + 3; 2. i = 1 - 3; 3. li = (unsigned long)0xFFFFFFFF + 3; 4. li = 1...
3
by: ram | last post by:
hi all, apart from readability, and not wanting the otherwise different statements to be assumed as one single loong statement what are the pros/cons of both snippets? i tried this out on a...
5
by: _Andy_ | last post by:
I'm looking for the alogithm to take a piece of 8-bit audio data, and scale it to a 16-bit short. I got as far as: private static short ByteToShort(byte b) { int word = 0; word |= ( ( b &...
18
by: Torben Laursen | last post by:
Hi Can anyone tell me what is the short-cut to switch between a .h and .cpp file, thanks Torben Laursen ---
10
by: Jim Langston | last post by:
Is the following well defined? size_t IntVal = 65537; unsigned short Length; if ( IntVal static_cast<unsigned short>( -1 ) ) { std::cout << "Value too long to fit in a short" << std::endl;...
17
by: spasmous | last post by:
I need a way to search through a block of memory for a char array "DA" using a pointer to a short. Ideally I would like to write something like: short *data = ... some data...; int j = 0;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.