Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
October 19, 2017, 11:03:30 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: Select Case w/o Break  (Read 2099 times)
n1mie
Newbie
*
Posts: 4



« on: July 19, 2010, 07:21:22 PM »

I'm not sure I understand the explanation of what happens if you omit the "break" from a select case. You say it continues into the next case whenever that case is executed? You mean it goes into the wrong case? Can you give an example?
Logged

-- Chip (FN41bn)
n1mie
Newbie
*
Posts: 4



« Reply #1 on: July 19, 2010, 09:25:08 PM »

Hmm. OK. So if I understand this now it works like this. If I don't put a break in, the program evaluates every case segment. So it will perform all that match the current conditions. Generally since all have to be unique it means it will perform the one that matches AND the default one (if used). So the benefit is that you can have it always do the default not just when nothing else matches. But this could just as easily be accomplished by placing the desired code immediately following the select case. Does that summarize it?
Logged

-- Chip (FN41bn)
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #2 on: July 22, 2010, 05:15:55 PM »

Here's an example:

Code: (Objective-C)
int n = 1;

switch (n) {
  case 1:
        NSLog (@"n is 1");
  case 2:
        NSLog (@"n is 2");
}

Since n is set to 1, the first case in the switch will match and the NSLog statement will display n is 1.  However, since there's no break after the first NSLog, flow will drop into the next case, and the second NSLog statement will also be executed, even though n is not equal to 2.  Hope that clears it up.

Cheers,

Steve Kochan
« Last Edit: July 22, 2010, 05:18:32 PM by skochan » Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents ゥ 2009 classroomM.com. All rights reserved.