Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

What is the difference between a Statement and an Expression?

by supernewbie (Beadle)
on Aug 02, 2001 at 10:31 UTC ( #101594=perlquestion: print w/ replies, xml ) Need Help??
supernewbie has asked for the wisdom of the Perl Monks concerning the following question:

Master Perl Monks:

What is the difference between a Statement and an Expression?

Could anyone please explain their differences?

$x = 1+1 # this is an expression? 1+1 # but this is a statement?
I am very confused...

Comment on What is the difference between a Statement and an Expression?
Download Code
Re: What is the difference between a Statement and an Expression?
by azatoth (Curate) on Aug 02, 2001 at 10:32 UTC

    Seems confusing, doesn't it?

    IMO, a statement is usually something like

    if ($this == 1) { do_that($thing) };

    Meaning part of your code that evaluates an expression, or EXPR. In this example, the EXPR is $this == 1, and the if statement evaluates it to return a certain value and perform a function based on the outcome.

    It seems to me you are just beginning your journey in the world of Perl, and would do well to invest in a book - perhaps Learning Perl.

    Update:

    In the simplest possible terms :

    Expression

    # this is an expression $foo = 1; # so is this $bar = ($foo . $baz);

    Statement

    # statements are like : #if .. else #while .. do #for .. # etc etc

    Azatoth a.k.a Captain Whiplash

    Make Your Die Messages Full of Wisdom!
    Get YOUR PerlMonks Stagename here!
    Want to speak like a Londoner?
      There is no technical difference between a statement and an expression. They are "human" terms.

      For example, one might say that...

      $x -= 1

      ..is a statement. "Decrease the value of x by one."

      Compare with:

      while ($x -= 1) { ... }

      ...where the same statement is being used as an expression.

      You should mentally parse the words statement and expression as just "piece of code".

      The EXPR context is another story however, and a has a very specific meaning to the Perl parser. That is an aside however. When people say expression they rarely mean precisely an EXPR context.

Re: What is the difference between a Statement and an Expression?
by Anonymous Monk on Aug 02, 2001 at 10:45 UTC
    A statement normally tells the compiler to do something or execute something. An expression is a value - typically represented by a variable or whatnot.
Re: What is the difference between a Statement and an Expression?
by Petruchio (Vicar) on Aug 02, 2001 at 11:17 UTC
    If I am off the mark, here, I trust our local computer scientists will set me straight.

    Quoth the Camel's glossary:


    expression
    Anything you can legally say in a spot where a value is required. Typically composed of literals, variables, operators, functions, and subroutine calls, not necessarily in that order.

    statement
    A command to the computer about what to do next, like a step in a recipe: "Add marmalade to batter and mix until mixed." A statement is distinguished from a declaration, which doesn't tell the computer to do anything, but just to learn something.


    Similarly, Barron's Dictionary of Computer and Internet Terms says an expression is a series of symbols that can be evaluated to have a particular value, while a statement is a single instuction in a computer language.

    In your example, then, 1+1 is actually the expression; it's not much of a statement, since it (normally) doesn't tell Perl to do anything meaningful.

    $x = 1+1 is a statement, if you terminate it with a semicolon. It tells Perl to do something... evaluate the expression 1+1 and assign the result to $x.

    It's not actually quite as clear cut as that, however. For instance, I believe 1+1 can qualify as a statement, as when you use it thus:

    print two(),"\n"; sub two { 1+1 }

    Here it's telling Perl that the return value of &two should be 2. Likewise, $x = 1+1 can be used as an expression:

    perl -e 'print "Yup\n" if $x = 1+1;'

    Here $x = 1+1 is used as an expression, with the conditional depending on its return value (which happens to be 2).

    If this seems vague, remember that these terms are for your use, to help you express yourself. Whether you choose to call $x = 1+1 a statement or an expression tells us something of the way you're regarding it in the present context.

Re: What is the difference between a Statement and an Expression?
by mitd (Curate) on Aug 02, 2001 at 12:06 UTC
    Lets look at a little grammar for a simple compiler perhaps this will help.
    expression -> expression + term | expression - term | term statement-> id = expression | if expression then statement | while expression do statement

    Notice that in my language grammar (taken directly out of he 'Red Dragon' Compiler Book, Aho, Sethi, Ullman). Statements always begin with either a identifier or a keyword.

    $x = 1+1; <--- statement starts with an identifier x followed by an expression 1+1 </code>

    mitd-Made in the Dark
    'My favourite colour appears to be grey.'

      Statements always begin with either a identifier or a keyword.
      That's not true for Perl:
      1 == $variable or next;
      It's also not true for C.
      3;
      is a valid, although useless, statement.
Re: What is the difference between a Statement and an Expression?
by Boots111 (Hermit) on Aug 02, 2001 at 17:41 UTC
    It seems like all of the answers drive at a fundamental difference. Expressions do something, while statement do not (always) do something.

    Thus a quick and dirty answer would be that an expression is anything typically followed by a ;
    If, else, while, and for do not usually have a ; after them because they just determine when (and how often) the assossiated expression is evaluated.

    If is improtant to note that any expression with a return value can be used as a statement. For example
    $x = $y; is an expression; however, it is used as the statement part of the expression
    $z = ($x = $y); (the parentheses are just there for clarity).

    Hope that helps,
    Matt
Re: What is the difference between a Statement and an Expression?
by herveus (Parson) on Aug 02, 2001 at 18:52 UTC
    Perl's flexibility doesn't make this as easy to answer as it might in some languages. $x = 1+1; can be considered a statement. But it can also be considered an expression. After all, you could have a sub that ends with this statement. That sub's return value will be the value of the statement, 2. English semantics could lead you to the conclusion that a statement might have expressions as parts of it, but not the other way around. Perl is not so restrictive. My counsel: don't get too wrapped up in discerning the difference. It mostly won't matter. yours, Michael
Re: What is the difference between a Statement and an Expression?
by pmas (Hermit) on Aug 02, 2001 at 20:37 UTC
    I can understand why you are confused. Here is my attempt to add light to darkness.. :)

    expression is just a way to calculate a value, using operators, build-in or your own functions.
    statement is more ezoteric: it may be assigment, variable declaration (my,...), subroutine call, control structures (if, while, foreach, block = {}, pragma (use...), etc.

    And now, IMO what confuses you: sometimes statement might have a value like expression has, and statement can be used also as expression sometimes. Simple example:

    while ($line = <>) { print $line; }
    Here $line = <> is used as statement (reading a line from STDIN) and also as expression (returning value assigned to $line. String is TRUE if something (not undef) was read in, and undef is returned when End_Of_File was reached. It might be confusing for beginners, but it is extremely convenient.
    You'll find many idioms like this, Larry Wall (inventor of perl) made perl so flexible, there is even abbreviation for that: TIMTOWTDI - There Is More Than One Way To Do It. Perl tries to help you. Just always  use strict and use -w switch, everything else will come along.

    Your experience will grow, and your fun using perl will follow. Welcome in monastery, and come again. There is lot of information for you around.

    I gave you ++ for noticing the difference.

    pmas
    To make errors is human. But to make million errors per second, you need a computer.

Re: What is the difference between a Statement and an Expression?
by inverse (Acolyte) on Aug 02, 2001 at 23:52 UTC

    Don't try to read too much into the difference beween a statement and an expression.

    The main point to remember is that an expression can be used inside a statement, or another expression. You can have a subexpression, but there is not such thing as a "sub-statement".

    The distinction between statement and expression is just an artifact of the language. Some languages only have expressions (LISP), and others only have statements (assembly).

    Look at the following example. Both do the same thing, but the first is a statement and the other an expression.

    if ($foo) { print "True\n" } else { print "False\n" } $foo ? print "True\n" : print "False\n"
    In the next example the first statement will work but the second will not.
    print $foo ? "True\n" : "False\n"; print if ($foo) { "True\n" } else {"False\n"};

    Why not? Mostly just because C is that way. If your not confused, notice that what I called an expression in the first example, is now a statement becuase there is a semicolon at the end. It is a subtle bit of terminology that really isn't a big deal.

      But note that:
      print $foo ? "True\n" : "False\n";
      is the same as
      print do { if ($foo) { "True\n" } else { "False\n" } };
      See do.
      You can have a subexpression, but there is not such thing as a "sub-statement".
      And then you give an example of a statement:
      if ($foo) { print "True\n" } else { print "False\n" }
      What's the print "True\n" then? Is that not a statement?
Re: What is the difference between a Statement and an Expression?
by BrentDax (Hermit) on Aug 03, 2001 at 09:53 UTC
    Expression and statement are nearly synonymous. A statement is basically an expression that stands on its own.

    For example, 1+1 is a statement; however, in $x=1+1, the addition is an expression. The entire line is a statement(unless it's part of something bigger, like if($x=1+1) {}.

    At least that's the way I think of them. Other people's thoughts may differ from mine.

    =cut
    --Brent Dax
    There is no sig.

Re: What is the difference between a Statement and an Expression?
by petral (Curate) on Aug 03, 2001 at 10:11 UTC
    I don't think merlyn has a column on this, but he and tye had an exchange which marches smartly off to the right beginning here.

    Here's a summing up from merlyn:
    'If you keep "statement" and "expression" separate, it's obvious that "EXPR1 while EXPR2;" is a statement, not an expression, and cannot further be modified, and that "EXPR1 or EXPR2" is still an expression and can be used in a larger expression.'

      p

Re: What is the difference between a Statement and an Expression?
by stuffy (Monk) on Aug 03, 2001 at 11:55 UTC
    First of all, I ++ you for comming up with topic that generated as much conversation as this one has. If I were asked to define on a test the difference between the two, but I failed to study (which was usually the case in high school) I would probably write something like this:
    A statement (in perl) is anything that is ended with a ";" while an expression is the part of a statement that actually does something like a mathematical computation, or the assigning of a value etc. The statement doesn't necessarly contain an expression. In reality, as long as you and those you are communicating with know what you are talking about, it doesn't matter what you call it.

    Stuffy
    That's my story, and I'm sticking to it, unless I'm wrong in which case I will probably change it ;~)
    may be reproduced under the SDL
      A statement (in perl) is anything that is ended with a ";" while an expression is the part of a statement that actually does something like a mathematical computation, or the assigning of a value etc.
      So, in
      for (my $i = 0; $i < 10; $i++) { print "\$i = "; print $i }
      there are three statements, my $i = 0;, $i < 10; and print "\$i = ";, but print $i isn't, and neither is the entire for construct?

      Curious.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://101594]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2014-04-18 03:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (460 votes), past polls