由于ForEach()方法遍历所有列表成员,为什么不能使用break/continue子句,而我可以在正常的foreach循环中使用它们
lstTemp.ForEach(i=>
{
if (i == 3)
break;
//do sth
}
);
Run Code Online (Sandbox Code Playgroud)
错误:
"没有封闭的环可以打破或继续"
在"继续"或"休息"之后将执行哪个声明?
for(int i = 0; i < count; ++i)
{
// statement1
for(int j = 0; j < count; ++j)
{
//statement2
if(someTest)
continue;
}
//statement3
}
for(int i = 0; i < count; ++i)
{
// statement1
for(int j = 0; j < count; ++j)
{
//statement2
if(someTest)
break;
}
//statement3
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个包含while循环的程序,在这个循环中,如果出现问题,我会收到错误消息.有点像这样;
while True:
questionx = input("....")
if x =="SomethingWrongabout questionX":
print ("Something went wrong.")
continue
other codes...
questiony = input("....")
if y == "SomethingWrongabout questionY":
print ("Something went wrong.")
continue
other codes...
questionz = input("....")
if z == "SomethingWrongabout questionZ":
print ("Something went wrong.")
continue
other codes..
Run Code Online (Sandbox Code Playgroud)
问题如下:当发生错误后questionX,程序进入开始状态.它从头开始,而不是从y或z.但是,x没有问题,因此,程序应该开始提出问题y或z因为,问题发生在y或z.
如何使程序从特定点开始,如果只有y问题出错,程序必须开始提问y或者只是在z,程序必须从头开始z,而不是从头开始x.
我应该while为此使用多个循环,还是有任何东西只能在一个循环中使用?
我有一类对象,需要将每个对象的一个属性与所有其他对象的相同属性进行比较.如果匹配,代码需要做一些事情.这导致两个'for循环'循环遍历对象以获取该属性,而在第二个'for循环'中,有第三个'for循环'遍历属性的元素(这是一个向量)来比较它们.如果它们匹配,我需要最外面的'for循环'来中止当前迭代并继续下一个(我只希望第一个匹配另一个对象被考虑).
我已经研究了'goto'语句并创建了一个do {} while()结构,但是却无法以获得所需结果的方式实现它们.我需要的是基于内部循环中条件语句中发生的事情,最外层循环的'continue'语句.
哪个是实现这一目标的好方法,它将如何实施?
编辑:在我接受的答案旁边,我也会推荐Martin Bonner的答案,这也很好,并且不依赖于goto.
for (int i = 0; i < max; i++){
Object & object1 = system.getAgent(i);
VectorOfStrings object_property1 = object1.getProperty();
for (int j = i + 1; j < max; j++){
Object & object2 = system.getObject(j);
VectorOfStrings object_property2 = object2.getProperty();
for (unsigned int k = 0; k < object_property1.size(); k++){
if (object_property1[k] == object_property2[k]){
//do something
break; //this aborts the inner most loop
//Additionally, I need the outer most loop to move on one iteration …Run Code Online (Sandbox Code Playgroud) 当您使用
continue无标签,它会终止最内层的当前迭代while,do-while或for语句,并继续与下一个迭代循环的执行.
我不确定为什么下面的代码不能像我期望的那样工作.
do {
continue;
} while(false);
Run Code Online (Sandbox Code Playgroud)
即使while条件是false,我希望它会永远运行,因为continue跳转到块的开始,它会立即continue再次执行,等等.然而,不管怎样,循环在一次迭代后终止.看起来好像continue被忽略了.
如何continue在一个do-while循环的工作?
我有下面的代码(实际上比你看到的要长得多!)
foreach (SensorPair sensor in _sensorPairs)
{
sensorByte = (byte) sensor.Sensor;
if (!packet.Contains(sensorByte))
continue;
index = packet.IndexOf(sensorByte);
byteCount = sensor.ByteCount;
switch (byteCount)
{
case 1:
try
{
switch(sensor.ValueType)
{
case SensorValueType.Unsigned:
val = (int)packet[index + 1];
if (val > 255)
//*** WHAT DOES THIS CONTINUE DO?
continue;
else //rise the event
OnSensorReport();
break;
Run Code Online (Sandbox Code Playgroud)
continue你看到的keywoard会导致foreach循环开始迭代下一个项目还是只是传递给下一个case语句?
如果它没有对foreach循环做任何事情,我怎么能强制代码退出开关并在foreach循环中启动下一个itteration?
基本上,我需要一种方法将控制返回到for循环的开头,并且如果满足某个条件,则在采取操作后实际重新启动整个迭代过程.
我想要做的是这样的:
for index, item in enumerate(list2):
if item == '||' and list2[index-1] == '||':
del list2[index]
*<some action that resarts the whole process>*
Run Code Online (Sandbox Code Playgroud)
这样,如果['berry','||','||','||','pancake'在列表中,我将结束:
['berry','||','pancake']相反.
谢谢!
我一直在努力解决这个错误,现在似乎对于翻译抱怨"继续"的原因有不同的看法.所以我想在下面提供错误的代码.
import tweepy
import time
def writeHandlesToFile():
file = open("dataFile.txt","w")
try:
list = tweepy.Cursor(tweepy.api.followers,screen_name='someHandle',).items(100000)
print "cursor executed"
for item in list:
file.write(item.screen_name+"\n")
except tweepy.error.TweepError as e:
print "In the except method"
print e
time.sleep(3600)
continue
Run Code Online (Sandbox Code Playgroud)
我特别想在最后包括continue的原因是因为我希望程序在睡眠后从它停止的位置重新开始执行,以便保留程序状态.我需要睡眠才能遵守twitter api速率限制,其中api只允许你每小时发出一定数量的请求.所以任何可能看到我的错误天真或其他的人请指出它或请在没有使用continue语句的情况下为我提供替代实现.
顺便说一下,我没有像另一篇文章中所建议的那样混合标签和空格.提前谢谢你的帮助.
这很好用:
#include <stdio.h>
int main(){
volatile int abort_counter = 0;
volatile int i = 0;
while (i < 100000000) {
__asm__ ("xbegin ABORT");
i++;
__asm__ ("xend");
__asm__ ("ABORT:");
++abort_counter;
}
printf("%d\n", i);
printf("nof abort-retries: %d\n",abort_counter-i);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,我最初写的是
#include <stdio.h>
int main(){
volatile int abort_counter = 0;
volatile int i = 0;
while (i < 100000000) {
__asm__ ("xbegin ABORT");
i++;
__asm__ ("xend");
continue;
__asm__ ("ABORT:");
++abort_counter;
}
printf("%d\n", i);
printf("nof abort-retries: %d\n",abort_counter);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但这导致了
/tmp/cchmn6a6.o: In …Run Code Online (Sandbox Code Playgroud) 我有以下循环,continue当内循环内的检查符合条件时,我想要while循环.我在这里找到了一个解决方案(我在下面的例子中应用了),但它适用于c#.
$continue = false;
while($something) {
foreach($array as $value) {
if($ok) {
$continue = true;
break;
// continue the while loop
}
foreach($value as $val) {
if($ok) {
$continue = true;
break 2;
// continue the while loop
}
}
}
if($continue == true) {
continue;
}
}
Run Code Online (Sandbox Code Playgroud)
continue当内部循环被break淘汰时,PHP是否有自己的主循环方式?
continue ×10
loops ×6
break ×3
python ×3
c# ×2
c++ ×2
foreach ×2
while-loop ×2
c ×1
conditional ×1
do-while ×1
for-loop ×1
gcc ×1
iteration ×1
javascript ×1
nested ×1
nested-loops ×1
php ×1
python-3.x ×1
try-except ×1
twitter ×1