Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Alternate for Case Code

Posted on 2003-11-13
9
233 Views
Last Modified: 2010-04-17
     Is it possible to replace the code between the braces with different code that is not a case statement.  I mean engineer a different way to do the same thing.  Possibly with a series of if statements or something such as "if B or b then x,  if V or v then y,  If Q or q then z."  If so what would the syntax be?  What code could replace this and plug into the same spot.  
{
                case 'B':
                case 'b':
                    filer.buildAFile();
                    break;
                case 'V':
                case 'v':
                    filer.viewAFile();
                    break;
                case 'Q':
                case 'q':
                    done = true;
                    break;
                default:
                    System.out.println("That is not a valid choice.");
                    break;
            }
0
Comment
Question by:rockyisin
9 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9745610
Hi rockyisin,
> {
>                 case 'B':
>                 case 'b':
>                     filer.buildAFile();
>                     break;
>                 case 'V':
>                 case 'v':
>                     filer.viewAFile();
>                     break;
>                 case 'Q':
>                 case 'q':
>                     done = true;
>                     break;
>                 default:
>                     System.out.println("That is not a valid choice.");
>                     break;
>             }

you did not specify the switch !!!!

I assume switch (a)

if ( a == 'B' || a == 'b' )
    filer.buildAFile();
else
    if ( a == 'V' || a == 'v' )
           filer.viewAFile();
    else
        if ( a == 'Q' || a == 'q' )
           done = true;
        else
            System.out.println("That is not a valid choice.");

this should give you an idea though sytax is Cish

Cheers!
Sunny:o)
0
 
LVL 9

Expert Comment

by:bhagyesht
ID: 9745978
I agree with whats sunny given
0
 
LVL 7

Expert Comment

by:Alpha Au
ID: 9746007
the syntax you used is java like syntax,

the vb syntax should be

select case Lcase(switchtext)
   case "b":
      filer.buildAFile();
   case "v":
      filer.viewAFile();
   case "q"
   else
      System.out.println("That is not a valid choice.");
end select

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 5

Expert Comment

by:g0rath
ID: 9747597
or...

if using C/C++
a = toupper(a);
Java String
a.toUpperCase()

if ( a == 'B')
    filer.buildAFile();
else
    if ( a == 'V')
           filer.viewAFile();
    else
        if ( a == 'Q')
           done = true;
        else
            // whatever your message
0
 

Author Comment

by:rockyisin
ID: 9751955
My question is:  Could I do what the Case statement does a different way.. maybe using a series of  "if" statements.  Can I cut out this case statement and replace it with a series of if statements?  What would this look like?
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 50 total points
ID: 9753054
>Could I do what the Case statement does a different way.. maybe using a series of  "if" statements
yes, you can do it
>Can I cut out this case statement and replace it with a series of if statements?  
Yes
>What would this look like?

if ( a == 'B' || a == 'b' )
    filer.buildAFile();
else
    if ( a == 'V' || a == 'v' )
           filer.viewAFile();
    else
        if ( a == 'Q' || a == 'q' )
           done = true;
        else
            System.out.println("That is not a valid choice.");
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
When i run adoquery my application freezes 26 178
convert Systemjs to Webpack 3 25
Programming Language for Wordpress 7 30
Create .bat File 16 24
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question