The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 10:10 AM
switch login sharif UNIX for Advanced & Expert Users 1 12-12-2007 04:03 AM
switch from csh to ksh veeracer Shell Programming and Scripting 8 11-05-2004 02:13 PM
switch from csh to ksh veeracer UNIX for Dummies Questions & Answers 1 11-03-2004 06:28 PM
can you switch neer45 Shell Programming and Scripting 3 12-05-2001 08:54 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-26-2005
abey abey is offline
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
  #2 (permalink)  
Old 09-26-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
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
  #3 (permalink)  
Old 09-26-2005
abey abey is offline
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.
  #4 (permalink)  
Old 09-26-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

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

vino
  #5 (permalink)  
Old 09-26-2005
abey abey is offline
Registered User
  
 

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

Once again thnx for the help...

abey
  #6 (permalink)  
Old 09-26-2005
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
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.
  #7 (permalink)  
Old 09-27-2005
sarwan sarwan is offline
Registered User
  
 

Join Date: Aug 2005
Posts: 54
use if......
if((a==1)||(a==2)||(a==3))
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:59 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0