objective-c switch case with multiple arguments - iphone

Probably a basic question but I would like to reduce some code using multiple arguments on switch case statements. Possible? Correct syntax?
switch (myInteger){
case (1): //here I would like to apply multiple arguments as case (1 || 3 || 5)
<#statements#>
break;
case (2):
<#statements#>
break;
default:
break;

You can use multiple cases right below each other.
switch (myInteger) {
case 1:
case 3:
case 5:
// statements
break;
case 2:
// statements
break;
default:
// statements
break;
}

case 1:
case 3:
case 5:
statements;
break;
case 2:
statements;
break;
default:
break;

For Swift 3 there is a modification that I would like to mention
switch some value to consider {
case 1: //single argument
print("ABC")
case 2,3: // multiple arguments
print("KLM")
default:
print("XYZ")
}
Hope it helps you. Thanks

Switch case must declare inside the main method
SYNTAX
Switch (variable r expression)
{
Case 1 :
Body ;
Break
Case 2 :
Body;
Break;
Default :
Body ;
Break;
}

Related

Switch case with multiple values for the same case

I would like to know the syntax to set a multiple case statement in a switch / case.
For example :
String commentMark(int mark) {
switch (mark) {
case 0 : // Enter this block if mark == 0
return "Well that's bad" ;
case 1, 2, 3 : // Enter this block if mark == 1 or mark == 2 or mark == 3
return "Gods what happend" ;
// etc.
default :
return "At least you tried" ;
}
}
I cannot find the right syntax to set multiple case (the line case 1, 2, 3 :), is it even possible in Dart ?
I did not found any informations on pub.dev documentation, neither on dart.dev.
I tried :
case 1, 2, 3
case (1, 2, 3)
case (1 ; 2 ; 3)
case (1 : 2 : 3)
case 1 : 3
and more !
Execution continues until it reaches a break;. Therefore, you can list cases one after the other to get the following code execute on either one of those cases.
String commentMark(int mark) {
switch (mark) {
case 0 : // Enter this block if mark == 0
return "mark is 0" ;
case 1:
case 2:
case 3: // Enter this block if mark == 1 or mark == 2 or mark == 3
return "mark is either 1, 2 or 3" ;
// etc.
default :
return "mark is not 0, 1, 2 or 3" ;
}
}
The return statements above serve to get out of the function. If you do not want to return, you have to use break; after each block, of course. This code below is equivalent to the one above.
String commentMark(int mark) {
String msg;
switch (mark) {
case 0 : // Enter this block if mark == 0
msg = "mark is 0" ;
break;
case 1:
case 2:
case 3: // Enter this block if mark == 1 or mark == 2 or mark == 3
msg = "mark is either 1, 2 or 3" ;
break;
// etc.
default:
msg = "mark is not 0, 1, 2 or 3" ;
break; // this is a good habit, in case you change default to something else later.
}
return msg;
}
Instead of multiple case we can use or operator in single switch case it self.
switch (date) {
case 1 | 21 | 31:
return "st";
case 2 | 22:
return "nd";
case 3 | 23:
return "rd";
default:
return "th";
}

Switch statement where value is Int but case can contain array [duplicate]

This question already has answers here:
Swift switch pattern matching with arrays
(3 answers)
Closed 6 years ago.
I want to put my array of values as a case for my switch statement
I have an array of values say
let intValues = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 .... 100]
and I want to use a switch statement to be used as a comparison tool like so.
let inputValue = 30 // or some other int value
switch inputValue {
case 101:
// do something lol
/* =============================== */
case intValues: //
// do something more:
/* =============================== */
case 13131:
// do another thing
default:
// do default
}
I know I can do this easily by either doing this case 1, 2, 3, 4, 5, and so on or by using an if statement like so:
if intValues.contains(inputValue) {
// do something more:
} else if inputValue == 101 {
// do something lol
} else if inputValue == 13131 {
// do another thing
} else {
// do default
}
or by doing somethig like
if intValues.contains(inputValue) {
// do something more:
} else {
switch inputValue {
case 101:
// do something lol
case 13131:
// do another thing
default:
// do default
}
}
and vice versa
But is it possible though? to do it with just a switch statement?
You can use case let with where for that.
let intValues = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
let inputValue = 30 // or some other int value
switch inputValue {
case let x where intValues.contains(x):
// do something more:
case 101:
// do something lol
case 13131:
// do another thing
default:
// do default
}
You can do this with range operator.
let inputValue = 30
switch value {
case 1...100:
print("between hundred")
case 101:
print("it's 101")
default:
print("outer value")
}
You should just use a countable closed range in your switch cases:
let inputValue = 30
switch inputValue {
case 1...10:
print(1...10)
case 11...20:
print(11...20)
case 21...100:
print(21...100) // "21...100\n"
default:
print("default")
}

How to print Greek characters from zpl commands

I have an Intermec PC43t barcode printer and i can't print greek characters. When i send greek characters for printing i have dots where the characters should be. When i print something in english i have the right result.
I send ZPL2 commands to Intermec printer (via Zsim).
Below are the commands.
^XA
^BY3
^FT430,80
^BCI,80,Y,N,N
^FD00001978^FS
^FT360,320^ADI,25,14^FD123457^FS
^FT140,320^ADI,25,14^FD1245^FS
^FT300,270^ADI,40,20^FD8794465^FS
^FT300,215^ADI,40,20^FD99999 / 99999^FS
^FT430,175^ADI,25,15^FD40125 - Ελληνικά - Greek ^FS
^XZ
Does someone know a solution?
Thank you
You could try with CIX parameter (Change International Font/Encoding), where X is number of encoding.
May be Zebra Code Page 1253 — Modern Greek Character Set ?
CI34 = Code page 1253
Example of usage:
^FB559,,,C,^FT30,106^A#N,23,31,TT0003M_^FH^CI17^F8^FD_text_^FS^CI0
This solution is not Unicode, but if your firmware supports it (x.14.x and higher according to the documentation) you can force the Greek windows-1253 codepage and append using the hex version of the 1253 code table for each character.
Note: Despite the name, windows-1253 does not require the Windows operating system to function with a Zebra printer.
// ^XA = Start of label
// ^XZ = End of label
// ^CF = Font size
// ^FO = X,Y position on label
// ^CI34 = Code Page 1253 - Zebra x.14.x fimware or higher only
// ^FH = Allow "_FF" style escaped hex characters
^XA
^CF0,60
^CI34^FO50,50^FH^FD - Theta: _C8 ^FS
^CI34^FO50,100^FH^FD - Lambda: _CB ^FS
^XZ
The disadvantage of this approach is a programmer will have to either:
Dynamically map each Greek character to a cp1253 equivalent before preparing the ZPL message, or...
Use a character encoding converter to map the UTF character to the cp1253 equivalent before sending. Many programming languages have built-in methods for doing this. Warning as these may appear as strange symbols in a terminal or output stream.
So, if the printer directly supports Unicode data, that is the ideal solution, but if it doesn't, cp1253 may be a viable option.
Following trsef's answer. This is a Java class that works for me. Might someone find it helpful and save some time:
public class GreekToHexZPL {
public String convertText(String newText) {
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < newText.length(); i++) {
switch (newText.charAt(i)) {
case 'Α':
sBuilder.append("_C1");
break;
case 'Β':
sBuilder.append("_C2");
break;
case 'Γ':
sBuilder.append("_C3");
break;
case 'Δ':
sBuilder.append("_C4");
break;
case 'Ε':
sBuilder.append("_C5");
break;
case 'Ζ':
sBuilder.append("_C6");
break;
case 'Η':
sBuilder.append("_C7");
break;
case 'Θ':
sBuilder.append("_C8");
break;
case 'Ι':
sBuilder.append("_C9");
break;
case 'Κ':
sBuilder.append("_CA");
break;
case 'Λ':
sBuilder.append("_CB");
break;
case 'Μ':
sBuilder.append("_CC");
break;
case 'Ν':
sBuilder.append("_CD");
break;
case 'Ξ':
sBuilder.append("_CE");
break;
case 'Ο':
sBuilder.append("_CF");
break;
case 'Π':
sBuilder.append("_D0");
break;
case 'Ρ':
sBuilder.append("_D1");
break;
case 'Σ':
sBuilder.append("_D3");
break;
case 'Τ':
sBuilder.append("_D4");
break;
case 'Υ':
sBuilder.append("_D5");
break;
case 'Φ':
sBuilder.append("_D6");
break;
case 'Χ':
sBuilder.append("_D7");
break;
case 'Ψ':
sBuilder.append("_D8");
break;
case 'Ω':
sBuilder.append("_D9");
break;
case 'Ϊ':
sBuilder.append("_DA");
break;
case 'Ϋ':
sBuilder.append("_DB");
break;
case 'ά':
sBuilder.append("_DC");
break;
case 'έ':
sBuilder.append("_DD");
break;
case 'ή':
sBuilder.append("_DE");
break;
case 'ί':
sBuilder.append("_DF");
break;
case 'ΰ':
sBuilder.append("_E0");
break;
case 'α':
sBuilder.append("_E1");
break;
case 'β':
sBuilder.append("_E2");
break;
case 'γ':
sBuilder.append("_E3");
break;
case 'δ':
sBuilder.append("_E4");
break;
case 'ε':
sBuilder.append("_E5");
break;
case 'ζ':
sBuilder.append("_E6");
break;
case 'η':
sBuilder.append("_E7");
break;
case 'θ':
sBuilder.append("_E8");
break;
case 'ι':
sBuilder.append("_E9");
break;
case 'κ':
sBuilder.append("_EA");
break;
case 'λ':
sBuilder.append("_EB");
break;
case 'μ':
sBuilder.append("_EC");
break;
case 'ν':
sBuilder.append("_ED");
break;
case 'ξ':
sBuilder.append("_EE");
break;
case 'ο':
sBuilder.append("_EF");
break;
case 'π':
sBuilder.append("_F0");
break;
case 'ρ':
sBuilder.append("_F1");
break;
case 'ς':
sBuilder.append("_F2");
break;
case 'σ':
sBuilder.append("_F3");
break;
case 'τ':
sBuilder.append("_F4");
break;
case 'υ':
sBuilder.append("_F5");
break;
case 'φ':
sBuilder.append("_F6");
break;
case 'χ':
sBuilder.append("_F7");
break;
case 'ψ':
sBuilder.append("_F8");
break;
case 'ω':
sBuilder.append("_F9");
break;
case 'ϊ':
sBuilder.append("_FA");
break;
case 'ϋ':
sBuilder.append("_FB");
break;
case 'ό':
sBuilder.append("_FC");
break;
case 'ύ':
sBuilder.append("_FD");
break;
case 'ώ':
sBuilder.append("_FE");
break;
case 'Ά':
sBuilder.append("_Α2");
break;
case 'Έ':
sBuilder.append("_Β8");
break;
case 'Ή':
sBuilder.append("_Β9");
break;
case 'Ί':
sBuilder.append("_ΒA");
break;
case 'Ό':
sBuilder.append("_ΒC");
break;
case 'Ύ':
sBuilder.append("_ΒE");
break;
case 'Ώ':
sBuilder.append("_ΒF");
break;
case 'A':
sBuilder.append("_41");
break;
case 'a':
sBuilder.append("_61");
break;
case 'B':
sBuilder.append("_42");
break;
case 'b':
sBuilder.append("_62");
break;
case 'C':
sBuilder.append("_43");
break;
case 'c':
sBuilder.append("_63");
break;
case 'D':
sBuilder.append("_44");
break;
case 'd':
sBuilder.append("_64");
break;
case 'E':
sBuilder.append("_45");
break;
case 'e':
sBuilder.append("_65");
break;
case 'F':
sBuilder.append("_46");
break;
case 'f':
sBuilder.append("_66");
break;
case 'G':
sBuilder.append("_47");
break;
case 'g':
sBuilder.append("_67");
break;
case 'H':
sBuilder.append("_48");
break;
case 'h':
sBuilder.append("_68");
break;
case 'I':
sBuilder.append("_49");
break;
case 'i':
sBuilder.append("_69");
break;
case 'J':
sBuilder.append("_4A");
break;
case 'j':
sBuilder.append("_6A");
break;
case 'K':
sBuilder.append("_4B");
break;
case 'k':
sBuilder.append("_6B");
break;
case 'L':
sBuilder.append("_4C");
break;
case 'l':
sBuilder.append("_6C");
break;
case 'M':
sBuilder.append("_4D");
break;
case 'm':
sBuilder.append("_6D");
break;
case 'N':
sBuilder.append("_4E");
break;
case 'n':
sBuilder.append("_6E");
break;
case 'O':
sBuilder.append("_4F");
break;
case 'o':
sBuilder.append("_6F");
break;
case 'P':
sBuilder.append("_50");
break;
case 'p':
sBuilder.append("_70");
break;
case 'R':
sBuilder.append("_52");
break;
case 'r':
sBuilder.append("_72");
break;
case 'S':
sBuilder.append("_53");
break;
case 's':
sBuilder.append("_73");
break;
case 'T':
sBuilder.append("_54");
break;
case 't':
sBuilder.append("_74");
break;
case 'U':
sBuilder.append("_55");
break;
case 'u':
sBuilder.append("_75");
break;
case 'V':
sBuilder.append("_56");
break;
case 'v':
sBuilder.append("_76");
break;
case 'Y':
sBuilder.append("_59");
break;
case 'y':
sBuilder.append("_79");
break;
case 'Z':
sBuilder.append("_5A");
break;
case 'z':
sBuilder.append("_7A");
break;
case ' ':
sBuilder.append(" ");
break;
default:
sBuilder.append(newText.charAt(i));
break;
}
}
return sBuilder.toString();
}
}

Does a break statement break from a switch/select?

I know that switch/select statements break automatically after every case. I am wondering, in the following code:
for {
switch sometest() {
case 0:
dosomething()
case 1:
break
default:
dosomethingelse()
}
}
Does the break statement exit the for loop or just the switch block?
Break statements, The Go Programming Language Specification.
A "break" statement terminates execution of the innermost "for",
"switch" or "select" statement.
BreakStmt = "break" [ Label ] .
If there is a label, it must be that of an enclosing "for", "switch"
or "select" statement, and that is the one whose execution terminates
(§For statements, §Switch statements, §Select statements).
L:
for i < n {
switch i {
case 5:
break L
}
}
Therefore, the break statement in your example terminates the switch statement, the "innermost" statement.
A hopefully illustrative example:
loop:
for {
switch expr {
case foo:
if condA {
doA()
break // like 'goto A'
}
if condB {
doB()
break loop // like 'goto B'
}
doC()
case bar:
// ...
}
A:
doX()
// ...
}
B:
doY()
// ....
Yes, break breaks the inner switch.
https://play.golang.org/p/SZdDuVjic4
package main
import "fmt"
func main() {
myloop:
for x := 0; x < 7; x++ {
fmt.Printf("%d", x)
switch {
case x == 1:
fmt.Println("start")
case x == 5:
fmt.Println("stop")
break myloop
case x > 2:
fmt.Println("crunching..")
break
default:
fmt.Println("idling..")
}
}
}
0idling..
1start
2idling..
3crunching..
4crunching..
5stop
Program exited.
This question might be too old already but I still think label makes our code become harder to read.
Instead of breaking the for inside select, just set a flag for the loop and handle it inside select-case before invoking break.
For example:
loop := true
for loop {
select {
case <-msg:
// do your task here
case <-ctx.Done():
loop = false
break
}
}
Updated: Totally agree with Vaelin in the comment. Declaring that flag inside the scope of the for loop can avoid memory leak and conflict with other variables in current scope, just in case we have a same variable name already.
for loop := true; loop; {
}
Just from a switch block. There's plenty of examples in Golang own code you can examine (compare inner break with outer break).
this should explain it.
for{
x := 1
switch {
case x >0:
fmt.Println("sjus")
case x == 1:
fmt.Println("GFVjk")
default:
fmt.Println("daslkjh")
}
}
}
Runs forever
for{
x := 1
switch {
case x >0:
fmt.Println("sjus")
break
case x == 1:
fmt.Println("GFVjk")
default:
fmt.Println("daslkjh")
}
}
}
Again, runs forever
BUT
package main
import "fmt"
func main() {
d:
for{
x := 1
switch {
case x >0:
fmt.Println("sjus")
break d
case x == 1:
fmt.Println("GFVjk")
default:
fmt.Println("daslkjh")
}
}
}
will print sjus
... clear ?
http://play.golang.org/p/GOvnfI67ih
It only exits the switch block.

problem with UISegmentedControl

-(IBAction)changeSegmentDistance:(UISegmentedControl *)sender{
// refineDistance=sender;
switch ([refineDistance selectedSegmentIndex]) {
case 0:
valueString=5;
NSLog(#"value String %d",valueString);
break;
case 1:
valueString=10;
NSLog(#"value Stringaaaa %d",valueString);
case 2:
valueString=15;
NSLog(#"value String %d",valueString);
break;
case 3:
valueString=16;
NSLog(#"value String %d",valueString);
break;
default:
break;
}
}
When i running application and printing on Console
using NSLog...
when i select 0 its printing 5...
when i selected 1 its printing 10 and 15
when i selected 2 its printing 15
when i selected 3 its printed 16..
I dont know why its printing 10 and 15 when i selected 2nd.
You're missing a "break;" at the end of your "case 1" statement block. As such, execution is continuting into the next case statement.
You are missing a break for case 1.