Swift Control Flow
Swift if Statement
If is a conditional statement. That provide the selection of statements which are execute in based on specific expression. This process is provide dynamic execution of block in conditional expression. Expression is based on boolean value or that are produce boolean result. Otherwise we cannot use unconditional statement within the if-block. Boolean true result is provide the execution of if-block. We can define if statement using of following syntax.
if expression {
//body of if statements
}
For example.
//Conditional expression (10>5)
if 10 > 5 {
//When 10 is greater than 5
print("Ten is bigger than Five")
}
//Conditional expression ("Foo"=="foo")
if "Foo"=="foo" {
//when "Foo is equal to foo"
print("Foo is equal to foo")
}
Output
Ten is bigger than Five
In this example note that given of two If conditional statements, but only executed first one because only first statement condition are valid and that result are produce boolean true result.
If-else statement
else-statement is an optional block of if-statement, that are work when if statement are not work then else-block are work.
if(condition){
//statements
}else{
//statements
}
else-block is optional but most of cases it can be used. For example there is possible to use expression as optional binding in swift programming. That are useful of most of cases when comparing value before assignment.
func convertToInt(_ data:String)->Int{
//when gets valid number format
if let number=Int(data){
return number;
}else{
return 0;
}
}
let test1=convertToInt("123")
let test2=convertToInt("X")
let test3=convertToInt("12X")
print("test1 : \(test1)")
print("test2 : \(test2)")
print("test3 : \(test3)")
Output
test1 : 123
test2 : 0
test3 : 0
This process is safe to check, that value is exist or not. when value is exist then possible to check other condition of created variable. And scope of this variable are accessible within "If block".
If expression is work on boolean value or boolean result if in case expression are not produce boolean expression then compiler are produce an error. for example view this program.
if(1){
//when expression result is boolean true ✓
print("If part")
}else{
//other case
print("Else part")
}
Output
swift test.swift
test.swift:1:4: error: 'Int' is not convertible to 'Bool'
if(1){
~^~
If we are run this program to c and c++ then that is fine but in Swift programming If-statement are depending upon boolean expression result or boolean values.
Series of (if .. else-if) statement
When if expression are not work then we can use series of (else-if) statements. That is useful when multiple situations are possible. Their syntax as follows.
if expression1{
//statement
}else if expression2{
//statement
}else if expression3{
//statement
}
:
:
:
:else {
//statement
}
let see an example
enum Direction{
case left
case right
case back
case straight
}
var mouse : Direction = .back
if mouse == .left{
print("Go to left")
}else if mouse == .right {
print("Go to right")
}
else if mouse == .back{
print("Go to back")
}else{
print("Go to straight")
}
Output
Go to back
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment