Expression Versus Statement

前端 未结 21 1749
别那么骄傲
别那么骄傲 2020-11-22 11:54

I\'m asking with regards to c#, but I assume its the same in most other languages.

Does anyone have a good definition of expressions and statements

相关标签:
21条回答
  • 2020-11-22 12:16

    Simply: an expression evaluates to a value, a statement doesn't.

    0 讨论(0)
  • 2020-11-22 12:17

    Most precisely, a statement must have a "side-effect" (i.e. be imperative) and an expression must have a value type (i.e. not the bottom type).

    The type of a statement is the unit type, but due to Halting theorem unit is fiction so lets say the bottom type.


    Void is not precisely the bottom type (it isn't the subtype of all possible types). It exists in languages that don't have a completely sound type system. That may sound like a snobbish statement, but completeness such as variance annotations are critical to writing extensible software.

    Let's see what Wikipedia has to say on this matter.

    https://en.wikipedia.org/wiki/Statement_(computer_science)

    In computer programming a statement is the smallest standalone element of an imperative programming language that expresses some action to be carried out.

    Many languages (e.g. C) make a distinction between statements and definitions, with a statement only containing executable code and a definition declaring an identifier, while an expression evaluates to a value only.

    0 讨论(0)
  • 2020-11-22 12:20
    • an expression is anything that yields a value: 2 + 2
    • a statement is one of the basic "blocks" of program execution.

    Note that in C, "=" is actually an operator, which does two things:

    • returns the value of the right hand subexpression.
    • copies the value of the right hand subexpression into the variable on the left hand side.

    Here's an extract from the ANSI C grammar. You can see that C doesn't have many different kinds of statements... the majority of statements in a program are expression statements, i.e. an expression with a semicolon at the end.

    statement
        : labeled_statement
        | compound_statement
        | expression_statement
        | selection_statement
        | iteration_statement
        | jump_statement
        ;
    
    expression_statement
        : ';'
        | expression ';'
        ;
    

    http://www.lysator.liu.se/c/ANSI-C-grammar-y.html

    0 讨论(0)
  • 2020-11-22 12:20

    Statements -> Instructions to follow sequentially
    Expressions -> Evaluation that returns a value

    Statements are basically like steps, or instructions in an algorithm, the result of the execution of a statement is the actualization of the instruction pointer (so-called in assembler)

    Expressions do not imply and execution order at first sight, their purpose is to evaluate and return a value. In the imperative programming languages the evaluation of an expression has an order, but it is just because of the imperative model, but it is not their essence.

    Examples of Statements:

    for
    goto
    return
    if
    

    (all of them imply the advance of the line (statement) of execution to another line)

    Example of expressions:

    2+2
    

    (it doesn't imply the idea of execution, but of the evaluation)

    0 讨论(0)
  • 2020-11-22 12:20

    A statement is a block of code that doesn't return anything and which is just a standalone unit of execution. For example-

    if(a>=0)
    printf("Hello Humen,I'm a statement");
    

    An expression, on the other hand, returns or evaluates a new value. For example -

     if(a>=0)
        return a+10;//This is an expression because it evalutes an new value;
    

    or

     a=10+y;//This is also an expression because it returns a new value. 
    
    0 讨论(0)
  • 2020-11-22 12:24

    Some things about expression based languages:


    Most important: Everything returns an value


    There is no difference between curly brackets and braces for delimiting code blocks and expressions, since everything is an expression. This doesn't prevent lexical scoping though: A local variable could be defined for the expression in which its definition is contained and all statements contained within that, for example.


    In an expression based language, everything returns a value. This can be a bit strange at first -- What does (FOR i = 1 TO 10 DO (print i)) return?

    Some simple examples:

    • (1) returns 1
    • (1 + 1) returns 2
    • (1 == 1) returns TRUE
    • (1 == 2) returns FALSE
    • (IF 1 == 1 THEN 10 ELSE 5) returns 10
    • (IF 1 == 2 THEN 10 ELSE 5) returns 5

    A couple more complex examples:

    • Some things, such as some function calls, don't really have a meaningful value to return (Things that only produce side effects?). Calling OpenADoor(), FlushTheToilet() or TwiddleYourThumbs() will return some sort of mundane value, such as OK, Done, or Success.
    • When multiple unlinked expressions are evaluated within one larger expression, the value of the last thing evaluated in the large expression becomes the value of the large expression. To take the example of (FOR i = 1 TO 10 DO (print i)), the value of the for loop is "10", it causes the (print i) expression to be evaluated 10 times, each time returning i as a string. The final time through returns 10, our final answer

    It often requires a slight change of mindset to get the most out of an expression based language, since the fact that everything is an expression makes it possible to 'inline' a lot of things

    As a quick example:

     FOR i = 1 to (IF MyString == "Hello, World!" THEN 10 ELSE 5) DO
     (
        LotsOfCode
     )
    

    is a perfectly valid replacement for the non expression-based

    IF MyString == "Hello, World!" THEN TempVar = 10 ELSE TempVar = 5 
    FOR i = 1 TO TempVar DO
    (    
        LotsOfCode  
    )
    

    In some cases, the layout that expression-based code permits feels much more natural to me

    Of course, this can lead to madness. As part of a hobby project in an expression-based scripting language called MaxScript, I managed to come up with this monster line

    IF FindSectionStart "rigidifiers" != 0 THEN FOR i = 1 TO (local rigidifier_array = (FOR i = (local NodeStart = FindsectionStart "rigidifiers" + 1) TO (FindSectionEnd(NodeStart) - 1) collect full_array[i])).count DO
    (
        LotsOfCode
    )
    
    0 讨论(0)
提交回复
热议问题