Let's understand the for-in statement!

This time, I learned about for-in statements, so I will output them.

What is a for-in statement?

*** The for-in statement iterates as many times as there are elements, passing each element to the executable statement in sequence. *** ***

The basic writing method is as follows

qiita.rbvar


for element name in sequence{ 
Statements that are repeatedly executed element by element
 }

Let's look at a simple example next.

qiita.rbvar


1.let array = [1,2,3]
2.for element in array {

print(element)
}
Execution result
1
2
3

In the above example, the values in the array array are accessed one by one through the constant element.

When enumerating elements of Dictionary <key, Value> type with for-in, the element type is a tuple of type (key, value). For example, a value of type [String: Int] When passed to a for-in statement, the element will be of type (String: Int).

Let's look at a simple example.

qiita.rbvar


let dictionary = ["a": 1,"b": 2]

for (key,value) in dictionary {

print("key:\(key),Value\(value)")
}
Execution result
key: a Value: 1
key: b Value: 2

break statement

The break statement breaks the execution statement and ends the entire repeating statement. For example, it is used when you need to repeat it any more.

Let's look at a simple example.

qiita.rbvar


var containsThree = false
let array = [1,2,3,4,5]

for element in array {
if element == 3 {
  containsThree = true
  break
//Finish when 3 is found
}
print("element:\(containsTwo)")
}
print("containsTwo: \(containsTwo)")

Execution result
element: 3
containsTwo: true

In the above example, it is a program that checks if the array contains 3. Since it is not necessary to repeat the following when 3 is found, the break statement is used to end the repeating statement.

continue statement

The continue statement suspends the execution statement and then continues the subsequent iterations. For example, it is used to skip processing only in specific cases.

qiita.rbvar


var adds = [Int]()
let array = [1,2,3,]

for element in array {
    if element % 2 == 1 {
    adds.append(element)
    continue
}
print("even: \(element)")

}

print("odds:\(adds)")

Execution result
even: 2
adds: [1,3]

In the above example, unlike the break statement, the subsequent iterations are continued, and you can see that all the elements are processed.

Recommended Posts

Let's understand the for-in statement!
Let's understand the if statement!
Let's understand the guard statement!
Let's understand the switch statement!
Let's understand the function!
Let's understand the Array (Element) type!
Let's understand the Optional (Wrapped) type!
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)
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)
Understand the basic mechanism of log4j2.xml
[java8] To understand the Stream API
[Swift] "for-in statement" about iterative processing
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)
Yes, let's preview the image. ~ part5 ~
Image processing: Let's play with the image
Let's attack the vulnerability (2) Open redirect