What are the alternatives to switch-case statements in C?2019 Community Moderator ElectionWhat is the difference between #include <filename> and #include “filename”?Why was the switch statement designed to need a break?With arrays, why is it the case that a[5] == 5[a]?What does “static” mean in C?What is the effect of extern “C” in C++?What is the difference between const int*, const int * const, and int const *?Improve INSERT-per-second performance of SQLite?What does the C ??!??! operator do?What is “:-!!” in C code?stdlib.h not working as intended with atoi() function

What is the tangent at a sharp point on a curve?

10 year ban after applying for a UK student visa

When did hardware antialiasing start being available?

UK Tourist Visa- Enquiry

label a part of commutative diagram

Symbolism of 18 Journeyers

Does the Shadow Magic sorcerer's Eyes of the Dark feature work on all Darkness spells or just his/her own?

What is the difference between something being completely legal and being completely decriminalized?

Why is there so much iron?

Unfrosted light bulb

Animating wave motion in water

Why are there no stars visible in cislunar space?

Hackerrank All Women's Codesprint 2019: Name the Product

Knife as defense against stray dogs

Why is this tree refusing to shed its dead leaves?

Jem'Hadar, something strange about their life expectancy

What is 管理しきれず?

How do you justify more code being written by following clean code practices?

Gauss brackets with double vertical lines

Did Nintendo change its mind about 68000 SNES?

What is it called when someone votes for an option that's not their first choice?

is this saw blade faulty?

What will the Frenchman say?

Weird lines in Microsoft Word



What are the alternatives to switch-case statements in C?



2019 Community Moderator ElectionWhat is the difference between #include <filename> and #include “filename”?Why was the switch statement designed to need a break?With arrays, why is it the case that a[5] == 5[a]?What does “static” mean in C?What is the effect of extern “C” in C++?What is the difference between const int*, const int * const, and int const *?Improve INSERT-per-second performance of SQLite?What does the C ??!??! operator do?What is “:-!!” in C code?stdlib.h not working as intended with atoi() function










-2















Here's my code below:



while ( ( c = getopt( argc, argv, "s:E:b:t:vh" ) ) != -1 )

switch ( c )

case 's':
params.s = atoi( optarg );
break;
case 'E':
params.E = atoi( optarg );
break;
case 'b':
params.b = atoi( optarg );
break;
case 't':
trace_file = optarg;
break;
case 'v':
verbosity = 1;
break;











share|improve this question



















  • 12





    Why do you need an alternative? What's wrong with the code you posted?

    – UnholySheep
    Mar 6 at 22:30






  • 3





    You could use a series of if else statements or you could lookup a set of values in a table.

    – MoDJ
    Mar 6 at 22:35






  • 1





    You could use an array of function pointers indexed by the character. But there's really no reason to use anything other than the switch statement.

    – user3386109
    Mar 6 at 22:37






  • 1





    Again, you need to state what is wrong with your current code and what you are trying to accomplish.

    – Michael Dorgan
    Mar 6 at 22:50















-2















Here's my code below:



while ( ( c = getopt( argc, argv, "s:E:b:t:vh" ) ) != -1 )

switch ( c )

case 's':
params.s = atoi( optarg );
break;
case 'E':
params.E = atoi( optarg );
break;
case 'b':
params.b = atoi( optarg );
break;
case 't':
trace_file = optarg;
break;
case 'v':
verbosity = 1;
break;











share|improve this question



















  • 12





    Why do you need an alternative? What's wrong with the code you posted?

    – UnholySheep
    Mar 6 at 22:30






  • 3





    You could use a series of if else statements or you could lookup a set of values in a table.

    – MoDJ
    Mar 6 at 22:35






  • 1





    You could use an array of function pointers indexed by the character. But there's really no reason to use anything other than the switch statement.

    – user3386109
    Mar 6 at 22:37






  • 1





    Again, you need to state what is wrong with your current code and what you are trying to accomplish.

    – Michael Dorgan
    Mar 6 at 22:50













-2












-2








-2








Here's my code below:



while ( ( c = getopt( argc, argv, "s:E:b:t:vh" ) ) != -1 )

switch ( c )

case 's':
params.s = atoi( optarg );
break;
case 'E':
params.E = atoi( optarg );
break;
case 'b':
params.b = atoi( optarg );
break;
case 't':
trace_file = optarg;
break;
case 'v':
verbosity = 1;
break;











share|improve this question
















Here's my code below:



while ( ( c = getopt( argc, argv, "s:E:b:t:vh" ) ) != -1 )

switch ( c )

case 's':
params.s = atoi( optarg );
break;
case 'E':
params.E = atoi( optarg );
break;
case 'b':
params.b = atoi( optarg );
break;
case 't':
trace_file = optarg;
break;
case 'v':
verbosity = 1;
break;








c






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 6 at 22:31









Blastfurnace

14k54259




14k54259










asked Mar 6 at 22:27









Swarnava ChatterjeeSwarnava Chatterjee

1




1







  • 12





    Why do you need an alternative? What's wrong with the code you posted?

    – UnholySheep
    Mar 6 at 22:30






  • 3





    You could use a series of if else statements or you could lookup a set of values in a table.

    – MoDJ
    Mar 6 at 22:35






  • 1





    You could use an array of function pointers indexed by the character. But there's really no reason to use anything other than the switch statement.

    – user3386109
    Mar 6 at 22:37






  • 1





    Again, you need to state what is wrong with your current code and what you are trying to accomplish.

    – Michael Dorgan
    Mar 6 at 22:50












  • 12





    Why do you need an alternative? What's wrong with the code you posted?

    – UnholySheep
    Mar 6 at 22:30






  • 3





    You could use a series of if else statements or you could lookup a set of values in a table.

    – MoDJ
    Mar 6 at 22:35






  • 1





    You could use an array of function pointers indexed by the character. But there's really no reason to use anything other than the switch statement.

    – user3386109
    Mar 6 at 22:37






  • 1





    Again, you need to state what is wrong with your current code and what you are trying to accomplish.

    – Michael Dorgan
    Mar 6 at 22:50







12




12





Why do you need an alternative? What's wrong with the code you posted?

– UnholySheep
Mar 6 at 22:30





Why do you need an alternative? What's wrong with the code you posted?

– UnholySheep
Mar 6 at 22:30




3




3





You could use a series of if else statements or you could lookup a set of values in a table.

– MoDJ
Mar 6 at 22:35





You could use a series of if else statements or you could lookup a set of values in a table.

– MoDJ
Mar 6 at 22:35




1




1





You could use an array of function pointers indexed by the character. But there's really no reason to use anything other than the switch statement.

– user3386109
Mar 6 at 22:37





You could use an array of function pointers indexed by the character. But there's really no reason to use anything other than the switch statement.

– user3386109
Mar 6 at 22:37




1




1





Again, you need to state what is wrong with your current code and what you are trying to accomplish.

– Michael Dorgan
Mar 6 at 22:50





Again, you need to state what is wrong with your current code and what you are trying to accomplish.

– Michael Dorgan
Mar 6 at 22:50












1 Answer
1






active

oldest

votes


















0














But to answer the question, you could use a bunch of if/else statements:



while ( ( c = getopt( argc, argv, "s:E:b:t:vh" ) ) != -1 )

if ( c == 's' )

params.s = atoi( optarg );

else if ( c == 'E' )

params.E = atoi( optarg );

else if ( c == 'b' )

params.b = atoi( optarg );

else if ( c == 't' )

trace_file = optarg;

else if ( c == 'v' )

verbosity = 1;

else

// always have some kind of default clause







share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55033177%2fwhat-are-the-alternatives-to-switch-case-statements-in-c%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    But to answer the question, you could use a bunch of if/else statements:



    while ( ( c = getopt( argc, argv, "s:E:b:t:vh" ) ) != -1 )

    if ( c == 's' )

    params.s = atoi( optarg );

    else if ( c == 'E' )

    params.E = atoi( optarg );

    else if ( c == 'b' )

    params.b = atoi( optarg );

    else if ( c == 't' )

    trace_file = optarg;

    else if ( c == 'v' )

    verbosity = 1;

    else

    // always have some kind of default clause







    share|improve this answer



























      0














      But to answer the question, you could use a bunch of if/else statements:



      while ( ( c = getopt( argc, argv, "s:E:b:t:vh" ) ) != -1 )

      if ( c == 's' )

      params.s = atoi( optarg );

      else if ( c == 'E' )

      params.E = atoi( optarg );

      else if ( c == 'b' )

      params.b = atoi( optarg );

      else if ( c == 't' )

      trace_file = optarg;

      else if ( c == 'v' )

      verbosity = 1;

      else

      // always have some kind of default clause







      share|improve this answer

























        0












        0








        0







        But to answer the question, you could use a bunch of if/else statements:



        while ( ( c = getopt( argc, argv, "s:E:b:t:vh" ) ) != -1 )

        if ( c == 's' )

        params.s = atoi( optarg );

        else if ( c == 'E' )

        params.E = atoi( optarg );

        else if ( c == 'b' )

        params.b = atoi( optarg );

        else if ( c == 't' )

        trace_file = optarg;

        else if ( c == 'v' )

        verbosity = 1;

        else

        // always have some kind of default clause







        share|improve this answer













        But to answer the question, you could use a bunch of if/else statements:



        while ( ( c = getopt( argc, argv, "s:E:b:t:vh" ) ) != -1 )

        if ( c == 's' )

        params.s = atoi( optarg );

        else if ( c == 'E' )

        params.E = atoi( optarg );

        else if ( c == 'b' )

        params.b = atoi( optarg );

        else if ( c == 't' )

        trace_file = optarg;

        else if ( c == 'v' )

        verbosity = 1;

        else

        // always have some kind of default clause








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 6 at 23:12









        binkybinky

        45227




        45227





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55033177%2fwhat-are-the-alternatives-to-switch-case-statements-in-c%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

            Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

            Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved