What is statement , expression , block (instruction, expression and block) and what is the difference between them?

    2 answers 2

    An expression is a piece of code that results in a quantity. The instruction is a code fragment that changes the state of the environment in which the program is running.

    I took this definition from a javascript book ( http://eloquentjavascript.net/ ), I think it works in the context of any language. But still curious: how it is exhaustive.

    From the definition follows:

    • sum = 2 + 3; - instruction containing an expression
    • var sum; - just an instruction without expression
    • 2 + 3; - just an expression, but expressions without instructions are meaningless (except when debugging) because with the result you need to do something / store it somewhere.

    Block - a set of several instructions.


      Expression ( Eng. Expression ) - an instruction that assigns the right side of the expression to the left side (expresses the left side through the right side).

      The instruction ( eng. Statement ) is in programming, a programming language command that specifies a certain step in the processing of information on a computer.

      1. Any expression is an instruction.
      2. Not every instruction is an expression.

      Code example:

      a = 1; Π²Ρ‹Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ int a = 10; инструкция ΠΈ Π²Ρ‹Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ (сначала [var a] β€” инструкция которая Π½Π΅ являСтся Π²Ρ‹Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ΠΌ Ρ‚.ΠΊ. присваивания Π½Π΅Ρ‚, Π° ΠΏΠΎΡ‚ΠΎΠΌ ΡƒΠΆΠ΅ [a = 10] β€” Π²Ρ‹Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ ΠΌΠΎΠΆΠ½ΠΎ Ρ‚Π°ΠΊΠΆΠ΅ Π½Π°Π·Π²Π°Ρ‚ΡŒ инструкциСй). if (a < 2){} else{} ΠΈ Ρ‚.Π΄. β€” инструкция, Π½ΠΎ Π½Π΅ Π²Ρ‹Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅, ΠΈΠ±ΠΎ Π½ΠΈΡ‡Π΅Π³ΠΎ Π½Π΅ выраТаСтся, Π½ΠΎ дСйствия происходят. Π’ΠΎΡ‡Π½ΠΎ Ρ‚Π°ΠΊΠΆΠ΅ ΠΈ с w = x+y+z : 1. x+y+z // инструкция, Π½ΠΎ Π½Π΅ Π²Ρ‹Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅. 2. w = //Ссли Π²Ρ‹ Π²Π²Π΅Π΄Π΅Ρ‚Π΅ сюда ΠΊΠΎΠ΄ Ρ‡Ρ‚ΠΎ Π²Π΅Ρ€Π½ΡƒΠ»Π° инструкция (x+y+z) β€” Π²Ρ‹Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ ΠΈ инструкция присваивания ΠΎΠ΄Π½ΠΎΠ²Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎ 

      A block (English Block) is 0 or more instructions placed between curly braces and can be placed in the place where the code is allowed. For example:

        class BlockDemo { public static void main(String[] args) { boolean condition = true; if (condition) { // Π½Π°Ρ‡Π°Π»ΠΎ Π±Π»ΠΎΠΊΠ° 1 System.out.println("Condition is true."); } // ΠΊΠΎΠ½Π΅Ρ† Π±Π»ΠΎΠΊΠ° 1 else { // Π½Π°Ρ‡Π°Π»ΠΎ Π±Π»ΠΎΠΊΠ° 1 System.out.println("Condition is false."); } // ΠΊΠΎΠ½Π΅Ρ† Π±Π»ΠΎΠΊΠ° 2 } } 

      References to sources:
      https://docs.oracle.com/javase/tutorial/java/nutsandbolts/expressions.html http://javascript.ru/forum/misc/25447-raznica-mezhdu-vyrazheniyami-i-instrukciyami.html

      • About the expression is not true. About Statement, however, too. It seems that you are confusing them in the example code. - Nofate ♦
      • @Nofate, you can edit if something is wrong, the answer is open. I myself wanted to understand this topic, wrote a response based on the information on the forums, so the information may be inaccurate. - Andreew Akulov