What is statement , expression , block (instruction, expression and block) and what is the difference between them?
2 answers
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