📜  Swift – For-in 循环

📅  最后修改于: 2022-05-13 01:55:47.634000             🧑  作者: Mango

Swift – For-in 循环

Swift 中的 for-in 循环与Python中的 for 循环非常相似。在Python中,我们在 range() 中编写迭代。这里的迭代只是迭代的一个变量。在swift中,我们也为范围内的迭代编写。我们有集合、数组、字典之类的集合,因此我们可以使用 for-in 循环来迭代它们的项目,这意味着它用于迭代任何序列。 for-in 循环和 if 语句非常相似。在 if 语句中,条件仅在满足条件时才用于执行语句块。否则,它不会被执行。同样,for-in 循环也用于在满足条件时执行语句块。但是我们可以将代码块重复 n 次。使用 if 条件我们不能重复执行代码。通常,for-in 循环有两个关键字forin

句法:

我们还可以在 for-in 循环中使用下划线 (_) 代替 variable_name 来忽略序列中的值。在循环的迭代过程中,它不会访问当前值。

句法:

for-in 循环流程图:

例子:

Swift
// Swift program to illustrate the use of for-in loop
  
// Creating an array which contain
// the name of the courses
let course = [ "DSA", "ReactJS", "Java", "C++" ]
  
print("Courses are: ")
  
// Iterating the elements
// of course array
// Using for-in loop
for i in course{ print(i) }


Swift
// Swift program to illustrate
// the use of for-in loop with array
  
// Creating an array which contain
// the name of the students
let arr = [ "Karthik", "Chandu", "Nandu", 
            "Komal", "Sohan", "Suresh" ]
  
print("Student names are: ")
  
// Iterating the elements
// of arr array
// Using for-in loop
for i in arr{ 
    print(i) 
}


Swift
// Swift program to illustrate the use
// of for-in loop with where clause
  
// Creating an array which contain
// the name of the students
let arr = [ "Karthik", "Chandu", "Nandu", 
            "Komal", "Sohan", "Suresh" ]
  
print("Student names are: ")
  
// Iterating the elements of the array
// Here we apply filter that is 
// "i != nandu" using where
// clause in for-in loop
for i in arr where i != "Nandu"{
    print(i)
}


Swift
// Swift program to illustrate the use
// of for-in loop with range
  
// Display the numbers starting from 1 to 15
print("Numbers are:")
for i in 1...15{
    print(i)
}


Swift
// Swift program to illustrate the use
// of for-in loop with range
  
// Display the numbers starting from 1 to 9
print("Numbers are:")
for i in 1..<10{
    print(i)
}


Swift
// Swift program to illustrate the use
// of for-in loop with stride function
  
// Display the numbers starting from 1 to 9
// Using stride() function
for i in stride(from:1, to:10, by:1)
{
    print(i)
}


输出:

Courses are: 
DSA
ReactJS
Java
C++

带数组的 for-in 循环

众所周知,数组用于存储相同类型的多个元素。因此,借助 for-in 循环,我们可以轻松地逐一迭代给定数组中存在的所有元素。

句法:

例子:

迅速

// Swift program to illustrate
// the use of for-in loop with array
  
// Creating an array which contain
// the name of the students
let arr = [ "Karthik", "Chandu", "Nandu", 
            "Komal", "Sohan", "Suresh" ]
  
print("Student names are: ")
  
// Iterating the elements
// of arr array
// Using for-in loop
for i in arr{ 
    print(i) 
}

输出:

Student names are: 
Karthik
Chandu
Nandu
Komal
Sohan
Suresh

for – in 循环使用 where 子句的数组:我们也可以使用 where 子句和 for-in 循环。 where 子句用于根据 where 子句中指定的条件过滤给定序列中的元素。

句法:

例子:

迅速

// Swift program to illustrate the use
// of for-in loop with where clause
  
// Creating an array which contain
// the name of the students
let arr = [ "Karthik", "Chandu", "Nandu", 
            "Komal", "Sohan", "Suresh" ]
  
print("Student names are: ")
  
// Iterating the elements of the array
// Here we apply filter that is 
// "i != nandu" using where
// clause in for-in loop
for i in arr where i != "Nandu"{
    print(i)
}

输出 :

Student names are: 
Karthik
Chandu
Komal
Sohan
Suresh

说明:在上面的例子中,我们首先创建了一个名为“arr”的数组,其中包含学生的姓名。现在使用 for-in 循环,我们正在访问数组中的元素。这里我们使用 where 子句和 for-in 循环根据条件“i != Nandu”过滤元素,这意味着 Nandu 不会显示在输出中。

for - 在范围内循环

使用 range 和 for 循环,我们可以多次重复循环内的语句。例如,如果我们想打印 1 到 100 个数字,我们可以在 for 循环中指定一个范围并打印它,而不是使用 print()函数100 次。

句法:

这里范围不是标识符。 Range 指定范围的开始和结束。例如,如果我们要打印 1 到 10 个值,则必须写 1…10 来代替范围。这里“ ...”运算符的行为通常类似于 –。通常,我们使用 1-10 来指定范围。在 Swift 中,我们使用三个点“ ……”。

例子:

迅速

// Swift program to illustrate the use
// of for-in loop with range
  
// Display the numbers starting from 1 to 15
print("Numbers are:")
for i in 1...15{
    print(i)
}

输出:

Numbers are:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

说明:在上面的例子中,我们将显示从 1 到 15 的数字。所以我们在 for-in 循环中使用范围运算符(...) 指定一个范围。这将打印从 1 到 15 的数字。

有时我们不想要一个近距离(包含起点和终点)。因此,我们使用带有 for-in 循环的半开范围运算符(..<)。它仅包括下限,但不包括上限。让我们借助一个例子来讨论:

迅速

// Swift program to illustrate the use
// of for-in loop with range
  
// Display the numbers starting from 1 to 9
print("Numbers are:")
for i in 1..<10{
    print(i)
}

输出:

Numbers are:
1
2
3
4
5
6
7
8
9

具有跨步函数的 for-in 循环

stride函数与Python编程中的 range()函数非常相似。在Python中,我们通过 start、stop、step 进入 range函数。同样,在 Swift 中,我们使用 from、to、by。工作原理类似于Python中的 range()函数。通常,在Python中,我们指定最大值而不是指定 from 和 to。至于我在范围内(100)。并且第三个参数步骤不是强制通过的。但是在这里,所有参数 from、to、by 都是强制性的。如果我们跳过任何参数,我们可能会得到一个错误。所以我们需要将所有参数传递给 stride函数。与Python中的 range函数一样,swift 中的 stride函数也可以打印 n-1 范围内的数字。如果我们想使用 range函数打印 1 到 20 个数字,它最多只能打印 19 个。同样,stride 也只打印 n-1。

句法:

例子:

迅速

// Swift program to illustrate the use
// of for-in loop with stride function
  
// Display the numbers starting from 1 to 9
// Using stride() function
for i in stride(from:1, to:10, by:1)
{
    print(i)
}

输出:

1
2
3
4
5
6
7
8
9

说明:在上面的例子中,我们使用 for-in 循环使用 stride()函数打印指定范围内的值。在这里,我们打印 1 到 10 个值。因为 stride函数的工作方式类似于Python中的 range函数。与 range()函数类似,stride()函数也打印 n-1 个值。在这里,我们给出了 1-10 的范围。我们得到了 n-1 个值。即 1-9 个值作为输出。