What is the difference between this solution:
switch (value)
{
case 1:
{
// some code
} break;
}
When break stay after brackets
And this:
switch (value)
{
case 1:
{
// some code
break;
}
}
When break stay in brackets
A break makes the switch statement to end its execution. If you don't add a break to your case, the next case will start executing. Consider:
NSInteger myInt = 0;
switch (myInt) {
case 0:
NSLog("0");
case 1:
NSLog("1");
default:
NSLog("Default");
}
will print:
0
1
Default
because there are no breaks.
It doesn't matter if you wrap break into a block { break; } because the block changes the scope of variables declared inside of it but has no effect on the break itself.
There is no difference between { ... } break; and { ... break; } because in both versions break is the last statement that is executed and that's all that matters.
However,
case 0:
if (myCondition) {
break;
}
case 1:
...
would have a very different meaning. If myCondition is NO, then the next case (case 1:) is executed too (fall-through) because the break statement has not been executed.
Related
In swift switch statements, does the condition get called once or for each case? Which would be faster?
switch foo(param) {
case 0:
NSLog(0)
default:
NSLog("default")
}
or
let myNumber = foo(param)
switch myNumber {
case 0:
NSLog(0)
default:
NSLog("default")
}
switch evaluates its argument only once.
As Martin suggested in the comments, this example proves it
func foo(name: String) -> Int {
println(name)
return countElements(name)
}
switch foo("hello") {
case 0: println(0)
case 1: println(1)
default: println("default")
}
output
hello
default
In case it were evaluating for each case, you would expect
hello
hello
default
instead
I saw several questions on this problem, but none about iOS.
I have two enums like following :
// My first enum eColor
typedef enum
{
eColorRed = 1,
eColorGreen,
eColorBlue
} eColor;
// My second enum eShape
typedef enum
{
eShapeCircle = 1,
eShapeSquare,
eRectangle
} eShape;
I would like to have a method which accept both of these enums like this :
+ (NSString*) toStr:(bothEnum)e
{
NSString *result = nil;
if (bothEnum == eColor)
{
switch(e) {
case eColorRed:
result = #"red";
break;
case eColorGreen:
result = #"green";
break;
case eColorBlue:
result = #"blue";
break;
default:
result = #"unknown";
}
}
else if (bothEnum == eShape)
{
switch (e) {
case eShapeCircle:
result = #"circle";
break;
case eShapeSquare:
result = #"square";
break;
case eShapeRectangle:
result = #"rectangle";
break;
default:
result = #"unknown";
}
}
return result;
}
Is a thing like this possible ?
I don't want to have methods like colorToStr:, shapeToStr:, etc. My wish is to have only one method called toStr:, as above...
Enums are just constants and at run time they are just numbers. So your method doesn't know what is eColorRed, it knows it as 1, and your only option is to pass additional parameter, telling your method wether 1 passed in first argument is eColorRed or eShapeCircle. It can be just a string, like:
+ (NSString*) toStr:(NSUInteger)e fromEnumType:(NSString*)type
{
if([type isEqualToString:#"eColor"])
{
switch(e)
...
}
else if([type isEqualToString:#"eShape"])
{
switch(e)
...
}
}
You may try this approach: You make the second enum to start indexing from the last index of the first enum, then you just use single witch in your method. Remember enum type is just an int type indeed.
// My first enum
typedef enum
{
eColorRed = 1,
eColorGreen,
eColorBlue,
eColorLastIndex
} eColor;
// My second enum
typedef enum
{
eShapeCircle = eColorLastIndex,
eShapeSquare,
eShapeRectangle,
} eShape;
typedef int eTypes;
+(NSString*)toStr:(eTypes)e
{
NSString *result = nil;
switch(e) {
case eColorRed:
result = #"red";
break;
case eColorGreen:
result = #"green";
break;
case eColorBlue:
result = #"blue";
break;
case eShapeCircle:
result = #"circle";
break;
case eShapeSquare:
result = #"square";
break;
case eShapeRectangle:
result = #"rectangle";
break;
default:
result = #"unknown";
break;
}
return result;
}
Instead eGraphics you may use just int or eColorShape, whatever.
Update for 64-bit Change:
According to apple docs about 64-bit changes,
Enumerations Are Also Typed : In the LLVM compiler, enumerated types can
define the size of the enumeration. This means that some enumerated
types may also have a size that is larger than you expect. The
solution, as in all the other cases, is to make no assumptions about a
data type’s size. Instead, assign any enumerated values to a variable
with the proper data type
So you have to create enumeration with type as below syntax if you support for 64-bit.
typedef enum eColor : NSUInteger {
eColorRed = 1,
eColorGreen,
eColorBlue
} eColor;
then go with #user2260054's answer, otherwise, it will lead with below warning in 64-bit environment.
Otherwise, it will lead to warning as Implicit conversion loses integer precision: 'NSUInteger' (aka 'unsigned long') to eColor
This is not good for design point of view. Cohesion is broken over here. While programming OOP concept must be implemented for reuse of code and decoupling the objects as well.
I'm trying to find a matching object in a tree, so I'm using ObjC fast enumeration. The problem is my method finds the matching value, hits the return line, and then sets the value to nil and keeps on iterating. Here's my method:
+ (INONode *)findByUUID:(NSString*)uuid fromRootNode:(INONode*)node
{
for (INONode * childNode in [node children]) {
if ([[node uniqueID] isEqualToString:uuid]) {
break;
}
else {
[INONode findByUUID:uuid fromRootNode:childNode];
}
}
return node;
}
When I follow code execution by setting a breakpoint, the break is hit, then goes to the return line, then back up to the statement that continues the iteration. What am I missing here?
Since your method is recursive, the return is returning you to the else branch of the if in your loop and continuing the search.
The way it is currently implemented, your method will only ever return the node passed in to it. The only return statement is return node, and node is never modified.
Here is one way you could do it:
+ (INONode *)findByUUID:(NSString*)uuid fromRootNode:(INONode*)node
{
// Check the root node
if ([[node uniqueID] isEqualToString:uuid]) {
return node;
}
// Check each child
for (INONode * childNode in [node children]) {
node = [INONode findByUUID:uuid fromRootNode:childNode];
if (node) {
return node;
}
}
return nil;
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
For loop and if statement
Hi everyone,
i have a following for loop and preeceding to it a if else condition.m using the following code.
for (int intPrjName=0 ; intPrjName<[arrPrjName count] ;intPrjName++)
{
if ([strSelectedProjectName caseInsensitiveCompare:[arrPrjName objectAtIndex:intPrjName])
{
//some code
}
else
{
//some code
}
}
suppose strSelectedProjectName is "aaa"and the arrPrjName contains "aaa" "bbb" "ccc" .. after the first iteration of for loop if condition gets true i.e string "aaa" matches with the string in array list,it should get out of the loop at the second iteration,i.e it should not enter the else condition..
add a break; command
for (int intPrjName=0 ; intPrjName<[arrPrjName count] ;intPrjName++)
{
if ([strSelectedProjectName caseInsensitiveCompare:[arrPrjName objectAtIndex:intPrjName])
{
//some code
break;
}
else
{
//some code
}
}
Use break to exit the iteration loop.
Use the break keyword:
for (...) {
if (condition) {
// do stuff
break;
} else {
// do other stuff
}
}
You need to use a break; in your loop to break out of the code if you do not want it to continue to loop.
for (int intPrjName=0 ; intPrjName<[arrPrjName count] ;intPrjName++)
{
if ([strSelectedProjectName caseInsensitiveCompare:[arrPrjName objectAtIndex:intPrjName])
{
//some code
break;//Get out of for loop
}
else
{
//some code
}
}
For example,
In your program you have:
NSLog(#"Where are you going?");
NSLog(#" 1 = Location1, 2 = Location2");
printf("Make a selection:");
scanf("%i, &value);
switch (value) {
case 1:
NSLog(#"You are going to Location 1.")
break;
case 2:
NSLog(#"You are going to Location 2.");
break;
default:
NSLog(#"That is not a valid location");
break;
}
Normally after you input your integer your program will return 0 and the application ends. How do you go about having it "loop" back to the original printf to make a new selection. Or even better, a new printf IE 'printf("Where else would you like to go?:");'?
Why don't you keep it as a separate method and call it from itself when you want to loop. Just consider the following code,
void takeMeToPlaces() {
NSLog(#"Where are you going?");
NSLog(#"0 = Exit, 1 = Location1, 2 = Location2");
printf("Make a selection:");
scanf("%i, &value);
switch (value) {
case 0:
NSLog(#"You don't like to go anywhere");
break;
case 1:
NSLog(#"You are going to Location 1.");
takeMeToPlaces();
break;
case 2:
NSLog(#"You are going to Location 2.");
takeMeToPlaces();
break;
default:
NSLog(#"That is not a valid location");
takeMeToPlaces();
break;
}
}