给出以下代码(不起作用):
while True:
#snip: print out current state
while True:
ok = get_input("Is this ok? (y/n)")
if ok.lower() == "y": break 2 #this doesn't work :(
if ok.lower() == "n": break
#do more processing with menus and stuff
Run Code Online (Sandbox Code Playgroud)
有没有办法让这项工作?或者我是否已经做了一次检查以突破输入循环,然后另一个更有限的检查外部循环以在用户满意的情况下将所有内容分开?
Edit-FYI: get_input是我编写的一个简短函数,支持显示提示和默认值以及所有那些幻想和返回__CODE__
在Javascript中打破嵌套循环的最佳方法是什么?
//Write the links to the page.
for (var x = 0; x < Args.length; x++)
{
for (var Heading in Navigation.Headings)
{
for (var Item in Navigation.Headings[Heading])
{
if (Args[x] == Navigation.Headings[Heading][Item].Name)
{
document.write("<a href=\""
+ Navigation.Headings[Heading][Item].URL + "\">"
+ Navigation.Headings[Heading][Item].Name + "</a> : ");
break; // <---HERE, I need to break out of two loops.
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 我试图break
在for
循环中使用一个语句,但由于我在Perl代码中也使用严格的subs,我收到一个错误说:
在./final.pl第154行使用"严格潜艇"时不允许使用Bareword"break".
有没有解决方法(除了禁用严格的潜艇)?
我的代码格式如下:
for my $entry (@array){
if ($string eq "text"){
break;
}
}
Run Code Online (Sandbox Code Playgroud) 是否可以使用该break
函数退出几个嵌套for
循环?如果是这样,你会怎么做呢?你还可以控制休息退出的循环次数吗?
我如何打破循环?
var largest=0
for(i<-999 to 1 by -1) {
for (j<-i to 1 by -1) {
val product=i*j
if (largest>product)
// I want to break out here
else
if(product.toString.equals(product.toString.reverse))
largest=largest max product
}
}
Run Code Online (Sandbox Code Playgroud)
如何将嵌套for循环转换为尾递归?
来自FOSDEM 2009 上的Scala Talk http://www.slideshare.net/Odersky/fosdem-2009-1013261在第22页:
打破并继续Scala没有它们.为什么?他们有点必要; 更好地使用许多较小的函数问题如何与闭包交互.他们不需要!
解释是什么?
我有一个foreach循环和一个if语句.如果发现匹配,我需要最终打破foreach.
foreach($equipxml as $equip) {
$current_device = $equip->xpath("name");
if ( $current_device[0] == $device ) {
// found a match in the file
$nodeid = $equip->id;
<break out of if and foreach here>
}
}
Run Code Online (Sandbox Code Playgroud) 我试过这个:
for(i = 0; i < 5; i++){
for(j = i + 1; j < 5; j++){
break(2);
}
alert(1);
}
Run Code Online (Sandbox Code Playgroud)
只得到:
SyntaxError
:;
在陈述之前遗漏
那么,我如何打破JavaScript中的嵌套循环?
选项1 - 使用return返回:
function myFunction(opt)
{
switch (opt)
{
case 1: return "One";
case 2: return "Two";
case 3: return "Three";
default: return "";
}
}
Run Code Online (Sandbox Code Playgroud)
选项2 - 使用break切换:
function myFunction(opt)
{
var retVal = "";
switch (opt)
{
case 1:
retVal = "One";
break;
case 2:
retVal = "Two";
break;
case 3:
retVal = "Three";
break;
}
return retVal;
}
Run Code Online (Sandbox Code Playgroud)
我知道两者都有效,但又是一种最佳实践吗?我倾向于喜欢选项1 - 使用返回最佳切换,因为它更干净,更简单.
这是我使用@ ic3b3rg评论中提到的技术的具体例子的jsFiddle:
var SFAIC = {};
SFAIC.common =
{
masterPages:
{
cs: "CS_",
cp: "CP_" …
Run Code Online (Sandbox Code Playgroud) 我知道switch
/ select
case在每个案例后自动中断.我想知道,在以下代码中:
for {
switch sometest() {
case 0:
dosomething()
case 1:
break
default:
dosomethingelse()
}
}
Run Code Online (Sandbox Code Playgroud)
是否break
声明退出for
循环或只是switch
块?
break ×10
javascript ×3
nested-loops ×3
for-loop ×2
loops ×2
c++ ×1
continue ×1
control-flow ×1
foreach ×1
go ×1
if-statement ×1
java ×1
perl ×1
php ×1
python ×1
return ×1
scala ×1
select ×1
strict ×1