我正在寻找一种方法来在条件满足后停止迭代正内循环。我想过使用“继续”,但它没有做我想要的。 “break”似乎打破了整个循环,包括外循环。
所以在我的代码中,一旦满足条件。我想停止迭代j,但我想再次开始迭代。谢谢,
for i in 0..<sortedArray.count{
for j in 1..<sortedArray.count{
if sortedArray[j] == sortedArray[i]{
//I want to skip iterating inner loop j from now. and back to iterating i
}
}
}
答案 0 :(得分:6)
Break只会破坏内循环。
e.g。
for var i in 0...2
{
for var j in 10...15
{
print("i = \(i) & j = \(j)")
if j == 12
{
break;
}
}
}
输出 - &gt;
i = 0 & j = 10
i = 0 & j = 11
i = 0 & j = 12
i = 1 & j = 10
i = 1 & j = 11
i = 1 & j = 12
i = 2 & j = 10
i = 2 & j = 11
i = 2 & j = 12
答案 1 :(得分:0)
嵌套索引循环和通过索引方法(let sortedArray = [2, 5, 7, 9]
for i in sortedArray {
for j in sortedArray where j <= i {
print(j)
}
print("-----")
}
/* 2
-----
2
5
-----
2
5
7
-----
2
5
7
9
----- */
)进行的后续元素访问是一种C ++'ish而不是Swifty。根据您的应用程序,您可以充分利用Swift中的简洁功能来实现您的目标。
例如,假设(基于数组的anme)对数组进行排序,
for ... in (... where ...)
请注意,我们不关心上面数组的索引,而是直接在for i in 1...sortedArray.count {
sortedArray.prefixUpTo(i).forEach {
print($0)
}
print("-----")
}
/* same printout as above */
循环中访问数组的元素。
另一个替代coulkd使用prefixUpTo(:_)
$servername = "localhost"; //host name
$username = "username"; //username
$password = "password"; //password
$database = "dbname"; //database name
//mysqli prepared statement
$conn = mysqli_connect($servername, $username, $password) or die("Connection failed: " . mysqli_connect_error());
mysqli_select_db($conn,$database) or die("Opps some thing went wrong");
$Wn8overall = mysqli_real_escape_string($_POST['wn8overall']);
$Wn8recent = mysqli_real_escape_string($_POST['wn8recent']);
$Email = mysqli_real_escape_string($_POST['email']);
$Zileonline = mysqli_real_escape_string($_POST['zileonline']);
$Tara = mysqli_real_escape_string($_POST['tara']);
$Tank10 = mysqli_real_escape_string($_POST['tank10']);
$Tank8 = mysqli_real_escape_string($_POST['tank8]);
$Careclan = mysqli_real_escape_string($_POST['careclan']);
$Numecont = mysqli_real_escape_string($_POST['numecont']);
$stmt = $conn->prepare("INSERT INTO aplicatii1 (wn8overall,wn8recent,email,zileonline,tara,tank10,tank8,careclan,numecont) VALUES (?,?,?,?,?,?,?,?,?)");
$stmt->bind_param('sssssssss',$Wn8overall,$Wn8recent,$Email,$Zileonline,$Tara,$Tank10,$Tank8,$Careclan$Numecont);
/*
The argument may be one of four types:
i - integer
d - double
s - string
b - BLOB
change it by respectively
*/
$stmt->execute();
$row_count= $stmt->affected_rows;
$stmt->close();
$conn->close();
if($row_count>0){
echo "New record created successfully";
}
else{
}