Is "if else" treated as a single statement?

Apologies if my wording is wrong.

I've been taught since high-school to add curly braces after statements such as if, while, for, etc. whenever there is more than one line of code to execute. Example:

while(i<12)
        i++;
        z = i+3;
        cout << "Answer is " << z << endl;

Will not execute what you'd expect. But the following will:

while(i<12){
        i++;
        z = i+3;
        cout << "Answer is " << z << endl;
}

However I recently came across a while loop that contains an if..else statement which seems like more than one line of code/statement when looked at, and yet it executes and works as it should regardless if it has curly braces for its scope or not.

while(current != NULL && !found)
          if(current->info >= newItem)
                found = true;
            else
            {
                trailCurrent = current;
                current = current->link;
            }

Why is that? Does it treat the if..else statement as a single statement?

if .. else is a single statement (technically it is called a selection statement) in C++. Per [stmt.select]/1 the valid forms of if are:

selection-statement:
    if constexpr-opt ( init-statement-opt condition ) statement
    if constexpr-opt ( init-statement-opt condition ) statement else statement

So

while(current != NULL && !found)
    if(current->info >= newItem)
        found = true;
    else
    {
        trailCurrent = current;
        current = current->link;
    }

is a single statement as well and behaves correctly.

How to Calculate the Mean Value, is the average of the numbers. It is easy to calculate: add up all the numbers, then divide by how many numbers there are. In other words it is the sum divided by the count. Is definition, 3rd person singular present indicative of be. See more.

The syntax for an if is basically:

if (condition) statement else statement

If you want to control multiple statements with either the if or the else leg, you use curly braces to group them together into a block, which is syntactically a single statement.

Likewise with while, for, and so on. Each of them controls a single statement, which can be either a simple statement or a compound statement (i.e., some number of statements grouped together into a block using curly braces).

while (condition) statement

do statement while (condition)

for (for-init condition ; statement) statement

For the moment, I'm sort of ignoring some of the more recent additions, such as defining a variable in the loop header, such as: for (int i...), since that's not really relevant to the questions at hand.

Origin, Is definition, 3rd person singular present indicative of be. See more. Define is. is synonyms, is pronunciation, is translation, English dictionary definition of is. v. Third person singular present indicative of be. vb a form of the present tense of be1 the internet domain name for Iceland abbreviation for Iceland v. Is - definition of is by The Free Dictionary.

From the C11 standard:

selection-statement:
    if (expression) statement
    if (expression) statement else statement
    switch (expression) statement

So yes, if-else is a single statement.

Think of it this way: If if-else weren't a single statement, what else would it be? Would the if-part be a statement and then the else-part another statement? If that were the case, you should be able to use else as a statement on its own (without a preceding if), but that would make no sense.

outhale defined, There is some difference of opinion regarding the use of apostrophes in the pluralization of references to letters as symbols. New Fowler's Modern English Usage,  Third-person singular simple present indicative form of be He is a doctor. 1999 January 8, Ken Starr, quoting Bill Clinton, Referral from Independent Counsel Kenneth W. Starr in Conformity with the Requirements of Title 28, United States Code, Section 595(c) (Starr Report)‎[1], Washington, D.C.: U.S. Government Printing Office, retrieved 14 February

Of course, they are single statement. You cannot write else without if. What will be if while loop with if-else loops only if case? Can you imagine that else will be executed after loop is finished?

Merriam-Webster, Define is. is synonyms, is pronunciation, is translation, English dictionary definition of is. v. Third person singular present indicative of be. vb a form of the present  .is, the Internet country-code top-level domain for Iceland. Information science, the study of data collection, manipulation, and dissemination. Information systems, organizations of data-processing persons, records, and activities. InstallShield, a software tool for creating software installers for Microsoft Windows.

Yes, the else part is part of the if statement. This is also the reason why you can write else if (...). The if in else if forms a second statement: the statement for the else part of the first if.

As others have pointed out in the comments already, relying on this can quickly lead to code that is hard to read, confusing, or even erroneous. For example:

if (a)
  if (b)
    ; // ...
else
  ; // this is the else for if (b), not if (a)!

Is, IS may refer to: Contents. 1 Arts and media. 1.1 Literature; 1.2 Other media. 2 Businesses and organisations; 3 Language; 4 Places; 5 Science and mathematics  It definition is - that one —used as subject or direct object or indirect object of a verb or object of a preposition usually in reference to a lifeless thing, a plant, a person or animal whose sex is unknown or disregarded, a group of individuals or things, or an abstract entity. How to use it in a sentence.

is, In knowledge representation, object-oriented programming and design is-a (is_a or is a) is a subsumption relationship between abstractions (e.g. types, classes)  Is vs. Are with Collective Nouns. A collective noun refers to a group of people or things that is treated as a single entity in speech. Committee is a collective noun. A committee is made up of multiple people, but the word itself is singular in form. In American English, collective nouns take is. In British English, collective nouns can take

Is, He's a political activist. His repertory is vast. And, during Germany's shutdown, he streamed more than fifty performances from home. It's made  Lue uutisia Suomesta ja maailmalta heti tuoreeltaan. IS seuraa uutistilannetta ympäri vuorokauden.

IS, is definition: 1. he/she/it form of be 2. he/she/it form of be 3. be, used with he/she/it​: . Learn more. Viihdeuutisia: VIP-kortti viihteen kulisseihin. Mitä kuuluu suomalaisille julkkiksille ja kovan luokan maailmantähdille.

Comments
  • Despite the poor coding practice, the code is correct.
  • Yes it is, otherwise it would not compile
  • The good habit is to always use braces after a while
  • Just use brackets everywhere without exception. It isn't too hard. You won't regret it.
  • I understand that it is poor coding practice (I always include braces) and that the code is correct, but I want to understand why it is correct? Why does it treat if..else differently than say 3 separate statements following the while loop?
  • Notice that it is semantically incorrect to consider the "if...else" syntax as one statement when the documentation you linked clearly states "if ... statement else statement" meaning that there are two statements involved. Minor nitpick, but important.
  • @TimKlein A statement is defined in part as attribute-specifier-seq-opt selection-statement so it is a statement. Just because it contains statements does not means it is not one.
  • @NathanOliver if … else … is a single statement, however, technically, it's a selection statement, not a compound statement
  • @MichaelKenzel Fixed. Not sure why I thought selection statement was listed under compound statement
  • @NathanOliver Thanks for the clarity and references.
  • Thanks for the detailed explanation. Following what you said above, am I correct in saying that if..else..if, is not a single statement? Since if..else by itself is a single statement and adding another if essentially makes it a compounding statement?
  • @user931018: Correct--in an if/the/else if chain, each if forms an independent statement (that happens to be controlled by the previous one).