473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Omitting the break; in the default: section of a switch()



Is there any need to keep the final break in a switch which uses a default
at the end? I.e:
switch ($data) {

case 'foo':
# Action
break;

case 'bar':
# Action
break;

default:
# Action
break; # Is there any reason to keep this line?
}

I'm trying to aim for best practice levels of coding. (e.g. using ''
rather than "", etc. etc).

As far as I can see there seems no reason to do so and it improves
readability of code through and is (ultra-marginally) more efficient.

Martin Lucas-Smith www.geog.cam.ac.uk/~mvl22
www.lucas-smith.co.uk

Senior Computing Technician (Web Technician)
Department of Geography, University of Cambridge (01223 3)33390

& Webmaster, SPRI
Scott Polar Research Institute, University of Cambridge
Jul 16 '05 #1
5 3314
Martin Lucas-Smith écrivit:


Is there any need to keep the final break in a switch which uses a
default at the end?


Of course not. breaks are even not mandatory in the non-final cases. It
just depends on what you are aiming to.
--
P'tit Marcel
Jul 16 '05 #2

Is there any need to keep the final break in a switch which uses a
default at the end?


Of course not. breaks are even not mandatory in the non-final cases. It
just depends on what you are aiming to.


I realise that breaks are not mandatory (though if not using them that
should be documented), but it seems to be me that there are never any
circumstances where putting a break in the default: section can make any
difference. Would others agree?

After all, the default is the last in the list, so the break automatically
happens if it's got that far down.
Martin Lucas-Smith www.geog.cam.ac.uk/~mvl22
www.lucas-smith.co.uk

Senior Computing Technician (Web Technician)
Department of Geography, University of Cambridge (01223 3)33390

& Webmaster, SPRI
Scott Polar Research Institute, University of Cambridge
Jul 16 '05 #3
Martin Lucas-Smith <mv***@cam.ac.uk> wrote:
Is there any need to keep the final break in a switch which uses a
default at the end? I.e:
switch ($data) {

case 'foo':
# Action
break;

case 'bar':
# Action
break;

default:
# Action
break; # Is there any reason to keep this line?
}


No reason to have it in "default" whatsoever.

See example 4
http://uk.php.net/manual/en/control-...res.switch.php

JOn
Jul 16 '05 #4
On Fri, 18 Jul 2003 13:17:47 +0000, Jon Kraft wrote:
Martin Lucas-Smith <mv***@cam.ac.uk> wrote:
Is there any need to keep the final break in a switch which uses a
default at the end? I.e:
switch ($data) {

case 'foo':
# Action
break;

case 'bar':
# Action
break;

default:
# Action
break; # Is there any reason to keep this line?
}
}

No reason to have it in "default" whatsoever.

See example 4
http://uk.php.net/manual/en/control-...res.switch.php

JOn


Not entirely true. (But it is true *in this case* (no pun intended))

The default does not have to be the last item! In such a situation you may
want to have a break.

for example...

switch ($foo) {
case "bar":
default:
echo "Your momma";
break;
case "bing":
echo "I like cheese";
break;
}

Both the default and case "bar" take the same action and there is a break
after default. Also, default is first.
--
Jeffrey D. Silverman | jeffrey AT jhu DOT edu
Johns Hopkins University | Baltimore, MD
Website | http://www.wse.jhu.edu/newtnotes/

Jul 16 '05 #5


Martin Lucas-Smith wrote:

Is there any need to keep the final break in a switch which uses a default
at the end? I.e:

switch ($data) {

case 'foo':
# Action
break;

case 'bar':
# Action
break;

default:
# Action
break; # Is there any reason to keep this line?
}

I'm trying to aim for best practice levels of coding. (e.g. using ''
rather than "", etc. etc).

As far as I can see there seems no reason to do so and it improves
readability of code through and is (ultra-marginally) more efficient.

There's no programmatic reason for it. It is sometimes recommended as a
maintenance feature, so that if for some reason the cases are reordered
it won't be mistakenly left out. While it's common to have the default
case come last in the sequence, there isn't any requirement for it
either.

Brian Rodenborn
Jul 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: nomula_s | last post by:
Can you write a MERGE statement omitting one of the WHEN MATCHED and WHE NOT MACTHED statements. In my program I have to update when macthed and not do any thing when not matched. EX: MERGE...
25
by: chunhui_true | last post by:
In <<expert c>>I know the break in if wich is scoped in switch is break the switch,like: switch c case 1: if(b){ break; } ...... But like this: while(a){
7
by: Colin King | last post by:
Amusingly, one can use a while(0) statement to allow one to perform a switch statement without breaks. The while (0) enables the continue statements to break out of the switch. Ugly and...
55
by: Ennixo | last post by:
hi, do you know where i can find some ebooks or websites talking about C# optimisation ? for exemple, i just learned that ++i is faster than i++. i would like to know more about the things...
2
by: jonefer | last post by:
This is quite serious - for a beginner... No property setting for a default button? I just want a button on a search page to automatically be pressed when I hit the return/enter key. Somehow I...
14
by: serrand | last post by:
Could someone tell me a beautiful way to exit from a switch and a loop in one statement ... without using a goto... and if possible without using an auxiliary variable as i did... int res;...
26
by: Alexander Korsunsky | last post by:
Hi! I have some code that looks similar to this: -------------------------------------------- char array = "abcdefghij"; for (int i = 0; i < 10; i++) {
3
by: Yansky | last post by:
Hi, I've looked through the tutorial on w3cschools.com, but I'm still uncertain as to the difference between using break and using return. If I have a simple "for" loop that I want to stop if a...
7
by: jeddiki | last post by:
Hi, I am using a function called htmlwrap() which states that it does NOT add a "<br>" to the 70 character line so that it forces a line wrap. ( the script safely wraps long words without...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.