Is a compound statement always 1 block?

Patrik Nusszer :

The if compound statement is 1 block. A compound statement consists of 0 or more statements

Now if you have

if (expression) {
    // do something
}
else {
    // do something else
}

Is it one compound statement or two? 1 compound statement = 1 block? Just because the 2 blocks seem to belong together.

Alan Sereb :

A compound statement consists of none or more statements enclosed within a set of braces: {}.

Source: https://www-numi.fnal.gov/offline_software/srt_public_context/WebDocs/Companion/glossary/compound_statement.html)

It means that these are two compound statements because there are two sets of curle braces. The statement above could be represented just like this:

if (expression) {
    // do something
}

if (!expression) {
    // do something else
}

EDIT: In java some commands allow you to skip curly braces if there is only a single command to be executed (like while or if). For example:

if (expression) doSomething();
else doSomethingElse();

These will count as a double compound statement as well because it is treated by compiler same as the above.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=161580&siteId=1