tags:

views:

244

answers:

9

Hi all,

Am using a XML sort of language which doesnt have '||' operator. How can i achieve the effect of this operator? The language doesnt support ternary operator also. Other than if-else approach is there any other way to achieve this?

The expression is, if((x == 2) || (y == 2)), and should achieve this without ||, ?: , if-else.. thanks in advance

+1  A: 

Split the or explicitly:

if (x == 2) {
  ... do stuff 
} else if (y == 2) { 
  ... do the same stuff 
} else { 
  condition not fulfilled 
}

Now, without else ... I know one 'xml sort of language' with that: XSL(T), in which you have a <xsl:if> but not the obvious <xsl:else>. What you do have there however is a more general <xsl:choose> that supports multiple conditions using <xsl:when> and also an <xsl:otherwise> to express the 'other' cases.

So, try searching in your 'xml sort of language' whether it maybe supports a case/switch/choose functionality.

ankon
He says theres no `if else`.
Daniel A. White
+3  A: 
temp = false;
if (x==2)
{
   doSomething();
   temp = true;
}
if (y==2)
{
   if (!temp)
       doSomething();
}

This also could work

if (x==2)
{
   doSomething();
}
else
{
   if (y==2)
       doSomething();
}
Daniel A. White
If x==2 and y==2 then DoSomething twice?
Paul Mitchell
Except if x==2 and y==2 doSomething() will be called twice
nikie
True. Let me fix that.
Daniel A. White
Now you are using else ...
Stephen C
But the condition is separate.
Daniel A. White
I see your answer is now the same as mine :-)
Stephen C
@ Stephen: I'm sorry my edit made his answer the same as yours. If I'd seen yours I'd just upvoted yours.
Georg
@Gs - please don't edit my answers to obilivion.
Daniel A. White
A: 

The only thing I can think of is repeating the set of instructions executed or specified for the case of x==2 and y==2. The problem here seems to come from the language, as it should at least support basic logical operators in order to be usable.

Diego Sevilla
+1  A: 
if (!((x != 2) && (y != 2))) {
    work();
}
pmg
A: 

Does this "XML sort of language" support &&? If so, invert the logic:

if ((x == 2) || (y == 2)) converts to: if (!((x != 2) && (y != 2)))

spoulson
A: 

How about:

boolean doIt = (x == 2);

if (!doit) {
    doIt = (y == 2);
}

if (doIt) {

   // do something
}
rsp
+8  A: 

How about this:

if (x == 2) {
    flag = true;
}
if (y == 2) {
    flag = true;
}
if (flag) {
    // do something
}


Or this:

if ((x == 2) | (y == 2)) {
    // do something
}

Note that this uses the non-short-circuit OR operator | rather than ||.

Stephen C
+1  A: 

How about:

if((x == 2) + (y == 2))
{
  do_something();
}

It will behave slightly different than the OR-operator because the + operator can't do short-circuit evaluation.

The control-flow of the program will be the same though..

Nils Pipenbrinck
A: 

You could try the following:

if ((x == 2) && (x+y == 4)) {

} else {

}

DarthNoodles