简体   繁体   English

C++ 中的语句被认为是什么?

[英]What is considered a statement in C++?

My professor commonly asks my class how many statements there are in a given program, but I can't determine what he defines as a statement.我的教授经常问我的 class 给定程序中有多少条语句,但我无法确定他将什么定义为语句。 It seems as though an if/else is one statement, and a for loop is one statement regardless of if there are other supposed statements within it.似乎 if/else 是一个语句,而 for 循环是一个语句,无论其中是否有其他假定语句。 Are there any governing rules for this matter or is his definition of his own invention?是否有任何关于这件事的管理规则,或者是他对自己发明的定义?

Thanks谢谢

For a precise definition of a statement:对于语句的精确定义

Definition: A statement is a block of code that does something.定义:语句是做某事的代码块。 An assignment statement assigns a value to a variable.赋值语句为变量赋值。 A for statement performs a loop. for 语句执行循环。 In C, C++ and C# Statements can be grouped together as one statement using curly brackets在 C、C++ 和 C# 语句可以使用大括号组合为一个语句

{ statement1; {声明1; statement2;声明2; } }

As far as counting statements, I agree with the others, there's not much point.至于计数陈述,我同意其他人的观点,没有多大意义。 Counting Lines of Code (LOC) though, actually has some value and there's a lot of research that tries to relate the number of LOC to the workload of developers.不过,计算代码行数 (LOC) 实际上有一些价值,并且有很多研究试图将 LOC 的数量与开发人员的工作量联系起来。 It's possible that your instructor is having you count statements and thinking of statements as nothing more than a single LOC, which isn't quite the case.您的讲师可能会让您计算语句并将语句视为仅是一个 LOC,但情况并非如此。

Statements nest, ie smaller statements can be joined into larger statements, like compound statements.语句嵌套,即较小的语句可以连接成较大的语句,如复合语句。 For this reason, the question about "how many statements are there in this program" are ambiguous.出于这个原因,关于“这个程序中有多少条语句”的问题是模棱两可的。 One has to define the counting method first.首先必须定义计数方法。 Without it the question of "how many" makes little sense.没有它,“有多少”的问题就毫无意义。

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 (eg, expressions).语句将具有内部组件(例如,表达式)。

More at Statement (Computer Science) at Wikipedia .更多信息请参见 Wikipedia 上的声明(计算机科学)

Here is the function that handles statements parsing in JS alike language:这是处理以 JS 类似语言解析的语句的 function:

static void do_statement(CsCompiler *c )
{
    int tkn;
    switch (tkn = CsToken(c)) {

    case T_IF:          do_if(c);       break;
    case T_WHILE:       do_while(c);    break;
    case T_WITH:        do_with(c);     break;
    case T_DO:          do_dowhile(c);  break;
    case T_FOR:         do_for(c);      break;
    case T_BREAK:       do_break(c);    CsSaveToken(c,CsToken(c)); break;
    case T_CONTINUE:    do_continue(c); CsSaveToken(c,CsToken(c)); break;
    case T_SWITCH:      do_switch(c);   break;
    case T_CASE:        /*do_case(c);*/    CsParseError(c,"'case' outside of switch");  break;
    case T_DEFAULT:     /*do_default(c);*/ CsParseError(c,"'default' outside of switch");  break;
    case T_RETURN:      do_return(c);   break;
    case T_DELETE:      do_delete(c);   break;
    case T_TRY:         do_try(c);      break;
    case T_THROW:       do_throw(c);    break;
    case '{':           do_block(c, 0); break;
    case ';':           ;               break;
    default:  
      {
        CsSaveToken(c,tkn);
        do_expr(c);
        break;
      }
    }
}

As you see it includes things like for , while and also expressions (separated by ; )如您所见,它包括forwhile和表达式(由;分隔)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM