我需要在Python程序中模拟do-while循环.不幸的是,以下简单的代码不起作用:
list_of_ints = [ 1, 2, 3 ]
iterator = list_of_ints.__iter__()
element = None
while True:
if element:
print element
try:
element = iterator.next()
except StopIteration:
break
print "done"
Run Code Online (Sandbox Code Playgroud)
而不是"1,2,3,完成",它打印以下输出:
[stdout:]1
[stdout:]2
[stdout:]3
None['Traceback (most recent call last):
', ' File "test_python.py", line 8, in <module>
s = i.next()
', 'StopIteration
']
Run Code Online (Sandbox Code Playgroud)
我能做些什么来捕获'stop iteration'异常并正确地打破while循环?
以下将伪代码示为可能需要这样的事物的示例.
状态机:
s = ""
while True :
if state is STATE_CODE :
if "//" in s :
tokens.add( TOKEN_COMMENT, s.split( "//" )[1] )
state = …Run Code Online (Sandbox Code Playgroud) 我无法想出正确的分号和/或括号组合.我想这样做,但作为命令行的一行代码:
while [ 1 ]
do
foo
sleep 2
done
Run Code Online (Sandbox Code Playgroud) 这是一位高级经理提出的面试问题.
哪个更快?
while(1) {
// Some code
}
Run Code Online (Sandbox Code Playgroud)
要么
while(2) {
//Some code
}
Run Code Online (Sandbox Code Playgroud)
我说两者都有相同的执行速度,因为里面的表达式while应该最终评估为true或false.在这种情况下,两者都评估,true并且条件内没有额外的条件指令while.因此,两者都具有相同的执行速度,而我更喜欢(1).
但采访者自信地说:"检查你的基础知识.while(1)比快while(2)." (他没有测试我的信心)
这是真的?
我看到人们最近在很多帖子中试图读取这样的文件.
码
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char **argv)
{
char *path = argc > 1 ? argv[1] : "input.txt";
FILE *fp = fopen(path, "r");
if( fp == NULL ) {
perror(path);
return EXIT_FAILURE;
}
while( !feof(fp) ) { /* THIS IS WRONG */
/* Read and process data from file… */
}
if( fclose(fp) == 0 ) {
return EXIT_SUCCESS;
} else {
perror(path);
return EXIT_FAILURE;
}
}
Run Code Online (Sandbox Code Playgroud)
这个__CODE__循环有什么问题?
我注意到以下代码在Python中是合法的.我的问题是为什么?有具体原因吗?
n = 5
while n != 0:
print n
n -= 1
else:
print "what the..."
Run Code Online (Sandbox Code Playgroud) 我听过很多次了.向后计数时JavaScript循环真的更快吗?如果是这样,为什么?我已经看到一些测试套件示例显示反向循环更快,但我找不到任何解释为什么!
我假设它是因为循环不再需要在每次检查它是否完成时评估属性并且它只是检查最终的数值.
即
for (var i = count - 1; i >= 0; i--)
{
// count is only evaluated once and then the comparison is always on 0.
}
Run Code Online (Sandbox Code Playgroud) 问题1:
为什么以下代码在没有return语句的情况下编译?
public int a() {
while(true);
}
Run Code Online (Sandbox Code Playgroud)
注意:如果我在一段时间后添加返回,那么我得到一个Unreachable Code Error.
问题2:
另一方面,为什么以下代码编译,
public int a() {
while(0 == 0);
}
Run Code Online (Sandbox Code Playgroud)
即使以下没有.
public int a(int b) {
while(b == b);
}
Run Code Online (Sandbox Code Playgroud) 为什么以下工作正常?
String str;
while (condition) {
str = calculateStr();
.....
}
Run Code Online (Sandbox Code Playgroud)
但据说这个是危险的/不正确的:
while (condition) {
String str = calculateStr();
.....
}
Run Code Online (Sandbox Code Playgroud)
是否有必要在循环外声明变量?
我有一个脚本,并想要询问用户一些信息,脚本无法继续,直到用户填写此信息.以下是我尝试将命令放入循环以实现此目的但由于某种原因它不起作用.
echo "Please change password"
while passwd
do
echo "Try again"
done
Run Code Online (Sandbox Code Playgroud)
我尝试了while循环的许多变体:
while `passwd`
while [[ "`passwd`" -gt 0 ]]
while [ `passwd` -ne 0 ]]
# ... And much more
Run Code Online (Sandbox Code Playgroud)
但我似乎无法让它发挥作用.
我已经用Java编程了好几年了,但我刚刚回到学校获得正式学位.我很惊讶地发现,在我上一次任务中,我使用了如下所示的循环而丢失了分数.
do{
//get some input.
//if the input meets my conditions, break;
//Otherwise ask again.
} while(true)
Run Code Online (Sandbox Code Playgroud)
现在我的测试我只是扫描一些控制台输入,但我被告知这种循环是不鼓励的,因为使用break类似于goto,我们只是不这样做.
我完全理解goto和它的Java堂兄的陷阱break:label,我很有意识不使用它们.我也意识到一个更完整的程序会提供一些其他的逃避手段,例如刚刚结束程序,但这不是我教授引用的原因,所以......
怎么了do-while(true)?
while-loop ×10
java ×3
bash ×2
c ×2
do-while ×2
loops ×2
optimization ×2
python ×2
syntax ×2
command ×1
compilation ×1
feof ×1
file ×1
for-loop ×1
if-statement ×1
javascript ×1
performance ×1
return ×1