Wrong:
let myCondition = 1
if myCondition {
//Error
}
Correct:
let myCondition = 1
if myCondition == 1 {
}
or
or
let myCondition = true
if myCondition {
}
The switch case is as below:
var myCondition = 5 //Change this for different values
switch myCondition {
case 0:
print("This is zero.")
case 1:
print("myCondition is one.")
case 2,3:
print("I like number two or three.")
case 4...10:
print("Four to ten.\nfallthrough")
fallthrough
default:
print("The default case.")
break
//print("Print more")
}
case 0:
print("This is zero.")
case 1:
print("myCondition is one.")
case 2,3:
print("I like number two or three.")
case 4...10:
print("Four to ten.\nfallthrough")
fallthrough
default:
print("The default case.")
break
//print("Print more")
}
//Console output: myCondition is one.
//No "break" is required for each case.
//Default condition is essential.
//Use comma if the cases share the same codes, e.g. case 2, 3:
//Use ... to represent consecutive values
//Write fallthrough if the case below needs to be run.
//Change this:
let myCondition = 7
//Console output:
Four to ten.
The default case.
No comments:
Post a Comment