Replacing switch statements?

This is a discussion on Replacing switch statements? within the C Programming forums, part of the General Programming Boards category; Hello all, i have a sort of silly question to ask. My question is: "What would be the best way ...

  1. #1
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355

    Thumbs up Replacing switch statements?

    Hello all, i have a sort of silly question to ask.
    My question is:
    "What would be the best way to replace switch statements without using if?"

    Also, is there a way to break out of a multiple level nested conditional operator being used as an lvalue-less statement?
    Code:
    int a,b,c;  /* Just exemplary variables */
    a ? (b ? (c ? 1:0) : XXX ):0;   /*  Expression used for it's sideeffects */
    At the point where i assign the value XXX i want to break, or goto (if possible)
    The actual expression is much more complex, and would obfuscate things if i pasted it.
    It is forbidden to use 'if' or 'switch' as a solution.

    It is not for an assignment, i was just curious and thought it would make interesting code since then one could replace an entire switch in one and only statement.
    Thanks in advance for any try on this.
    Last edited by xuftugulus; 02-16-2008 at 11:26 AM.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,961
    "What would be the best way to replace switch statements without using if?"
    Why do you want to do this?

    Also, is there a way to break out of a multiple level nested conditional operator being used as an lvalue-less statement?
    Do not use nested conditional operators.

    It is forbidden to use 'if' or 'switch' as a solution.
    That is silly as it leads to code obfuscation.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    What would be the best way to replace switch statements without using if?
    *sigh* Stupid instructors and/or books...

    You could always take the switch and break it into a set of if chains and then find a way to replace the ifs. Think about what else checks a condition before doing a block of code.

  4. #4
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Quote Originally Posted by laserlight View Post
    Why do you want to do this?
    Actually i am writing some obfuscated code, and the switch statements are too obvious. I just thought i could do away with conditional operators managing the entire switch, but then again i could not control execution within conditional operators. Maybe i should use a function or something. I don't know if i can 'goto' from one function body to the other using just plain 'C', although i imagine that would be very bad for the stack frame
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Hm. I seem to recall replacing a nested switch in this. I think I used an FSM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Thanks Dave_Sinkula! That was very helpful indeed!
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Come to think of it -- or rather, digging it up -- it was actually nested loops.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Well, a function table can replace a switch. Just fill an array with function pointers with the appropriate array number.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by xuftugulus View Post
    Actually i am writing some obfuscated code, and the switch statements are too obvious.
    I don't really see the point in purposely writing obfuscated code, but you could always write it normally, compile it, then decompile it. Everything should be nice and obfuscated then.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using Files Inside Switch Statements
    By arealfind08 in forum C Programming
    Replies: 11
    Last Post: 03-17-2009, 04:49 PM
  2. Explanation of switch statements
    By ammochck21 in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2006, 01:59 PM
  3. arrays within switch statements
    By divinyl in forum C++ Programming
    Replies: 6
    Last Post: 07-17-2003, 01:56 PM
  4. help with switch statements
    By Wexy in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 04:44 PM
  5. Switch Statements
    By blackgingr in forum C Programming
    Replies: 3
    Last Post: 10-07-2002, 02:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21