Shoddy Weather
Shoddy Weather

Reputation: 139

Why did the creators of C decide to support one statement while loops without braces?

I am reading The C Programming Language (K&R) and noticed C allows the use of while loops preceding a single statement to function without any braces; why did the creators of C decide to support this? I presume this introduces some extra complexity for the compiler, is the desire for single statement while loops so common (for readability, perhaps?) it was worth whatever trade-off was required to allow them?

Upvotes: 1

Views: 66

Answers (1)

hobbs
hobbs

Reputation: 240010

It doesn't add any special complexity to the compiler, and it's not just while loops. All of the control structures (if, for, while, etc.) govern a "statement", where a block is just a special case of a statement (called a "compound-statement") containing 0 or more declarations or statements. There isn't any specific use case or rationale for applying this rule to while, but none is really needed, other than maybe simplicity or consistency.

Upvotes: 5

Related Questions