Thread: Replacing switch statements?

  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 12:26 PM.
    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
    28,413
    "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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,005
    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,005
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.

  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, 02: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, 05:44 PM
  5. Switch Statements
    By blackgingr in forum C Programming
    Replies: 3
    Last Post: 10-07-2002, 02:36 PM