switch-case statement without break
The break
acts like a goto
command. Or, as a better example, it is like when using return
in a void
function. Since it is at the end, it makes no difference whether it is there or not. Although, I do like to include it.
is switch case without break a bad practice?
It depends on what you want to do. If you get to the case NA
, without the break
keyword, the value_0
case will be executed after one of the two if
branches is finished. So if that's the behaviour you want, it's OK not to use break
, but I don't think that's what you wanted to do.
I suggest you simply move the if - else
statement above the switch
and remove the NA
case. This way, you will first assign the correct data to property_A
and then you can do whatever you want with it in the switch
.
EDIT: As Jack Deeth points out, if you omitted the break
statement on purpose, it's a good idea to add a comment that you did so.
TypeScript enum switch statement without break
A switch
statement runs all the code from the first match. It doesn't check the other cases when it finds a match. For example if you pass a
it will log 1
, 2
and 3
. If you pass b
. It will return 2
and 3
. It will keep going and only omit the code that goes before your match. The break
tells your switch statement that it can stop there. It works similarly to a return
.
It's not really like an if-statement
it's more like:
if (SomeEnum.a) {
... run this code and all other code after it.
}
if (SomeEnum.b) {
... run this code and all other code after it
}
...
switch statement without break
Fallthrough was an intentional design feature for allowing code like:
switch ($command) {
case "exit":
case "quit":
quit();
break;
case "reset":
stop();
case "start":
start();
break;
}
It's designed so that execution runs down from case to case.
default
is a case like any other, except that jumping there happens if no other case was triggered. It is not by any means a "do this after running the actual selected case" instruction. In your example, you could consider:
switch($param) {
case "created":
if(!($value instanceof \DateTime))
throw new \Exception("\DateTime expected, ".gettype($value)." given for self::$param");
break;
case "Creator":
if(!($value instanceof \Base\User)) {
throw new \Exception(get_class($value)." given. \Base\User expected for self::\$Creator");
}
break;
}
$this->$param = $value;
The rule of thumb here is, if it doesn't depend on the switch, move it out of the switch.
Can I use a switch statement without break;?
A solution is to extract the switch into a method an use its return value:
public bool EvaluateSwitch(int exitValidation)
{
switch (exitValidation)
{
case 'y':
case 'Y': return false;
case 'n':
case 'N': Console.WriteLine("\nPlease Enter Valid values");
return false;
default: return true;
}
}
Is a switch executing all the cases without stopping?
The switch statements jump to the right value, and continue up to the end of other cases.
If you like to exit the switch statement you have to use a break (or return in some situations).
This is useful to handle situations in wich many values can be handled at the same manner:
switch (x) {
case 0:
case 1:
case 2:
System.out.println("X is smaller than 3");
break;
case 3:
System.out.println("X is 3");
case 4:
System.out.println("X is 3 or 4");
break;
}
If the case selection is also a final condition for a method you can return from it.
public String checkX(int x) {
switch (x) {
case 0:
case 1:
case 2:
return "X is smaller than 3";
case 3:
return "X is 3";
case 4:
return ("X is necessary 4");
default:
return null;
}
}
}
Related Topics
Eigen How to Concatenate Matrix Along a Specific Dimension
Why Does Enumwindows Return More Windows Than I Expected
Warning C4003 and Errors C2589 and C2059 On: X = Std::Numeric_Limits<Int>::Max();
Print Template Typename at Compile Time
How to Do a C++ Style Compile-Time Assertion to Determine MAChine's Endianness
Can Member Functions Be Used to Initialize Member Variables in an Initialization List
Opencv Read Jpeg Image from Buffer
What Is the Size of a Pointer? What Exactly Does It Depend On
Best Way to Start a Thread as a Member of a C++ Class
Dropping Privileges in C++ on Windows
Converting Cv::Mat for Tesseract
Is Rebasing Dlls (Or Providing an Appropriate Default Load Address) Worth the Trouble
Lambdas Require Capturing 'This' to Call Static Member Function
How to Create a Type List (For Variadic Templates) That Contains N-Times the Same Type
Is It Valid for a Lambda To, Essentially, Close Over Itself