Let's understand the if statement!

This time, I learned about if, so I will output it.

What is an if statement?

*** 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!

What is the else clause?

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
}

What is an if-let statement?

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

Finally

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

Let's understand the if statement!
Let's understand the guard statement!
Let's understand the for-in statement!
Let's understand the switch statement!
Let's understand the function!
if statement
Let's understand the Array (Element) type!
Let's understand the Optional (Wrapped) type!
Let's understand closures!
10 Corresponds to if statement
Studying Java-Part 10-if statement
About for statement and if statement
Isn't the While statement evil?
Let's make the app better
12 Corresponds to the while statement
Understand the basics of docker
Let's try the S2Struts tutorial (# 3_180425)
Let's solve the roman numerals
Understand the helper method form_with
Let's try the S2Struts tutorial (# 5_180526)
Points for refactoring (if statement)
Let's try the S2Struts tutorial (# 4_180505)
Let's try the S2Struts tutorial (# 1_180423)
Let's solve the FizzBuzz problem!
Let's try the S2Struts tutorial (# 2_180424)
[Ruby] problem with if statement
[Ruby] conditional branch if statement
Understand the basic mechanism of log4j2.xml
Let's verify the image search filter
Understand the difference between each_with_index and each.with_index
Let's try the S2Struts tutorial (# 0_yymmdd)
Java study # 4 (conditional branching / if statement)
About if statement and branch processing
Yes, let's preview the image. ~ part5 ~
Image processing: Let's play with the image
Let's attack the vulnerability (2) Open redirect
Write a null case using the Optional type without using an if statement
[Ruby] I tried to diet the if statement code with the ternary operator