Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: if/else syntax

by ikegami (Patriarch)
on Jun 26, 2009 at 15:08 UTC ( [id://775085]=note: print w/replies, xml ) Need Help??


in reply to if/else syntax

Brackets are mandatory on if statements in Perl.

Note that, unlike C and Pascal, these are defined in terms of BLOCKs, not statements. This means that the curly brackets are required--no dangling statements allowed.

The optionality of the brackets is a source of problems for C.

In Perl 6, it's the parens around the condition that are optional. They're already optional around the condition of statement modifiers in Perl 5.

someFunc() if ($someVar == $anotherVar); someFunc() if $someVar == $anotherVar; # Same thing

Replies are listed 'Best First'.
Re^2: if/else syntax
by Anonymous Monk on Jun 26, 2009 at 15:48 UTC
    so if the brackets are there to avoid problems, the C/perl syntax (a?b:c) also has those problems right ?

      (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.

      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
        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
      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."

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://775085]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-26 02:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found