The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Switch + stirng DNAx86 Shell Programming and Scripting 5 01-09-2008 07:10 AM
switch login sharif UNIX for Advanced & Expert Users 1 12-12-2007 01:03 AM
switch from csh to ksh veeracer Shell Programming and Scripting 8 11-05-2004 11:13 AM
switch from csh to ksh veeracer UNIX for Dummies Questions & Answers 1 11-03-2004 03:28 PM
can you switch neer45 Shell Programming and Scripting 3 12-05-2001 05:54 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 09-25-2005
Registered User
 

Join Date: Sep 2005
Posts: 27
Switch

using switch can we match for more than one values..

eg:

switcha(a)
{
case 1, 2, 3: printf("ddd");
break;
case 4, 5, 6: printf("mmm");
break;
}

In this case wat i found was only for the last value, i.e 3 and 6 the switch works.

I'd greateful if someone can help me in solving this out(using switch or any other alternative).


abey
Reply With Quote
Forum Sponsor
  #2  
Old 09-25-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,683
Quote:
Originally Posted by abey
using switch can we match for more than one values..

eg:

switcha(a)
{
case 1, 2, 3: printf("ddd");
break;
case 4, 5, 6: printf("mmm");
break;
}

In this case wat i found was only for the last value, i.e 3 and 6 the switch works.

I'd greateful if someone can help me in solving this out(using switch or any other alternative).


abey
Code:
{
case 1:
case 2:
case 3: printf("ddd"); break ;
case 4:
case 5:
case 6: printf("mmm"); break ;
}
This should work.

vino
Reply With Quote
  #3  
Old 09-25-2005
Registered User
 

Join Date: Sep 2005
Posts: 27
thnx, tht works....

is there anyother option to implement this apart from switch, that wil reduce the number of lines.
Reply With Quote
  #4  
Old 09-25-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,683
You can go ahead with the if-else-if combo.

vino
Reply With Quote
  #5  
Old 09-25-2005
Registered User
 

Join Date: Sep 2005
Posts: 27
yup, i'l try tht too...

Once again thnx for the help...

abey
Reply With Quote
  #6  
Old 09-26-2005
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,326
Quote:
Originally Posted by abey
thnx, tht works....

is there anyother option to implement this apart from switch, that wil reduce the number of lines.
Actually that's what you use switch for, it is much easier to read/debug switch statements than the if-else-if-else... combo. It also makes it easier to extend the program if you want to check for more conditions - you just have to add case statements as required instead of entire if-else blocks.

If you have more than two conditions to check for, you are best off using switch.
Reply With Quote
  #7  
Old 09-27-2005
Registered User
 

Join Date: Aug 2005
Posts: 45
use if......
if((a==1)||(a==2)||(a==3))
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:38 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0