Thread: Simplifying Code

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    6

    Simplifying Code

    Sorry to post again today, I tried searching the forums for a similar issue but couldn't find a thread..

    Right now I'm learning the switch case (lesson 5 ) and instead of repeatedly using case statements like this:

    Code:
    switch(example)
    { 
    case 1:
    
    break;
    
    case 2:
    
    break;
    
    case 3:
    
    break;
    
    
    ...etc
    
    }
    I'm trying to use a loop for simplifying this code, but it's not working out well..
    My initial question is, can we even use loops with case?

    This is an attempt:

    Code:
    printf("Enter a number less than 10:");
    scanf("%d\n", &tester);
    
    if(tester >= 10)
    {
        printf("Learn to follow instructions!");
    }
    
    else
    {
        switch(tester)
         {
            for (j=0;j<10;j++) //error 1
              {
                    case j: //error 2
                    printf("This number is a +%d reference from 0", j);
                    break;
              }
    
         }
    }
    it's not the best style and its probably sloppy(like I dont even have a default case), but i'm just trying to learn the concept for simplifying the code

    I get 2 errors:
    1. unreachable code at beginning of switch statement
    2. case label does not reduce to an integer constant

    any suggestions would be nice
    Last edited by Myst1caL; 09-08-2007 at 03:19 PM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Why do you have the switch at all?
    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.*

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    6
    well it's not really necessary in this case but i'm just trying to learn it

    I made my own example (pretty much any example) just so I can use it

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    From here:
    An important thing to note about the switch statement is that the case values may only be constant integral expressions. Sadly, it isn't legal to use case like this:
    If it's showing that something won't work, don't copy it and expect it to work.

    [edit]And I have no idea what you expect a loop to help with.
    [edit=2]Oh, wait. I think I see. No you can't do that.
    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.*

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    6
    Quote Originally Posted by Dave_Sinkula View Post
    From here:
    If it's showing that something won't work, don't copy it and expect it to work.

    [edit]And I have no idea what you expect a loop to help with.
    [edit=2]Oh, wait. I think I see. No you can't do that.
    so if I ever needed case, I can never use a loop to run through cases?

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    You can use a switch in a loop, yes. You can't create a "loop" of "cases".

    What you are trying to invent, methinks, is an if statement.

    [edit]That is, what I think you are implying by this:
    Code:
          switch ( var )
          {
             for ( i = 1; i < 5; ++i )
             {
                case i:
                   foo();
                   break;
             }
          }
    Is implemented like this.
    Code:
          if ( var >= 1 && var <= 5 )
          {
             foo();
          }
    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.*

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    6
    Quote Originally Posted by Dave_Sinkula View Post
    You can use a switch in a loop, yes. You can't create a "loop" of "cases".

    What you are trying to invent, methinks, is an if statement.

    [edit]That is, what I think you are implying by this:
    Code:
          switch ( var )
          {
             for ( i = 1; i < 5; ++i )
             {
                case i:
                   foo();
                   break;
             }
          }
    Is implemented like this.
    Code:
          if ( var >= 1 && var <= 5 )
          {
             foo();
          }
    yeah, that makes things much easier then, thanks! ^^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM