expected statement before else

Discussion in 'iOS Programming' started by ljg93, Mar 13, 2011.

  1. ljg93 macrumors member

    Joined:
    Mar 13, 2011
    #1
    the error i get is expected statement before else

    Code:
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    	if(section == 0)
    		return @"Ways to contact us";
    	else
    		return @"Other Settings";
    	else 
    		return @"Our Website";
    	
    

    whats wrong?
     
  2. itickings macrumors 6502a

    itickings

    Joined:
    Apr 14, 2007
    #2
    Well, you have one if and two else, that doesn't work. Strangely enough that is exactly what the error tells you as well.

    Did you intend something like this?
    Code:
    	if(section == 0)
    		return @"Ways to contact us";
    	else if(section == 1)
    		return @"Other Settings";
    	else 
    		return @"Our Website";
    
    You probably will want to check out the switch statement in that case anyway.
     
  3. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #3
    You have two else's in a row.

    The condition evaluated by if is either true or false. There is no third outcome possible, so adding a second else is meaningless.

    If you intend a series of if(x)/else if(y)/else, then you have to write it that way:
    Code:
    if ( some condition )
    {  some result;  }
    else if ( other condition )
    {  other result;  }
    else if ( yet another condition )
    {  yet another result;  }
    else
    {  result when none of the other if/else's matched;  }
    
     
  4. ljg93 thread starter macrumors member

    Joined:
    Mar 13, 2011
    #4
    thanks guys that solved it! sorry im new at this! appreciate the help
     
  5. forum user macrumors regular

    Joined:
    Aug 28, 2008
    #5
    You might want to look at switch. If/then is fine when you have a choice between two things. When there are multiple choices switch is easier to maintain:

    Code:
    switch section {
    case 0: bar;
    break;
    case 1: foo;
    break;
    default:
    foobar;
    break;
    }
    - Olaf
     

Share This Page