none
What is the difference between a statement and an expression? Im so confused now

    Question

  • My book talks little of this, and google, and msdn gives me all kinds of mixed answers.

    Can anyone make this clear and simple

    Which one of these is correct:

    if (expression)
    {
         statement;
    }
    
    
    //or is it this way?//
    
    
    if (statement)
    {
         expression;
    }

    Friday, May 4, 2012 2:48 PM

Answers

  • Expressions produce a result, which may or may not be used. 1+2 is an expression, just as it is in mathematics. Statement is short for expression statement, when some kind of expression exists within the statement. In C#, every statement ends with a semicolon. Each statement is, therefore, a complete line of code which may or may not include one or more expressions. For example, int i;  is a statement, not an expression statement as it contains no expressions - it is a simple declaration.

    This may not be the most elaborate answer nor the most correct in the minds of those who know more programming history than me, but I would say it is accurate as far as C# is concerned.


    James Finch (MCDST) -- Please vote as helpful if you found this post helpful, or mark as answer if it answered your question.

    Friday, May 4, 2012 3:12 PM

All replies

  • Friday, May 4, 2012 2:56 PM
  • Expressions produce a result, which may or may not be used. 1+2 is an expression, just as it is in mathematics. Statement is short for expression statement, when some kind of expression exists within the statement. In C#, every statement ends with a semicolon. Each statement is, therefore, a complete line of code which may or may not include one or more expressions. For example, int i;  is a statement, not an expression statement as it contains no expressions - it is a simple declaration.

    This may not be the most elaborate answer nor the most correct in the minds of those who know more programming history than me, but I would say it is accurate as far as C# is concerned.


    James Finch (MCDST) -- Please vote as helpful if you found this post helpful, or mark as answer if it answered your question.

    Friday, May 4, 2012 3:12 PM
  • Thank you so much. I can't say how happy I get when people take the time to explain things for free!

    I think I got it now.

    Friday, May 4, 2012 4:15 PM