Thread: Switch function

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    7

    Switch function

    I was wondering if you can have two 'break' in one case with a switch function.
    For Example:
    switch(menu)
    case1:
    code
    break
    code
    break
    case2:

    Is this allowed in C.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, but that second line of code will likely be unreachable.
    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
    Registered User
    Join Date
    Apr 2008
    Posts
    83

    It works..

    Hi it works, but i am wondering where do you need such applications?
    its no of use.

    Regards,
    Shwetha

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    7
    basically im writing a program for a user calender operation, i wanted to use the 'break' function to end the case if the user entered an invalid date, for example.
    thanks for your help

  5. #5
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Quote Originally Posted by shwetha_siddu View Post
    Hi it works, but i am wondering where do you need such applications?
    There are at least two applications:

    The nasty one is performing initialization tasks for the next case, if you got there with the goto statement. When Rob Pike says that the goto statement is useful under certain circumstances, he certainly didn't have this technique in mind, so we better forget about it immediately.

    The other application is making two branches have the same size in the resulting binary in order to separate execution duration from code length. This is so even if I know how long the program took to execute a certain branch, I don't know which branch it was until I fully understood the code in all branches (i.e. no automatic algorithm which matches execution duration and code length). A similar technique involves if-statements where the conditional expression always evaluates to false (to increase code size) or code which only performs irrelevant computations (to increase execution duration). Both of these are a common technique in proprietary software to make it harder to understand the disassembled code (Microsoft's operating system binaries are full of this stuff).


    Along those lines, I once wrote a program for the DFKI (which probably means "German Research Center for Artificial Intelligence" in English) which was able to determine key events (keypress/keyrelease, and partly even the corresponding keys) by only examining CPU usage and exploiting several facts about the CPU's cache, the layout of the keymap and so on. Without the execution duration of all the branches in the corresponding key event interrupt service routine being equal, I can (as a regular user) make highly sophisticated guesses about other people's passwords, e.g. the admin password.


    This should tell you two things:
    - don't give me access to your computer
    - you don't need fake code if you're not working for the NSA

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM