Voodoo Child
03-18-04, 01:26 AM
Not exactly a giant problem but how do you right a shorthand if with no else segment, ie
a = (b == c) ? d: e;
But without the e bit.
This is really irritating me.
testify
03-18-04, 04:07 AM
Have you tried just removing e like so?:
a = (b == c)? d:;
It's not a very common thing to use this syntax for an if statement without the else because an if statement can just be as long if it was:
if( b == c) { a = d };
OR if it was:
a = (b == c)? d:;
Normally the latter syntax is used for else statements that have recurring lines such as this:
if (Character.isUpperCase(aChar)) {
System.out.println("The character " + aChar + " is upper case.");
} else {
System.out.println("The character " + aChar + " is lower case.");
}
Which could be made smaller into this:
System.out.println("The character " + aChar + " is " +
(Character.isUpperCase(aChar) ? "upper" : "lower") +
"case.");
you could always also simply use the current value. Since you want a to take the value of d or stay the same do this:
a = (b == c) ? d : a;
-AntonK
Voodoo Child
03-18-04, 10:27 PM
Yes, I tried
a = (b == c)? d:;
...didn't work.
The particular code was in a for loop so your classical if wouldn't work. I should probably stop being so anal.
testify
03-19-04, 03:38 AM
mind showing the code you're using?
malkiri
03-23-04, 09:38 AM
Removing the third expression shouldn't be expected to work - if (b == c) evaluated to false, you'd be left with an assignment something like: a = ;, which is obviously not valid.
I assume you want your code to look something like this:
for (int i=0; i<n; ++i, a = (b == c) ? d:a)
{
...
}
AntonK's suggestion will do what you want, but before you use it...is there a good reason not to do the following?
for (int i=0; i<n; ++i)
{
if (b == c)
a = d;
}
It's clearer and more understandable, partly because the ternary operator takes a couple glances, and partly because it doesn't mix the loop control variables and your program's data. Plus, you'll avoid an unnecessary assignment of a = a whenever b != c, assuming it's not optimized out by the compiler.
testify
03-24-04, 12:51 AM
This is a bit off topic but I didn't realize you could put more than one statement in a for loop section. Does it really work like that...seperating statements in the starting of the loop with commas to trigger them every time the loop occurs?
Edit: tried to put the part of the code that mark showed, but it was messed up, even in the code tags...so meh.
okinrus
03-24-04, 01:30 AM
The use of the ternary operater can be justified when it represents something not in the main path of control. For example, the use of it in the System.out.print statement mentioned above can be justified because the two paths of control don't effect the major path of control through the method. That said, it's almost never used except for short inline method functions in C++.
The "," operator is a sequencing operator in C++, but I'm not sure about java. The Java for-loop grammar accepts a statement expression list, which is a list of statement expressions separated by commas, in the for update portion. But the statement expression does not include all statements but only the ones list <a href="http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#5984">here</a>
malkiri
03-24-04, 08:10 AM
I don't know Java well enough to know for certain, but I'd be fairly surprised if it had a different syntax (regarding multiple statements). Officially, my example was in C++ :)
malkiri
03-24-04, 07:49 PM
Of course. My reply was regarding the question was about the comma separator.
Edit: Hmm...did you delete your reply, okinrus?
okinrus
03-24-04, 08:52 PM
Yes, my bad. It's likely that Java uses the same syntax as C on this.