Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the c# compiler requires the break statement in switch construction?

I'm having hard time understanding, why the compiler requires using break statement. It's not possible to miss it since the fall through is now allowed. I see the reason for the break in C or C++, but is it needed here.

Why it's not a built-in behavior to break after a case is over ? Isn't it just a syntax with no semantic?

Sorry, if it's a stupid question.

EDIT: The fall through is allowed only when the case is empty. When there is a statement there you can't omit the break statement. So, it's a different matter.

like image 487
anthares Avatar asked Mar 02 '10 13:03

anthares


People also ask

Why the name C was given?

Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C". C is about the tone C.

Why do we use C?

C is highly portable and is used for scripting system applications which form a major part of Windows, UNIX, and Linux operating system. C is a general-purpose programming language and can efficiently work on enterprise applications, games, graphics, and applications requiring calculations, etc.

Why is C code called C?

C is a general purpose computer programming language developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system. It was named 'C' because many of its features were derived from an earlier language called 'B'.

Why C is so popular language?

The C programming language is so popular because it is known as the mother of all programming languages. This language is widely flexible to use memory management. C is the best option for system level programming language.


1 Answers

The compiler doesn't so much 'need' the break statements, it demands them.

This was a design decision. It keeps the code semantically close to C and C++ while eliminating the pitfalls of fall-through that was always a debatable 'feature' of the C languages.

like image 191
Henk Holterman Avatar answered Oct 21 '22 19:10

Henk Holterman