whats the difference between a command and a statement
Asked Answered
A

6

6

Often when reading about Tcl (e.g. http://antirez.com/articoli/tclmisunderstood.html) you read that "everything is a command". Sometimes you also hear how other languages are, like Tcl, "command languages."

To me with my background in other languages, I just view these "commands" as statements. What precisely is the difference between a command and a statement?

Archibaldo answered 25/9, 2009 at 16:24 Comment(2)
Thanks everybody. PS...just got my copy of Ousterhout (and Jones') "Tcl and the Tk Toolkit" 2nd editionArchibaldo
Did you find the answer you were looking for in the responses?Arlyne
A
7

Traditionally, in Tcl, the phrase "everything is a command" means that there's no such thing as a "reserved word" command, or one that is defined by the system that you can't change. Every single executable piece of Tcl code is of the format:

command ?arg1? ... ?argN?

There's no such thing as a command that's part of the syntax and can't be overwritten (like "if" and other control structures in most languages). It's entirely possible to redefine the "if" command to do something slightly different.

For example, you could redefine "while" as:

proc while {expression body} {
    for {} {[uplevel 1 expr $expression]} {} {
        uplevel 1 $body
    }
}

The above code being untested... but it shows the general idea.

Arlyne answered 25/9, 2009 at 16:50 Comment(1)
I think I like this answer better than mine.Benign
B
3

A command is what other languages call a function, routine or reserved word, and can be defined by the "proc" command or in C or whatever. A statement is an invocation of a command. Using traditional definitions, a statement in Tcl is a command followed by zero or more arguments.

Consider the following code:

1   proc hello {string} {
2      puts "hello, $string"
3   }
4   hello "world"

Line 1 defines a command named "hello", line 4 is a statement that calls the "hello" command.

True enough, some articles on Tcl use the term "command" to mean both the name of a command and the invocation of the command. I wouldn't get too hung up on that. Think of statements as the invocation of a command and you'll be fine.

When you see the phrase "everything is a command", it means is that there are no reserved words. Things that look like language syntax or keywords -- if, return, exit, break, while and so on... -- are all commands. They can all be treated alike: they can be renamed, they can be removed, they can be re-implemented, etc.

Benign answered 25/9, 2009 at 16:36 Comment(2)
IMO a function call (with or without arguments) is a command. So, line 4 is both a statement and a command.Feoff
nitpick alert: else is not a command. It's an argument to ifPlasticizer
C
1

I'd say that a command is what you execute in a statement; different statements may execute the same command (with different arguments, typically). The command is the operation; a statement is a specific invocation of the operation.

Cooperstein answered 25/9, 2009 at 16:26 Comment(0)
C
1

I guess this is mainly a question of semantics, so there may be some variation in the understanding of these concepts. That said, this Wikipedia article provides some guidance that is in keeping with my intuition on the topic.

A statement is a unit of an imperative program. Specifically, it is the unit that is terminated by the statement terminator. In C that's a semi-colon. Or, in Pascal and its descendants, it's the unit that's separated by the statement separator. I think in most flavours of Pascal that's also a semi-colon.

A command is an action, such as a function call or a keyword that performs an action. The Wikipedia article likens them to verbs, and I think that's a good description.

So, a variable declaration is a statement, but probably not a command. And a variable assignment via an assignment operator might be considered a command by some and not by others. One sometimes hears it referred to as an assignment command, but not often. If it looks like a function call, as in TCL, then it's more 'command-like', since there's an explicit verb set.

Also, statements may consist of several commands. For example, think about several function calls in C joined with commas. I would consider that one statement, since it has one semi-colon and returns one value. However, each of the separate calls could be considered a command.

The other thing to bear in mind when considering the statement/command terminology is that statements typically refer to imperative programs, while commands typically refer to shells and scripts. So, when using a command shell like bash, one speaks of commands. If I write a bash script, then it usually thought of as a script of commands, rather than a program of statements, even though the difference is largely academic.

TCL, as one of the early scripting languages, probably wanted to draw a distinction between itself as an interpreted scripting language running in a shell (wish), versus compiled languages like C and Pascal. Perl, for example, having come to popularity somewhat later, typically doesn't harp on the distinction.

However, you still often hear people refer to Perl scripts, rather than Perl programs, and likewise for TCL.

I fear that my answer may have done nothing to clarify the distinction, but I hope it helps.

Conjoin answered 25/9, 2009 at 16:50 Comment(0)
N
0

I often wondered why the term 'statement' is used in programming since it does not seem to match with the meaning of the word in natural language, where a command is an imperative and a statement is not. Intuitively I would prefer the word command.

In Pascal, a variable declaration is not considered a statement, as Jeremy Bourque suggests (it may be true for other languages), since each programming block is divided in a declaration section with all declarations and the statement section with all statements. Statements are separated by semicolons, as Jeremy Bourque says above. I don't think it is possible to have more than one command in one statement (as in C, apparently). Except for compound statements of course, which have one or more sub-statements. So I guess that one could consider command and statement synonyms in Pascal.

However, in implementations of Pascal, the word command is often used for commands given to the compiler and development environment (IDE). It might be useful to have a different word for commands (statements) in the source code. Perhaps the people who developed Pascal and other early languages, considered a command as something which was to be executed immediately. Therefore Bourque's remarks about the difference between compiled and scripted languages sound logical to me.

Northbound answered 4/6, 2014 at 12:13 Comment(0)
E
-1

I would think that a command is an instruction in code, and a statement may run several commands but at the end evaluates to true or false.

Erfert answered 25/9, 2009 at 16:32 Comment(1)
What you say doesn't make sense in the context of the question, which is specifically asking about Tcl. Statements in Tcl don't necessarily evaluate to true or false.Benign

© 2022 - 2024 — McMap. All rights reserved.