views:

124

answers:

3

Hello, I have just a terminology question. I have read that if, foreach etc. are statements but what does it mean in the terminology - are these commands? It is maybe "lost in the translation" problem

+3  A: 

From Statement (programming)

In computer programming a statement can be thought of as the smallest standalone element of an imperative programming language. A program is formed by a sequence of one or more statements. A statement will have internal components (e.g., expressions).

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. A distinction can also be made between simple and compound statements; the latter may contain statements as components.

In a nutshell it is one of many instructions in the language that makes the program to perform some very basic action. Through combining statements you define a complex activity that makes sense to you as an author. You define it by using little building blocks namely those statements.

Developer Art
Thanks, also they are statements, so they are "commands" as they are executable
Bornemix
+2  A: 

A command and a statement is generally the same thing.

However, when talking about commands, it's usually something that a user enters directly rather than a statement in a program.

Also, while a command generally is an instruction to do something, a statement can also be instructions that doesn't actually perform anything, like for example declaring a constant value:

const int Answer = 42;
Guffa
That is not a statement, it is a declaration :)
leppie
@leppie: Good point. Let's use a declaration that is a statement as example instead. :)
Guffa
@Guffa: That is still primarily a declaration, the fact that the compiler 'magically' store and initialize the value is kinda irrelevant.
leppie
@leppie: So any expression that ends with a semicolon is a statement, *except* if it happens to declare something also? Or is it just when I write something that it isn't a statement? ;)
Guffa
@Guffa: Declarations end with a `;` too, but i'm getting tired. Go read the C# grammar, you might be surprised :)
leppie
A: 

A statement is an expression that has no return value (or void or unspecified).

leppie
Do you have any references for this?
Stefan Steinegger
Many statements actually have a return value, like `x = 42` for example.
Guffa
@Guffa: If you terminate any expression with a `;`, it becomes a statement.
leppie
@Stefan Steinegger: You can find this in the C# spec, grammar section. Pretty much goes for any 'procedural' language. (procedural is not quite correct, but I cant think of an easier description)
leppie