Resolved Switch Case

Discussion in 'iOS Programming' started by nickculbertson, Mar 11, 2011.

  1. nickculbertson, Mar 11, 2011
    Last edited: Mar 11, 2011

    nickculbertson macrumors regular

    nickculbertson

    Joined:
    Nov 19, 2010
    Location:
    Nashville, TN
    #1
    Hello,
    I think this should be an easy question for most but I'm having trouble finding the answer. I know how to switch between cases using rand and arcrand but how do I switch them in order 1,2,1,2,1,2,1,2...

    Code:
    // for random
    int Number = rand() % 2;
    switch (Number){
    		case 0:{
                             NSLog(@"one");}
    			break;
    		case 1:{
    			NSLog(@"two");}
    			break;
    		default:
    			break;
    	}	
    	
    }
    
    // for non random
    
    int Number = //How do I just make this = 2
    switch (Number){
    		case 0:{
                             NSLog(@"one");}
    			break;
    		case 1:{
    			NSLog(@"two");}
    			break;
    		default:
    			break;
    	}	
    	
    }
    
    
    thanks,
    Nick
     
  2. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #2
    I'd love to help, but I have absolutely no idea what you're trying to achieve.
     
  3. ulbador macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #3
    XOR it:


    Code:
    int Number = 1;
    Number = Number ^ 1; // i is now 0
    Number = Number ^ 1; // i is now 1
    Number = Number ^ 1; // i is now 0
    
     
  4. seepel macrumors 6502

    seepel

    Joined:
    Dec 22, 2009
    #4
    This sounds a lot better than my solution, but I'll post mine anyway, just in case someone wants to have a higher sequence, for example 0,1,2. And of course number_ is an iVar or global variable, something that is going to stick around.

    Code:
    #define SEQUENCE_COUNT 3
    number_++;
    switch(number_ % SEQUENCE_COUNT) {
        case 0:
            // Do some stuff
            break;
        case 1:
            // Do some stuff
            break;
        case 2:
            // Do some stuff
            break;
        default:
           // Shouldn't ever get here!
           break;
    }
    
     
  5. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #5
    In this case, NOT would be just fine, no?

    Code:
    int number = 0;
    number = !number; //1
    number = !number; //0
    number = !number; //1
    
    I know many of you don't like seeing seemingly boolean operators as integers...

    (EDIT: I was going to add something about mod, but seepel got to it first.)

    B
     
  6. ulbador macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #6
    I think using ! is probably better.

    You can look at that and know exactly what it's doing quickly, versus using XOR and having to think about your bitwise math. The modulo operation is definitely good too for the reasons mentioned (ability to two more than just 2 numbers).
     
  7. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #7
    And for completeness, if you need to oscillate between two values that don't happen to be 0 and 1 you can do it like this

    Code:
    #define kValue1 27 // or whatever
    #define kValue2 42  // or whatever
    
    // Setup
    int currentValue = kValue1;
    
    // Oscillate
    currentValue = (kValue1 + kValue2) - currentValue;
    Obviously that code also works with values of 0 and 1 but using a boolean and currentValue = ! currentValue is more idiomatic for that case.
     
  8. nickculbertson thread starter macrumors regular

    nickculbertson

    Joined:
    Nov 19, 2010
    Location:
    Nashville, TN
    #8
    Thanks for your help guys. This did the trick.

    Code:
    static int Number = 0;
    Number ^= 1;
    	
    	
    	switch (Number){
    
    Thanks,
    Nick
     

Share This Page