This time, I learned about if, so I will output it.
*** In a nutshell, if is a syntax that is executed depending on the success or failure of the condition. *** *** The basic writing method is as follows
qiita.rbvar
if conditional expression{Statement executed when the conditional expression is true}
Let's look at a simple example next.
qiita.rbvar
1.let value = 5
2.if value <= 10 {
print("value is 10 or less")
}
Execution result: value is 10 or less
If you try to translate this code into Japanese
qiita.rbvar
if,[If the value of value is 10 or less{}Please execute the method in]It means that
It's very easy and easy to understand! *** By the way, the conditional expression of the if statement must return a Bool type! *** ***
Let's dig deeper into the if statement!
The else clause is a statement that is executed when the *** condition is not met. *** *** Let's look at the basic syntax!
qiita.rbvar
if conditional expression{Statement executed when the conditional expression is true
}else{
Statement executed when the conditional expression is false
}
Let's look at a simple example next!
qiita.rbvar
1.let value = 26
2.if value <= 10 {
print("value is 10 or less")
}else{
print("value is greater than 10")
}
//Execution result: value is greater than 10
Similar to the above, if you try to translate this code into Japanese
qiita.rbvar
if,[If the value of value is 10 or less{}Please execute the method in.
If not, else{}It means to execute the method in.
You can also connect if statements to the else clause.
qiita.rbvar
if conditional expression 1{
Statement executed when conditional expression 1 is true
}else if conditional expression 2{
Executed when conditional expression 1 is false and conditional expression 2 is true
}else{
Executed when both conditional expression 1 and conditional expression 2 are false
}
The if-let statement is a statement that branches depending on the presence or absence of an optional type value, and if a value exists, the value can be retrieved at the same time.
*** What is Optional type? *** Click here for those who want to deepen the Optional type! Understand the Optional (Wrapped) type!
Let's see the basic writing style!
qiita.rbvar
if let constant name=Optional type value{
Statement executed if the value exists
}else{
Statement executed when the value does not exist
}
Next, let's look at a simple example.
qiita.rbvar
let optionalA =Optional("G")
if let X = optionalA {
print("value is\(X)")
}else{
print("Value does not exist")
Execution result:The value is G
This time, I output about the if statement. Even if you are a beginner in programming, the syntax is easy to understand and is often used in application development, so I want to master the basics firmly.
Recommended Posts