PHP, C ++, etc. Syntax Explanation

Why most programming languages requireto have semicolonafter operators, but not after things like if elseifand else.

Do all compilers look like newlines? If this is true, then why won't they do this for all ? p>

Did I miss something? It really doesn't make sense to me ...

+5
source share
6 answers

Usually a semicolon is required because compilers ignore most spaces. This is not necessary after statements such as if, elseif, otherwise, because this is not the end of the statement. These are just complete statements followed by an instruction or block of statements.

+11
source

Because compilers for these languages ​​do not consider spaces to complete the statement. The statement must be terminated somehow and done with a semicolon. You can write all the code (although it would be a terrible, terrible idea) on one line if you complete the statements correctly.

+2
source

, if {} .

, python, .

+2

. , C, , . , if while, :

void foo() {
    if (bar) {
        /* ... */
    }
    while (baz) {
        /* ... */
    }
}

, foo() . , , bar() , ? C , , ( , , ;, ).

, :

void foo() {
    1 + 2
}

1 + 2 . :

void foo() {
    1 + 2;
}

, , , (- curlies like { foo(); } , if :

if ( <condition> ) <statement> (else <statement>)?

if : - .

+2

';' '\n', .

, ( - ). , ( ).

, , , ( ..). ( ), , , , , .: -)

, , , , ( ) ( , ).

, .
, , , .

+1

- . , . , . . ( , .) :

ParserUtils.RefreshProperty(m_cfg.PORTAL_ID, ParserUtils.CreateHashFromUrl(strDetailLinkUrl), Convert.ToInt32(m_cfg.Type), strPrice, strAddress, strStreet, strPostCode, strFeatures, strDescription, strImgFile, strBedrooms, strReception, strBath, strStatus, strLink, strPropType, strOutside, strTenure, strKeywords, strFullText, strContactInfo, m_ieBrowser.URL);

, , :

    ParserUtils.RefreshProperty(m_cfg.PORTAL_ID,
    ParserUtils.CreateHashFromUrl(strDetailLinkUrl),
    Convert.ToInt32(m_cfg.Type), strPrice,
    strAddress, strStreet, strPostCode, strFeatures, strDescription, strImgFile,
    strBedrooms, strReception, strBath, strStatus, strLink, strPropType,
    strOutside, strTenure, strKeywords, strFullText, strContactInfo,
    m_ieBrowser.URL);

, . If s, while for , . :

for (int i = 0; i < n; i++)
    {
        if (i % 2 == 0)
        {
            System.out.println("It can be divided by two");
        }
        {
            System.out.println("It can't be divided by two");
        }
    }

, :

for (int i = 0
     i < 0
     i++) { if (i % 2 == 0) { System.out.println("It can be divided by two")
                          } { System.out.println("It can't be divided by two")
                          } }

, . , () :

Buy food(Bread, Meat, Butter),
Go and pay the taxes,
Call your mother, because she wants to talk to you

These tasks are separated by commas, but note that the parameters are separated by commas. We must make the difference between a comma as a parameter separator and a comma as a task separator, because the computer is not as smart as people are. As an output, the task separator is a larger comma than the parameter separator. Therefore, the operator separator is a semicolon, and the parameter separator is a comma.

+1
source

All Articles