Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 5th, 2006, 7:40 PM   #1
NightShade01
Programmer
 
Join Date: Oct 2005
Posts: 52
Rep Power: 3 NightShade01 is on a distinguished road
switch statement

just a quick question about switch statements. is it true that you always have to use break; at the end of each case in a switch statement? Someone was trying to explain a way to me where you don't but every text book i've read says you must.
NightShade01 is offline   Reply With Quote
Old Nov 5th, 2006, 7:42 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Only if you don't want to execute the next case. That's most of the time.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 5th, 2006, 11:23 PM   #3
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 381
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
And a short example :
int n ;

switch(n){
   case 0:
   case 1:
   case 2: System.out.println ( "the number is <= 2"); break;
   case 3:
   case 4:
   case 5: System.out.println ( "the number is >=3 and <= 5"); break; 
   default :  System.out.println ( "the number is grater than 5"); break;
}
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Nov 5th, 2006, 11:31 PM   #4
crawforddavid2006
Expert Programmer
 
crawforddavid2006's Avatar
 
Join Date: Apr 2005
Location: Not sure yet
Posts: 579
Rep Power: 0 crawforddavid2006 is an unknown quantity at this point
Send a message via AIM to crawforddavid2006 Send a message via MSN to crawforddavid2006
just to let you know xavier... that won't do anything since n is not defined :p

now if you did something like this
int n = 0;
while(n < 3)
{
switch(n)
{
    case 0: System.out.println("0"); break;
    case 1: System.out.println("1"); break;
    case 2: System.out.println("2"); break;
}
n++;
}

it would work
__________________
Quote:
Originally Posted by DaWei View Post
Well, it's better than Pen Islands url....;)

crawforddavid2006 is offline   Reply With Quote
Old Nov 6th, 2006, 1:39 AM   #5
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 381
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
Quote:
Originally Posted by crawforddavid2006 View Post
just to let you know xavier... that won't do anything since n is not defined :p
Yeah, you'r right, but the focus was the switch statement. I guess he cuold try the code a few times and initialize n to different values. I'm sure he would have realized that :p
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Jan 11th, 2007, 2:40 AM   #6
Mixmaster
Newbie
 
Mixmaster's Avatar
 
Join Date: Sep 2006
Posts: 8
Rep Power: 0 Mixmaster is on a distinguished road
I thought you had to use break all the time as well. Im glad you asked that
Mixmaster is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with a switch Varrickmana C++ 11 May 22nd, 2006 6:34 PM
Trouble with a swith statement cjaime C 10 Nov 8th, 2005 11:09 AM
New Switch, FTP Problems ViOLATiON Coder's Corner Lounge 6 Sep 13th, 2005 1:44 PM
Multiple IF statements, perhaps? foxcity911 PHP 7 Jun 22nd, 2005 3:33 PM
Switch statement Hellion C++ 5 Apr 11th, 2005 7:51 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:20 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC