473,804 Members | 3,311 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3339
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.u k> 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.u k> 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
19680
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 INTO table1 tb1 USING table2 tb2 WHEN MACTHED THEN UPDATE SET tb1.col1 = tb2.col1 WHEN NOT MACTHED then do nothing
25
3920
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
4063
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 beautiful! void sw(int s) { switch (s) while (0) { case 0: printf("zero\n");
55
3735
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 that can make code faster than fast. thank you.
2
1303
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 did it with one of my 2 buttons, (but I don't know how) one of the buttons (cmdClear) decided on its own to be the default button - instead of the cmdSearchMember button - so much so that even after searching and finding 2 other solutions: 1)...
14
6260
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; while (1) { res = 0; if ((i = msgrcv (msqid, &rq_resa, SZ_MsgSrcResa, pid(), 0) == -1) { aff_erreurs ("msgrcv", "Error when recieving message : %d", errno);
26
10226
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
31447
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 condition is met, should I use break or return to stop the loop? e.g. is something like this ok? Would using return do the same thing? var dow = ;
7
4206
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 destroying html tags which wordwrap has a tendency of doing! )) What I want to do is add that line break so that it DOES force a line wrap - but I am not sure where to insert it in the function
0
9708
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9588
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10327
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4302
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2999
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.