Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Using if statement with switch/case

    Is it possible to add an if statement inside of switch case? Below is a small piece of a pretty long switch statement that has about 12 different cases. But I only want it to change/switch cases if certain conditions are met for each particular case.

    Can I just add an if statement within the case?

    PHP Code:
    switch($current_state){
        case 
    'forcetrade'$next_state 'forceplace';
                            break;
        case 
    'forceplace'$next_state 'attacking';
                            break;


  • #2
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    Yep, you can do all sorts of fun stuff...

    Code:
    switch($somevar) {
    	case "option1":
    	case "option2":
    		//this will run for options 1 and 2
    		if($something == 2) {
    			//do for 2
    		}
    		else {
    			//do for not 2
    		}
    		break;
    	case "option3":
    		//option 3
    		break;
    	default:
    		//everything else, this is a catch all
    }
    I think you can even do things like <4 for your cases as well, but I've never tried it.

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Thanks. Then if I understand correctly, I think the below code is correct?
    PHP Code:
    case 'initial'
                if (
    $game_capital == Yes){
                    
    $next_state 'capital';
                } else {
                    
    $next_state 'inactive';
                }
                    break;
    case 
    'capital'$next_state 'inactive'
    I ask because I haven't gotten my $next_state variable to work correctly. If the above is correct, than I likely need to amend the code somewhere else. Sort of a trial and error on my part to implement some new stuff.

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Strings (such as your string 'Yes') must be enclosed in quotes. If you are trying to set a boolean value, use TRUE or 1.

  • #5
    New to the CF scene
    Join Date
    Feb 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This is a question I'd like to know the answer to aswell. I understand what you're trying to do.

    If your $xxxstatexxx changes are sequential then you can just run the switch without breaks, and so the switch begins to run the code at certain points depending on the condition. I get that... no problem.... BUT what if you want to skip the next case go to another one x cases down the line..... That's what I want to know. That would be useful.

  • #6
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    If you forget a break (on purpose or not), then the next case statements will not be evaluated. It will automatically fall through and execute the code. Otherwise the break statement wouldn't be necessary.

    BUT what if you want to skip the next case go to another one x cases down the line..... That's what I want to know. That would be useful.
    You can't skip X cases from within a case.

    It sounds to me like you need a different setup. Consider using a for/while statement with a switch to achieve what you need. It depends on your situation though.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •