developer tip

"else if"는 단일 키워드입니까?

copycodes 2020. 8. 21. 07:56
반응형

"else if"는 단일 키워드입니까?


저는 C ++를 처음 사용합니다. 나는 종종 아래와 같은 조건문을 본다.

if 
  statement_0;
else if
  statement_1;

질문:

구문 적else if 으로는 단일 키워드로 취급해야 합니까? 아니면 실제로 아래와 같이 if외부에 중첩 된 else입니까?

if 
  statement_0;
else 
  if
    statement_1;

우리가에 가면 그들은 하나의 키워드 아닌 초안 C ++ 표준 섹션 2.12 키워드 테이블 4목록을 모두 ifelse별도로 더이없는 else if키워드가. 우리는 C ++의 더 접근 목록을 찾을 수있는 키워드를 이동하여 키워드 cppreferences 섹션 .

섹션의 문법 6.4은이를 명확하게합니다.

selection-statement:
 if ( condition ) statement
 if ( condition ) statement else statement

if의는 else ifA는 다음 else용어. 이 섹션은 또한 다음과 같이 말합니다.

[...] 선택 문의 하위 ( if 문의 else 형식의 각 하위 문)은 암시 적으로 블록 범위 (3.3)를 정의합니다. 선택 문의 하위 문이 복합 문이 아니라 단일 명령문 이면 원래 하위 문을 포함하는 복합 문으로 다시 작성된 것과 같습니다.

다음 예제를 제공합니다.

if (x)
 int i;

can be equivalently rewritten as

if (x) {  
  int i;
}

그렇다면 약간 확장 된 예제는 어떻게 구문 분석됩니까?

if 
  statement_0;
else 
  if
    statement_1;
  else
    if
      statement_2 ;

다음과 같이 구문 분석됩니다.

if 
{
  statement_0;
}
else
{ 
    if
    {
      statement_1;
    }
    else
    {
        if
        {
         statement_2 ;
        }
    }
}

노트

We can also determine that else if can not be one keyword by realizing that keywords are identifiers and we can see from the grammar for an identifier in my answer to Can you start a class name with a numeric digit? that spaces are not allowed in identifiers and so therefore else if can not be a single keyword but must be two separate keywords.


Syntactically, it's not a single keyword; keywords cannot contain white space. Logically, when writing lists of else if, it's probably better if you see it as a single keyword, and write:

if ( c1 ) {
    //  ...
} else if ( c2 ) {
    //  ...
} else if ( c3 ) {
    //  ...
} else if ( c4 ) {
    //  ...
} // ...

The compiler literally sees this as:

if ( c1 ) {
    //  ...
} else {
    if ( c2 ) {
        //  ...
    } else {
        if ( c3 ) {
            //  ...
        } else {
            if ( c4 ) {
                //  ...
            } // ...
        }
    }
}

but both forms come out to the same thing, and the first is far more readable.


No, it is not.
They are two keywords and, moreover, the second "if" is a substatement "inside" the scope determined by the first "else" statement.


You can see the scope by using curly braces:

if(X) {
  statement_0;
}
else {
  if(Y) {
    statement_1;
  }  
}

And normally implemented with two distinct keywords, one is if and one is else.


As already answered, it isn't. They are two keywords. It's start of two statements one following each one other. To try make it a bit more clear, here's the BNF gramar which deal with if and else statements in C++ language.

 statement:      
    labeled-statement
    attribute-specifier-seqopt expression-statement
    attribute-specifier-seqopt compound-statement    
    attribute-specifier-seqopt selection-statement  
    attribute-specifier-seqopt iteration-statement    
    attribute-specifier-seqopt jump-statement  
    declaration-statement
    attribute-specifier-seqopt try-block

   selection-statement: 
         if ( condition ) statement
     if ( condition ) statement else statement

Note that statement itself include selection-statement. So, combinations like:

if (cond1)
   stat
else if(cond2)
   stat
else
   stat

are possible and valid according to C++ standard/semantics.

Note: C++ grammar take from this page.


else and if are two different C++ keywords. An if statement can be followed by an optional else if...else statement. An if statement can have zero or more else if's and they must come before the else.

You can find syntax and example in this if...else statement tutorial


I would just like to add my point of view to all these explanations. As I see it, if you can use these keywords separately, they must be TWO keywords. Maybe you can have a look at c++ grammar, from this link in stackoverflow: Is there a standard C++ grammar?

Regards


An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions using single if...else if statement.

When using if , else if , else statements there are few points to keep in mind.

An if can have zero or one else's and it must come after any else if's.

An if can have zero to many else if's and they must come before the else.

Once an else if succeeds, none of he remaining else if's or else's will be tested.

have a look if...else statement tutorial.

참고URL : https://stackoverflow.com/questions/24373076/is-else-if-a-single-keyword

반응형