in reply to Re: if/else syntax
in thread if/else syntax

so if the brackets are there to avoid problems, the C/perl syntax (a?b:c) also has those problems right ?

Replies are listed 'Best First'.
Re^3: if/else syntax
by ikegami (Patriarch) on Jun 26, 2009 at 16:49 UTC

    (a?b:c) also has those problems right ?

    No, because the ":" isn't optional like else is, and because people don't indent the conditional operator like they would an if/else.

    It has a different problem, though. A frequent mistake with the conditional operator is to use it as follows:

    $c ? $x = $i : $y = $j;
    The above means
    ( $c ? $x = $i : $y ) = $j;
    but people expect it to mean
    $c ? ($x = $i) : ($y = $j);
      a thats true.
      indeed, the fact that ":" isn't optional changes everything, now it makes more sense. thanks.
Re^3: if/else syntax
by Crackers2 (Parson) on Jun 26, 2009 at 16:10 UTC

    I don't think so. With if/else:

    if a if b c else d
    could mean
    if a { if b { c } else { d } } or if a { if b { c } } else { d }

    With the ternary operator that would be

    a ? b ? c : d : () and a ? b ? c : () : d

    so there's no abmbiguity.

      There's actually no ambiguity in the C syntax for your example. The compiler understands it fine. The problem is that the programmer/reader of the code does not always read it the same way.

      if(a) if(b) c(); else d();

      According to what I recall, the else matches with the nearest if in this case. Unfortunately, indentation gives humans fits on this.

      if(a) if(b) c(); else d();

      This does the same thing (else goes with if(b)). As I said, there's nothing ambiguous about it.

      G. Wade
        thanks, thats exactly what I'm trying to say, if the programmer knows how the compiler interprets the code he can use it in the right way.

        The compiler understands it fine.

        Not true. How can the compiler understand it fine when C doesn't define it

        if(a) if(b) c(); else d();

        means

        if(a) { if(b) c(); else d(); }

        or

        if(a) { if(b) c(); } else d();

        Both are right. Neither is right.

        I don't know if that was fixed in recent versions of C.

        There's actually no ambiguity in the C syntax for your example.

        That's not true either. You contradict yourself in the next sentence. You write code for both people and for compilers to read. Ambiguity for either is still ambiguity.

        Anyway, the topic wasn't ambiguity but (the avoidance of) traps. C's if/else definitely has pitfalls.

      so why we can't have a if/else syntax without brackets that behaves just like the ternary operator ?
      if a { if b { c } else { d } } or if a { if b { c } } else { d }

      Does not matter who the compiler will assume, since if it get implemented we would know it before hand
Re^3: if/else syntax
by dreadpiratepeter (Priest) on Jun 26, 2009 at 15:51 UTC
    what you are looking for is:
    if ($cond==1) { do_good(); } else { do_bad(); }


    -pete
    "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."