我在TCL编程方面相当新,通过基础知识我遇到了以下代码片段:
set x 0;
while "$x < 3" {
set x [expr $x + 1]
if {$x >6} break;
if "$x > 2" continue;
puts "x is $x";
}
puts "exited second loop with X equal to $x\n"
Run Code Online (Sandbox Code Playgroud)
执行时,结果如下:
x是1
x是2个
退出的第二个循环,X等于7
令我惊讶的是,当执行continue命令时,似乎没有评估while循环测试(x <3).但是在tcl联机帮助页中声明" 正文中的continue语句将停止代码的执行,并且将重新评估测试. "
我错过了什么?
我的程序基本如下:
for l in range(0,100):
file = open("C:/Twitter/json/user_" + str(l) + ".json", "r")
#do some stuff
file.close()
Run Code Online (Sandbox Code Playgroud)
我试图找出一种方法来处理如果丢失文件20将被抛出的异常,并告诉它continue.然而,我试图使用continuewith try语句,它一直在抱怨我没有把它正确放入循环中.任何意见,将不胜感激.
基本上我试过:
try:
for:
except:
continue
Run Code Online (Sandbox Code Playgroud)
谢谢,
我已经搜索过,并且可以找到解决类似主题的问题,但没有找到针对此特定问题和语言的问题。在阅读了一些问答但没有找到答案后,我搜索了java、for-loop和continue并得到了零结果。
我在一次大学测验中被问到这个问题:
如果我有:
int n = 3;
for (int i = 1; i <= n; i++) {
System.out.print(" x ");
for (int j = 1; j <= n; j++) {
System.out.println(" x ");
continue;
//no content here
}
}
Run Code Online (Sandbox Code Playgroud)
continue语句后没有任何内容;这个使用如何continue影响这个循环?是否会导致第二个循环中断,循环是否继续迭代?
$arr = array('not want to print','foo','bar');
foreach($arr as $item) {
switch($item) {
case 'foo':
$item = 'bar';
break;
case 'not want to print':
continue;
break;
}
echo $item;
}
Run Code Online (Sandbox Code Playgroud)
但是," not want to print"得到了回应.为什么继续不适用于foreach?
为了跳出内循环,并继续外循环,我们可以使用continue Label或使用break.
以下是来自http://www.goinggo.net/2013/11/label-breaks-in-go.html的示例
原帖是使用continue Label模式:
guestList := []string{"bill", "jill", "joan"}
arrived := []string{"sally", "jill", "joan"}
CheckList:
for _, guest := range guestList {
for _, person := range arrived {
fmt.Printf("Guest[%s] Person[%s]\n", guest, person)
if person == guest {
fmt.Printf("Let %s In\n", person)
continue CheckList
}
}
}
Run Code Online (Sandbox Code Playgroud)
通过使用可以实现相同的结果break,如下所示:http:
//play.golang.org/p/0YUjkdxxRE
guestList := []string{"bill", "jill", "joan"}
arrived := []string{"sally", "jill", "joan"}
for _, guest := range guestList {
for …Run Code Online (Sandbox Code Playgroud) 我偶然发现了这段代码:
public IEnumerable<object> Process()
{
foreach (var item in items)
{
if (item.Created < DateTime.Now)
{
yield return item;
continue;
}
}
}
Run Code Online (Sandbox Code Playgroud)
有人可以帮助我理解为什么continue在这种情况下不是不必要的(VS不标记continue为冗余控制流跳转语句)?
考虑以下代码:
let value = "ABCDE"
for letter in value {
print(letter)
}
Run Code Online (Sandbox Code Playgroud)
这打印:
A
B
C
D
E
Run Code Online (Sandbox Code Playgroud)
现在考虑假设的代码,当它找到“2”时,它会在循环中向前跳过 2。这是显示我要问的内容的伪代码......
let value = "AB2DE"
for letter in value {
if letter == "2" {
continue 2 <-- Does Swift support something similar to this?
}
print(letter)
}
Run Code Online (Sandbox Code Playgroud)
上面的输出看起来像这样:
A
B
E
Run Code Online (Sandbox Code Playgroud)
注意:当然,我可以通过管理跳过计数器轻松实现此目的,然后使用continue,在进行时递减计数器,或者,如果源支持下标,正如其他人建议的那样,我可以使用循环while来管理自己的索引。有多种方法可以解决这个假设的问题,但这不是我问题的目的。
我的问题是专门查明该语言是否原生支持一次跳过 for 循环的多次迭代(例如类似continueManyor的东西continue x)。
尝试完成要求我执行以下任务的任务:
“编写一个while循环,该循环计算1到20(含)之间的整数之和,不包括那些被3整除的整数。(提示:您会发现模运算符(%)和continue语句很方便。)
我尝试自己构造代码,但是对代码的评估超时。我猜我的语法不正确并导致无限循环
total, x = 0, 1
while x >=1 and x <= 20:
if x%3==0:
continue
if x%3 != 0:
print(x)
x+=1
total+=1
print(total)
Run Code Online (Sandbox Code Playgroud)
预期的答案应该是:
20 19 17 16 14 13 11 10 8 7 5 4 2 1
但是我只是收到“超时”错误
***最新::
尝试这样做:
total, x = 0, 1
while x>=1 and x<=20:
if x%3 == 0:
x+=1
continue
if x%3 != 0:
print(x)
x+=1
total=+1
print(total)
Run Code Online (Sandbox Code Playgroud)
收到此::
Traceback (most recent call last):
File "/usr/src/app/test_methods.py", line 23, in test
self.assertEqual(self._output(), …Run Code Online (Sandbox Code Playgroud) 我有一个 for 循环,其中有一个简单的单行 if 条件。我想在 else 部分使用 continue 选项。
这不起作用:
def defA() :
return "yes"
flag = False
for x in range(4) :
value = defA() if flag else continue
Run Code Online (Sandbox Code Playgroud)
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
工作代码:
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud) 我希望你们做得很好,我正在学习 Javascript,我了解了“继续”,我们可以在循环中使用它进行迭代。但这是我无法得到的
首先看一下代码:
let k = 1
do {
if (k === 9) {
k++;
continue;
}
console.log(k + 1);
k++;
} while (k < 15);Run Code Online (Sandbox Code Playgroud)
当console.log中的值为(k+1)时,则打印9,而漏掉10。不明白为什么?
但是当使用这段代码时
let k = 1
do {
if (k === 9) {
k++;
continue;
}
console.log(k);
k++;
} while (k < 15);Run Code Online (Sandbox Code Playgroud)
当console.log中为(k)时,不打印9而打印10。
无法理解何时使用简单 (k) 和何时使用 (k+1) 背后的逻辑?
这就是我所理解的。如果K=1。控制台.log(k+1)。2 被打印。然后由于 k++。k 变为 2 并且条件经过测试,由于它为真,因此它将移动到另一个循环。这种情况还在继续。正确的?
谢谢