Point me in right directing for "if / else"

Discussion in 'iOS Programming' started by iphonequestions, Mar 24, 2010.

  1. iphonequestions macrumors newbie

    Joined:
    Mar 21, 2010
    #1
    Can someone tell me what is wrong with me code here, im getting an error "error: expected expression before 'else'" and im not sure where to go to fix this, the second one seems to be right but when i added a third line i got the error! Any help even documentation would help, thanks!


    I think im missing {} but not sure here to put it," } else { " seems to give me the error also



     
  2. SilentPanda Moderator emeritus

    SilentPanda

    Joined:
    Oct 8, 2002
    Location:
    The Bamboo Forest
    #2
    If need to do:

    if
    else if
    else if
    else if
    ....
    else if
    else

    You can't have two "else" statements in a row.

    You can either do:

    if

    -----------

    if
    else

    -----------

    if
    else if
    ..... (repeat as needed)
    else
     
  3. -aggie- macrumors P6

    -aggie-

    Joined:
    Jun 19, 2009
    Location:
    Where bunnies are welcome.
    #3
    The first "else" needs to be "else if".

    Edit: Looks like Panda beat me to it.
     
  4. iphonequestions thread starter macrumors newbie

    Joined:
    Mar 21, 2010
    #4

    Much thanks, ill give that a shot!

    ok i put that in and then i get the error:

    So i am missing }{ then?


    Code:
        // Configure the cell.
    	if (indexPath.row % 2) 
    		cell.textLabel.text = [NSString stringWithFormat:@"FirstVC", indexPath.row]; 
    
    	else if   
    		cell.textLabel.text = [NSString stringWithFormat:@"SecondVC", indexPath.row];
    	
    	else 
    		cell.textLabel.text = [NSString stringWithFormat:@"SecondVC", indexPath.row];
    	
    
    	return cell;
    }
    
     
  5. SilentPanda Moderator emeritus

    SilentPanda

    Joined:
    Oct 8, 2002
    Location:
    The Bamboo Forest
    #5
    You kinda have to finish the "else if"... else if what? Also as far as I can tell your else if and else statements do the same thing....
     
  6. -aggie- macrumors P6

    -aggie-

    Joined:
    Jun 19, 2009
    Location:
    Where bunnies are welcome.
    #6
    They do, but he's also missing a "{ ". If there's code above, he might want to include it.
     
  7. iphonequestions thread starter macrumors newbie

    Joined:
    Mar 21, 2010
    #7
    My apologies, this is right out of the App...
    Code:
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        
        static NSString *CellIdentifier = @"CellIdentifier";
        
        // Dequeue or create a cell of the appropriate type.
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        
        // Configure the cell.
    	if (indexPath.row % 2) 
    		cell.textLabel.text = [NSString stringWithFormat:@"FirstVC", indexPath.row]; 
    
    	else if   
    		cell.textLabel.text = [NSString stringWithFormat:@"SecondOneVC", indexPath.row];
    	
    	else 
    		cell.textLabel.text = [NSString stringWithFormat:@"SecondVC", indexPath.row];
    	
    	
    					
    	return cell;
    }
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    else if what? It needs a condition. As in: else if (condition).

    Also, the condition should return a boolean. indexPath.row % 2 doesn't.
     
  9. iphonequestions thread starter macrumors newbie

    Joined:
    Mar 21, 2010
    #9
    So what should that look like? I don't know, thats what im asking.. How do i fix this code so it compiles in my test project?
     
  10. -aggie- macrumors P6

    -aggie-

    Joined:
    Jun 19, 2009
    Location:
    Where bunnies are welcome.
    #10
    Looks like you can get rid of:

    Code:
    else if   
    cell.textLabel.text = [NSString stringWithFormat:@"SecondOneVC", indexPath.row];
    Edit: Just noticed you changed the SecondOneVC to SecondVC. Did you mean to do that?

    If the modulus is not zero, it's true. If zero, it's false. So, why wouldn't it work the way it is?
     
  11. iphonequestions thread starter macrumors newbie

    Joined:
    Mar 21, 2010
    #11
    no no, thats a view, ok i wrote it a bit silly, but still, there are 3 views i want to present in the TableView, FirstView - SecondOneView - SecondView.

    It was just the way i wrote it, sorry for any confusion..
     
  12. -aggie- macrumors P6

    -aggie-

    Joined:
    Jun 19, 2009
    Location:
    Where bunnies are welcome.
    #12
    Then you need some condition after the else if.
     
  13. iphonequestions thread starter macrumors newbie

    Joined:
    Mar 21, 2010
    #13
    can you point me where i would find a list of conditions?
     
  14. jhurley macrumors newbie

    Joined:
    Feb 2, 2010
    #14
    The only reason the code wouldn't work as is is because there are only two options for the whatever % 2... It's either 1 or 0. So yes it'll work as a condition in an if statement, but you can only have two different conditions. There will be if(! thing%2) .. else if (thing%2)
    and if you have an "else" it will never be run.
     
  15. SilentPanda Moderator emeritus

    SilentPanda

    Joined:
    Oct 8, 2002
    Location:
    The Bamboo Forest
    #15
    There is no list of conditions. You may want to refresh yourself on if/then/else statements.
     
  16. iphonequestions thread starter macrumors newbie

    Joined:
    Mar 21, 2010
    #16
    Your saying it will only run with 2 views? Surely that cant be right?! There are lots of Apps using loads more.... Or if i misunderstood you, should there be a "(! thing%2)" after the 'if else' so it will work? Your saying it will work without a condition if there are only two?

    I need three, maybe more, so how will i make this work?
     
  17. -aggie- macrumors P6

    -aggie-

    Joined:
    Jun 19, 2009
    Location:
    Where bunnies are welcome.
    #17
    Yeah, I see that now.
     
  18. SilentPanda Moderator emeritus

    SilentPanda

    Joined:
    Oct 8, 2002
    Location:
    The Bamboo Forest
    #18
    That assumes the else if condition is dependent on the same thing as the if condition.

    You could say:

    if (x % 2)
    else if (someRandomFunction())
    else
     
  19. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #19
    Step back from the real coding and go (re)learn the basics of Objective-C / C, especially conditional programming.
     
  20. jhurley macrumors newbie

    Joined:
    Feb 2, 2010
    #20
    No I'm saying your logic is wrong. You're trying to set up an if/else statement with more than two conditions, but the number of conditions you will get from something mod 2 is only 2.

    so you could do
    Code:
    if(thing%2) {
    //do 1
    }
    else {
    //do 0
     }
    
    or something more like
    Code:
    if(thing%3 == 1) {
    // do 1
    }
    else if(thing%3 == 2) {
    //do 2
    }
    else {
    //do 0
    }
    
    but logically the way you were doing it to begin with makes no sense.
     
  21. jhurley macrumors newbie

    Joined:
    Feb 2, 2010
    #21
    ah, good point.
     
  22. iphonequestions thread starter macrumors newbie

    Joined:
    Mar 21, 2010
    #22
    So,

    Code:
        // Configure the cell.
    	if (indexPath.row % 3) 
    	 cell.textLabel.text = [NSString stringWithFormat:@"FirstVC", indexPath.row]; 
    
    	 else if (indexPath.row % 2)   
    		cell.textLabel.text = [NSString stringWithFormat:@"SecondOneVC", indexPath.row];
    	
    	 else  
    		cell.textLabel.text = [NSString stringWithFormat:@"SecondVC", indexPath.row];
    	
    	
    					
    	return cell;
    }

    Cool, that seems to be working... kinda
     
  23. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #23
    Step one: Use a calculation other than (something % 2). As posted above, any number % 2 will always return one of the value 0 or 1. There are no other outcomes possible. So if you want 3 possible outcomes, 0, 1, or 2, then your calculation must be (something % 3).

    Step two: If you want 3 (or more) distinct branches, then a chain of if/else if/.../else is usually a poor way to do it. In C and Objective-C, the switch statement performs a multi-way branch based on a single input value.

    http://en.wikipedia.org/wiki/Switch_statement#C.2C_C.2B.2B.2C_Java.2C_php.2C_ActionScript

    If the Wikipedia example is too unclear, then you should refer to a C or Objective-C language reference manual or tutorial (a book or an online webpage), and learn how to use switch from that.

    The way you're doing it now, it seems that your desire to use 3 or more views has exceeded your skills in the language. No problem, really, it happens all the time when learning. Just realize that you're going to have to learn something new if you want to accomplish this task.
     
  24. potaco macrumors regular

    Joined:
    Mar 12, 2010
    #24
    You need to rethink your logic regarding when to display which view. I don't think you fully understand how the % operator works.

    Based on this thread, you may want to take some other users' advice and spend a few days (re)learning programming concepts... these are pretty basic mistakes that are completely due to not understanding the fundamental concepts.

    edit: To elaborate a little more... you want to change your conditions to something like:

    Code:
        // Configure the cell.
    	if (indexPath.row % 3 == 2) 
    	 cell.textLabel.text = [NSString stringWithFormat:@"FirstVC", indexPath.row]; 
    
    	 else if (indexPath.row % 3 == 1)   
    		cell.textLabel.text = [NSString stringWithFormat:@"SecondOneVC", indexPath.row];
    	
    	 else  
    		cell.textLabel.text = [NSString stringWithFormat:@"SecondVC", indexPath.row];
    					
    	return cell;
    }
    which uses FirstVC if the row is 2, 5, 8, 11, etc... SecondOneVC if the row is 1, 4, 7, 10, etc... SecondVC if the row is 3, 6, 9, 12, etc. Somehow I don't think this is what you're trying to accomplish.
     
  25. iphonequestions thread starter macrumors newbie

    Joined:
    Mar 21, 2010
    #25
    No not quite, i mean what do i do if i want to show 20 views?
     

Share This Page